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

caballero

  • Member
  • *****
  • Posts: 2033
  • Matrix - Noah
    • abre ojos ensamblador
Re: Adding two numbers with MASM32 ?
« Reply #30 on: August 04, 2018, 01:39:46 AM »
Here's my example 100% masm source. Ask your teacher what is my punctuation. :bgrin:

Code: [Select]
.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:
Code: [Select]
; >sumawm02
Your first number: 45
Your second number: 23
Both sum: 68


« Last Edit: August 04, 2018, 11:45:35 PM by caballero »
The logic of the error is hidden among the most unexpected lines of the program

nidud

  • Member
  • *****
  • Posts: 2388
    • https://github.com/nidud/asmc
Re: Adding two numbers with MASM32 ?
« Reply #31 on: August 04, 2018, 01:52:24 AM »
deleted
« Last Edit: February 25, 2022, 01:15:06 AM by nidud »

nidud

  • Member
  • *****
  • Posts: 2388
    • https://github.com/nidud/asmc
Re: Adding two numbers with MASM32 ?
« Reply #32 on: August 04, 2018, 02:09:25 AM »
deleted
« Last Edit: February 25, 2022, 01:15:21 AM by nidud »

jj2007

  • Member
  • *****
  • Posts: 13299
  • Assembly is fun ;-)
    • MasmBasic
Re: Adding two numbers with MASM32 ?
« Reply #33 on: August 04, 2018, 02:20:33 AM »
How do you do this in assembly language ?

Quote
printf("Enter two integers: ");

Like this maybe?

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

  • Regular Member
  • *
  • Posts: 46
  • MASM32
Re: Adding two numbers with MASM32 ?
« Reply #34 on: August 04, 2018, 10:44:26 PM »
Thanks for all the reply .


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

  • Regular Member
  • *
  • Posts: 46
  • MASM32
Re: Adding two numbers with MASM32 ?
« Reply #35 on: August 04, 2018, 10:46:08 PM »
Here's my example 100% masm source. Ask your teacher what is my punctuation. :bgrin:

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

  • Administrator
  • Member
  • ******
  • Posts: 10005
  • Mnemonic Driven API Grinder
    • The MASM32 SDK
Re: Adding two numbers with MASM32 ?
« Reply #36 on: August 04, 2018, 10:52:39 PM »
There are no paths in the source code for the includes and libraries. You need to "make" method to build it.
hutch at movsd dot com
http://www.masm32.com    :biggrin:  :skrewy:

nidud

  • Member
  • *****
  • Posts: 2388
    • https://github.com/nidud/asmc
Re: Adding two numbers with MASM32 ?
« Reply #37 on: August 04, 2018, 11:16:30 PM »
deleted
« Last Edit: February 25, 2022, 01:15:40 AM by nidud »

jj2007

  • Member
  • *****
  • Posts: 13299
  • Assembly is fun ;-)
    • MasmBasic
Re: Adding two numbers with MASM32 ?
« Reply #38 on: August 04, 2018, 11:21:16 PM »
When 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.

caballero

  • Member
  • *****
  • Posts: 2033
  • Matrix - Noah
    • abre ojos ensamblador
Re: Adding two numbers with MASM32 ?
« Reply #39 on: August 04, 2018, 11:34:18 PM »
> 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
The logic of the error is hidden among the most unexpected lines of the program

hutch--

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

caballero

  • Member
  • *****
  • Posts: 2033
  • Matrix - Noah
    • abre ojos ensamblador
Re: Adding two numbers with MASM32 ?
« Reply #41 on: August 05, 2018, 12:17:59 AM »
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.
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 #42 on: August 05, 2018, 12:31:21 AM »
My god , i finally made it work  :greenclp:








Quote
include 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  :(

caballero

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

hutch--

  • Administrator
  • Member
  • ******
  • Posts: 10005
  • Mnemonic Driven API Grinder
    • The MASM32 SDK
Re: Adding two numbers with MASM32 ?
« Reply #44 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.
hutch at movsd dot com
http://www.masm32.com    :biggrin:  :skrewy: