News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests

Main Menu

Adding two numbers with MASM32 ?

Started by sunshine33, August 03, 2018, 08:01:30 AM

Previous topic - Next topic

avcaballero

#30
Here's my example 100% masm source. Ask your teacher what is my punctuation. :bgrin:


.386
.MODEL     flat, stdcall
OPTION     casemap :none
         
INCLUDE    windows.inc
INCLUDE    kernel32.inc
INCLUDE    masm32.inc
         
INCLUDELIB kernel32.lib
INCLUDELIB masm32.lib

cdMaxSize  EQU 11

.DATA?
  szNumber1 db cdMaxSize dup(?)
  szNumber2 db cdMaxSize dup(?)
  iNum      dd ?
 
.DATA
  szTxtNum1 db "Your first number: ", 0
  szTxtNum2 db "Your second number: ", 0
  szTxtSum  db "Both sum: ", 0
  number1   DD 442
  number2   dd 22
  msg       db cdMaxSize dup(0)

.CODE
  num2str proc uses ebx edx esi
    ; In: eax, number value
    ; Out: eax, offset of value in string format
    mov     esi, offset msg + cdMaxSize - 1
    mov     ebx, 10
    @Next:
      dec     esi
      xor     edx, edx
      div     ebx
      or      edx, 30h
      mov     byte ptr [esi], dl
      or      eax, eax
    jnz     @Next
    mov     eax, esi
    ret
  num2str endp
 
  str2num proc uses ebx ecx edx esi edi
    ; In: eax, offset to str_num
    ; out: eax, the number converted
    mov     edi, eax              ; offset
    mov     eax, 0
    mov     ecx, cdMaxSize
    repne   scasb
    jne     @NoFound
    sub     edi, 2
    sub     ecx, cdMaxSize
    neg     ecx
    mov     ebx, 1                ; factor
    mov     esi, 0                ; Acumulative
    @Next:
      dec     ecx
      jl      @Exit
      xor     eax, eax            ; clear it
      mov     al, byte ptr [edi]
      and     al, 15              ; 2num
      mul     ebx
      add     esi, eax            ; get into accumulative
      mov     eax, ebx            ; increases factor
      mov     ebx, 10
      mul     ebx
      mov     ebx, eax
      dec     edi                 ; reset pointer
    jmp     @Next
    @Exit:
    mov     eax, esi
    @NoFound:
    ret
  str2num endp

  start:
    ; Get the numbers in asciiz
    invoke     StdOut, offset szTxtNum1
    invoke     StdIn, offset szNumber1, cdMaxSize
    invoke     StdOut, offset szTxtNum2
    invoke     StdIn, offset szNumber2, cdMaxSize
   
    ; Convert to numbers and sum
    mov        eax, offset szNumber1
    call       str2num
    mov        ebx, eax
   
    mov        eax, offset szNumber2
    call       str2num
    add        ebx, eax
   
    ; Show the result
    invoke     StdOut, offset szTxtSum
    mov        eax, ebx
    call       num2str
    invoke     StdOut, eax
    INVOKE     ExitProcess, 0

END start

Executing it:
; >sumawm02
Your first number: 45
Your second number: 23
Both sum: 68




nidud

#31
deleted

nidud

#32
deleted

jj2007

Quote from: sunshine33 on August 04, 2018, 12:09:38 AMHow do you do this in assembly language ?

Quoteprintf("Enter two integers: ");

Like this maybe?

include \masm32\include\masm32rt.inc

.code
start:
  printf("Enter two integers:") ; needs console assembly and link
  MsgBox 0, "That was easy, right?", "Hi Sunshine", MB_OK
  exit

end start


You are coming from C, that's fine, you'll be comfortable with the CRT functions. Check the \Masm32\Help folder, it's full of goodies. To get a feeling how to set up a simple program, check the \Masm32\Examples folder. Open the *.asm sources in \Masm32\qEditor.exe, and see what they are doing. Attention for console applications you need "console assemble & link".

Re "emulated Basic": Hutch came from PowerBasic, I came from GfaBasic.
His version: print str$(eax), 13, 10
My version: Print Str$(eax), CrLf$
;)

sunshine33

Thanks for all the reply .


Quote from: dedndave on August 04, 2018, 01:19:07 AM
the hard part of the problem is not adding two values together
nor is it even converting the result to an ASCII string and displaying it
the hard part is getting the two values from the user

This is what i have been trying to figure out in pure Assembly language without using any c type syntax's



I would like to learn Assembly language with MASM32 a bit more seriously


sunshine33

Quote from: caballero on August 04, 2018, 01:39:46 AM
Here's my example 100% masm source. Ask your teacher what is my punctuation. :bgrin:


.386
.MODEL     flat, stdcall
OPTION     casemap :none
         
INCLUDE    windows.inc
INCLUDE    kernel32.inc
INCLUDE    masm32.inc
         
INCLUDELIB kernel32.lib
INCLUDELIB masm32.lib

cdMaxSize  EQU 11

.DATA?
  szNumber1 db cdMaxSize dup(?)
  szNumber2 db cdMaxSize dup(?)
  iNum      dd ?
 
.DATA
  szTxtNum1 db "Your first number: ", 0
  szTxtNum2 db "Your second number: ", 0
  szTxtSum  db "Both sum: ", 0
  number1   DD 442
  number2   dd 22
  msg       db cdMaxSize dup(0)

.CODE
  num2str proc uses ebx edx esi
    ; In: eax, number value
    ; Out: eax, offset of value in string format
    mov     esi, offset msg + cdMaxSize - 1
    mov     ebx, 10
    @Next:
      dec     esi
      xor     edx, edx
      div     ebx
      or      edx, 30h
      mov     byte ptr [esi], dl
      or      eax, eax
    jnz     @Next
    mov     eax, esi
    ret
  num2str endp
 
  str2num proc uses ebx ecx edx esi edi
    ; In: eax, offset to str_num
    ; out: eax, the number converted
    mov     edi, eax              ; offset
    mov     eax, 0
    mov     ecx, cdMaxSize
    repne   scasb
    jne     @NoFound
    sub     edi, 2
    sub     ecx, cdMaxSize
    neg     ecx
    mov     ebx, 1                ; factor
    mov     esi, 0                ; Acumulative
    @Next:
      dec     ecx
      jl      @Exit
      xor     eax, eax            ; clear it
      mov     al, byte ptr [edi]
      and     al, 15              ; 2num
      mul     ebx
      add     esi, eax            ; get into accumulative
      mov     eax, ebx            ; increases factor
      mov     ebx, 10
      mul     ebx
      mov     ebx, eax
      dec     edi                 ; reset pointer
    jmp     @Next
    @Exit:
    mov     eax, esi
    @NoFound:
    ret
  str2num endp

  start:
    ; Get the numbers in asciiz
    invoke     StdOut, offset szTxtNum1
    invoke     StdIn, offset szNumber1, cdMaxSize
    invoke     StdOut, offset szTxtNum2
    invoke     StdIn, offset szNumber2, cdMaxSize
   
    ; Convert to numbers and sum
    mov        eax, offset szNumber1
    call       str2num
    mov        ebx, eax
   
    mov        eax, offset szNumber2
    call       str2num
    add        ebx, eax
   
    ; Show the result
    invoke     StdOut, offset szTxtSum
    mov        eax, ebx
    call       num2str
    invoke     StdOut, eax
    INVOKE     ExitProcess, 0

END start

Executing it:
; >sumawm02
Your first number: 45
Your second number: 23
Both sum: 68


When i tried to Assemble it in MASAM32 SDK , its giving me errors .

Which assembler do you use ?

hutch--

There are no paths in the source code for the includes and libraries. You need to "make" method to build it.

nidud

#37
deleted

jj2007

Quote from: sunshine33 on August 04, 2018, 10:46:08 PMWhen i tried to Assemble it in MASAM32 SDK , its giving me errors.
- replace the include etc lines with include \masm32\include\masm32rt.inc
  Masm32 has hard-coded paths. That is old-fashioned but very, very efficient. Read the masm32rt.inc file to understand.
  In particular:
  include \Masm32\include\windows.inc  ; correct! path relative to the source's path, no unnecessary SHOUTING, perfect!
  INCLUDE C:\Masm32\include\windows.inc  ; BAD, assumes that the user uses drive C:
  INCLUDE kernel32.inc  ; BAD, assumes that the user uses environment variables

- replace num2str with @num2str (3x)
  Caballero has used the name of a Masm32 SDK macro for a proc. That won't work, so num2str needs a new name.

Oh, and btw: "its giving me errors" is not an acceptable way to ask for help. You post the exact error messages including the line numbers.

avcaballero

> When i tried to Assemble it in MASAM32 SDK , its giving me errors
> Which assembler do you use ?

The "bat" file had also the compilation of another program (ConWM01), my fault. You only need these two lines, assuming that your compiler is in C:\masm32, compiling "SumaWM02.asm" from its folder:

C:\masm32\bin\ml.exe /c /coff /Cp /IC:\masm32\Include SumaWM02.asm
C:\masm32\bin\link.exe /SUBSYSTEM:CONSOLE /LIBPATH:c:\masm32\lib SumaWM02

The compiler I use is masm

hutch--

If its the MASM32 SDK you are using, use its methods to get up and going. Custom builds are fine once you know your way around but while you are learning how it works, use the stuff provided as it is designed to be easy enough to use.

avcaballero

It is the MASM32 SDK what I am using, no custom build. My choice is to tell the compiler/linker where are the includes/libraries, not in the source. This way you only have to change the bat in the case the includes/libraries are in other location.

I have put all what is needed in my first post. Please, download and tell me if everything goes right.

sunshine33

My god , i finally made it work  :greenclp:








Quoteinclude windows.inc

This is included so that we can used predefined constants and
structures. It is a master include file that defines all the
Windows data types, function calls, data structures, and constant
identifiers so you can refer to them by name in your code. Of
note here is that windows.inc defines NULL and MB_OK so that
these can be used by name to make the code more readable.

include kernel32.inc

This contains the prototypes for functions in kernel32.dll

include user32.inc

This contains the prototypes for functions in user32.dll

includelib kernel32.lib

It is required to invoke ExitProcess api.

Ref: https://msdn.microsoft.com/en-us/library/windows/desktop/ms682658%28v=vs.85%29.aspx

includelib user32.lib

It is required to invoke MessageBox api.

Ref: https://msdn.microsoft.com/en-us/library/windows/desktop/ms645505%28v=vs.85%29.aspx

.data
MessageText db "Hello World",0

Defining a array of bytes containing string "Hello World". This is
the message we want to display.

CaptionText db "Welcome to ASM",0

Defining a array of bytes containing string "Welcome to ASM". This
is the title of the window.

Note: I have null terminated both the strings.
Reason?? Left for the home work and try yourself.

.code
Start:

invoke MessageBox,NULL,Addr MessageText,Addr CaptionText,MB_OK

Invoke is similar to call function except one difference. Invoke
does type checking of parameters whereas call does not. So, to
invoke to work, we have included *.inc file for the respective
.dll as *.inc file contain the prototypes for functions in .dll

MessageBox api:

This api is defined in user32.dll. It has 4 Parameters:

hWnd [in, optional]

    Type: HWND

    A handle to the owner window of the message box to be created. If
    this parameter is NULL, the message box has no owner window.

lpText [in, optional]

    Type: LPCTSTR

    The message to be displayed. If the string consists of more than
    one line, you can separate the lines using a carriage return
    and/or linefeed character between each line.

lpCaption [in, optional]

    Type: LPCTSTR

    The dialog box title. If this parameter is NULL, the default
    title is Error.

uType [in]

    Type: UINT

We have passed the following 4 arguments:

NULL - there is no parent window.
addr MessageText - address of our text string.
addr CaptionText - address of our caption.
MB_OK - one of a set of pre-defined styles.

Addr

This give the address of the string. For above, address of
MessageText and CaptionText.

invoke ExitProcess,0

This calls the ExitProcess with argument 0 i.e the return value.
This api is invoked to exit the program gracefully.

End Start

The only remaining problem , The program is exiting too fast  :(

avcaballero

> My god , i finally made it work
It is not necessary include the path in the source if you tell it to the compiler/linker

> The only remaining problem , The program is exiting too fast
What exactly means that?

hutch--

Before your exit of the program, use the macro "inkey". That stops the app until you press any key.