Author Topic: Adding two numbers with MASM32 ?  (Read 28562 times)

hutch--

  • Administrator
  • Member
  • ******
  • Posts: 10572
  • Mnemonic Driven API Grinder
    • The MASM32 SDK
Re: Adding two numbers with MASM32 ?
« Reply #45 on: August 05, 2018, 12:43:15 AM »
> 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.
hutch at movsd dot com
http://www.masm32.com    :biggrin:  :skrewy:

caballero

  • Member
  • *****
  • Posts: 2136
  • Matrix - Noah
    • abre ojos ensamblador
Re: Adding two numbers with MASM32 ?
« Reply #46 on: August 05, 2018, 12:51:23 AM »
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:
The logic of the error is hidden among the most unexpected lines of the program

sunshine33

  • Regular Member
  • *
  • Posts: 46
  • MASM32
Re: Adding two numbers with MASM32 ?
« Reply #47 on: August 05, 2018, 12:53:54 AM »
Before your exit of the program, use the macro "inkey". That stops the app until you press any key.



Code: [Select]
.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--

  • Administrator
  • Member
  • ******
  • Posts: 10572
  • Mnemonic Driven API Grinder
    • The MASM32 SDK
Re: Adding two numbers with MASM32 ?
« Reply #48 on: August 05, 2018, 01:02:08 AM »
> 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 at movsd dot com
http://www.masm32.com    :biggrin:  :skrewy:

hutch--

  • Administrator
  • Member
  • ******
  • Posts: 10572
  • Mnemonic Driven API Grinder
    • The MASM32 SDK
Re: Adding two numbers with MASM32 ?
« Reply #49 on: August 05, 2018, 01:09:04 AM »
 :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.
hutch at movsd dot com
http://www.masm32.com    :biggrin:  :skrewy:

sunshine33

  • Regular Member
  • *
  • Posts: 46
  • MASM32
Re: Adding two numbers with MASM32 ?
« Reply #50 on: August 05, 2018, 01:28:45 AM »
I would like to continue using this code to learn assembly language in MASM32 SDK ,

Code: [Select]
.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

  • Member
  • *****
  • Posts: 13871
  • Assembly is fun ;-)
    • MasmBasic
Re: Adding two numbers with MASM32 ?
« Reply #51 on: August 05, 2018, 04:04:35 AM »
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:

Code: [Select]
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

the best way to confuse a person learning is to flood them with options.
:t

sunshine33

  • Regular Member
  • *
  • Posts: 46
  • MASM32
Re: Adding two numbers with MASM32 ?
« Reply #52 on: August 05, 2018, 04:50:59 AM »
It worked  :greenclp:
Thanks jj2007



Code: [Select]
.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

  • Member
  • *****
  • Posts: 1937
Re: Adding two numbers with MASM32 ?
« Reply #53 on: August 05, 2018, 08:01:08 AM »
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)
Code: [Select]
      .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.
« Last Edit: August 05, 2018, 09:04:26 AM by zedd151 »
Regards, zedd.
:tongue:

sunshine33

  • Regular Member
  • *
  • Posts: 46
  • MASM32
Re: Adding two numbers with MASM32 ?
« Reply #54 on: August 05, 2018, 04:01:55 PM »
Thank you caballero for the original code ,
Thank you zedd151 for explaining that ,
Suddenly i feel like i can learn this properly

 :greenclp:

deeR44

  • Member
  • **
  • Posts: 101
Re: Adding two numbers with MASM32 ?
« Reply #55 on: July 26, 2022, 04:07:51 PM »

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

What is "val"?


NoCforMe

  • Member
  • *****
  • Posts: 1104
Re: Adding two numbers with MASM32 ?
« Reply #56 on: July 26, 2022, 05:57:49 PM »
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").

jj2007

  • Member
  • *****
  • Posts: 13871
  • Assembly is fun ;-)
    • MasmBasic
Re: Adding two numbers with MASM32 ?
« Reply #57 on: July 26, 2022, 06:17:16 PM »
What is "val"?
Quote
    mov num1, val(pbuf1)        ; convert input strings to numbers

deeR44

  • Member
  • **
  • Posts: 101
Re: Adding two numbers with MASM32 ?
« Reply #58 on: July 27, 2022, 01:28:29 PM »
Thanks, guys,
I appreciate it!

deeR44

  • Member
  • **
  • Posts: 101
Re: Adding two numbers with MASM32 ?
« Reply #59 on: July 27, 2022, 01:59:39 PM »
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.