News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests
NB: Posting URL's See here: Posted URL Change

Main Menu

Adding two numbers with MASM32 ?

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

Previous topic - Next topic

hutch--

> It is not necessary include the path in the source if you tell it to the compiler/linker

There are many ways to build an executable, set the environment, add the paths to the ML and LINK lines but the best way to confuse a person learning is to flood them with options.

avcaballero

Which option if everything goes in a batch? I guess that the confusing comes from he uses a text editor with predefined options. IE, why am I going to use the batch he gave me? :greensml:

sunshine33

Quote from: hutch-- on August 05, 2018, 12:39:17 AM
Before your exit of the program, use the macro "inkey". That stops the app until you press any key.



.386
.MODEL     flat, stdcall
OPTION     casemap :none

INCLUDE    \masm32\include\windows.inc
INCLUDE    \masm32\include\masm32.inc         
INCLUDE    \masm32\include\kernel32.inc

         
INCLUDELIB \masm32\lib\kernel32.lib
INCLUDELIB \masm32\lib\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


Sorry this is my first time with MASM32 SDK , Where do i add that code ?

hutch--

> Where do i add that code ?

After you display the result.

Here is a test piece.

; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤
    include \masm32\include\masm32rt.inc
; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤

comment * -----------------------------------------------------
                     Build this console app with
                  "MAKEIT.BAT" on the PROJECT menu.
        ----------------------------------------------------- *

    .code

start:
   
; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤

    call main
    inkey
    exit

; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤

main proc

    LOCAL pbuf1 :DWORD          ; allocate pointers
    LOCAL pbuf2 :DWORD
    LOCAL buff1[64]:BYTE        ; allocate buffers
    LOCAL buff2[64]:BYTE

    LOCAL num1  :DWORD          ; variable for numbers
    LOCAL num2  :DWORD

    LOCAL rslt  :DWORD          ; variable for result

    lea eax, buff1              ; load address into 1st pointer
    mov pbuf1, eax

    lea eax, buff2              ; load address into 2nd pointer
    mov pbuf2, eax

    print "Enter a number "
    invoke StdIn,pbuf1,64       ; get the first number from the console

    print "Enter another  "
    invoke StdIn,pbuf2,64       ; get the second number from the console

    mov num1, val(pbuf1)        ; convert input strings to numbers
    mov num2, val(pbuf2)

    mov eax, num1               ; add thhe two numbers
    add eax, num2

    mov rslt, eax               ; store the result in a variable

    print "The total = "
    print str$(rslt),13,10      ; display the result

    ret

main endp

; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤

end start


A suggestion.

Use .686p instead of .386 and you can use later instructions.

hutch--

 :P

> IE, why am I going to use the batch he gave me?

Lets look at a few options.
1. Set the paths in the OS. Tough if you want multiple environments.
2. Set the paths using "set" in the build batch file. Fine if you already know how to do it.
3. Manually add the paths to the ML and LINK command lines if you are familiar with both.

This is how you confuse someone who is starting to learn assembler, flood them with options.

sunshine33

I would like to continue using this code to learn assembly language in MASM32 SDK ,

.386
.MODEL     flat, stdcall
OPTION     casemap :none

INCLUDE    \masm32\include\windows.inc
INCLUDE    \masm32\include\masm32.inc         
INCLUDE    \masm32\include\kernel32.inc

         
INCLUDELIB \masm32\lib\kernel32.lib
INCLUDELIB \masm32\lib\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


But i cant get this program to work with inkey
It says

error A2008 : syntax error : inkey

jj2007

It will work perfectly if you follow the handful of instructions I gave you in reply #38.

inkey is one of the macros that get included with the one-liner include \masm32\include\masm32rt.inc, which replaces everything before cdMaxSize  EQU 11:

include \masm32\include\masm32rt.inc
cdMaxSize  EQU 11

.DATA?
...


The masm32rt.inc one-liner is the standard way of writing an assembler program. No special options or batch files required, just pick the "Console assemble & link" menu in \masm32\qEditor.exe, and it will build your program.

In case you still have doubts where to place the inkey:

    invoke     StdOut, eax
    inkey " - it works, hooray!!!"
    INVOKE     ExitProcess, 0

Quote from: hutch-- on August 05, 2018, 12:43:15 AMthe best way to confuse a person learning is to flood them with options.
:t

sunshine33

It worked  :greenclp:
Thanks jj2007



.386
.MODEL     flat, stdcall
OPTION     casemap :none



INCLUDE    \masm32\include\masm32rt.inc



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
    inkey
    INVOKE     ExitProcess, 0
   


   
   

END start





:greenclp:


zedd151

#53
When using "include \masm32\include\masm32rt.inc", you don't need those lines above it, masm32rt.inc takes care
of .386, .model, option, etc...

-------------------------------------------
.386
.MODEL     flat, stdcall
OPTION     casemap :none


INCLUDE    \masm32\include\masm32rt.inc
--------------------------------------------
:t

Just so you know what is in masm32rt.inc... (I removed comments for brevity here)

      .486
      .model flat, stdcall
      option casemap :none
      include \masm32\include\windows.inc
      include \masm32\include\masm32.inc
      include \masm32\include\gdi32.inc
      include \masm32\include\user32.inc
      include \masm32\include\kernel32.inc
      include \masm32\include\Comctl32.inc
      include \masm32\include\comdlg32.inc
      include \masm32\include\shell32.inc
      include \masm32\include\oleaut32.inc
      include \masm32\include\ole32.inc
      include \masm32\include\msvcrt.inc
      include \masm32\include\dialogs.inc
      include \masm32\macros\macros.asm
      includelib \masm32\lib\masm32.lib
      includelib \masm32\lib\gdi32.lib
      includelib \masm32\lib\user32.lib
      includelib \masm32\lib\kernel32.lib
      includelib \masm32\lib\Comctl32.lib
      includelib \masm32\lib\comdlg32.lib
      includelib \masm32\lib\shell32.lib
      includelib \masm32\lib\oleaut32.lib
      includelib \masm32\lib\ole32.lib
      includelib \masm32\lib\msvcrt.lib

As you can see, it takes care of loading the listed include (.inc) files, and libraries (.lib) , as well as a more recent cpu (.486)
plus, it loads macros.asm, so you have more macros at your immediate disposal. Macros makes programming in assembler really easy.

sunshine33

Thank you caballero for the original code ,
Thank you zedd151 for explaining that ,
Suddenly i feel like i can learn this properly

:greenclp:

deeR44


    mov num1, val(pbuf1)        ; convert input strings to numbers
    mov num2, val(pbuf2)

What is "val"?


NoCforMe

It's a macro. Look in ...\masm32\macros\macros.asm.

Actually ends up invoking the function crt__Xtoi(), where X can be either a for ANSI or w for Unicode (for "wide").
Assembly language programming should be fun. That's why I do it.

jj2007

Quote from: deeR44 on July 26, 2022, 04:07:51 PM
What is "val"?
Quotemov num1, val(pbuf1)        ; convert input strings to numbers

deeR44


deeR44

Quote;   add  [number1], [number2]
;.data
;number1       DD    442
;number2       dd     22

    mov     eax, number1        ; get 1st argument
    add     eax, number2        ; add 2nd argument
    printf  ("\nResult: %u\n", eax)
My version of adding those two numbers from wayback.