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

deeR44


How would I include a 'zip' file in a post?

hutch--


deeR44


That sounds easy enough. Thank you--I'll try it tomorrow.

HSE

Equations in Assembly: SmplMath

deeR44

Quote from: NoCforMe on July 27, 2022, 07:26:09 PM
So now do it where you get those numbers from the user (via text input). How would you do that?

Here is the program that inputs two integers, adds them together, and displays the result.
This time it's in 'zip' format and lines will line up nicely.

hutch--

I downloaded it and built it but I don't know what input it requires. If I run the exe by itself, nothing happens, tried two numbers on the command line but itt does nothing, how do I input any numbers to it ?

hutch--

While waiting, here is a simple 32 bit example of getting user input, converting it to DWORD integers, adding the two together then converting it back to string to display.

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

comment * -----------------------------------------------------
                        Build this  template with
                       "CONSOLE ASSEMBLE AND LINK"
        ----------------------------------------------------- *

    .data?
      inp1 db 128 dup (?)
      inp2 db 128 dup (?)

    .data
      pin1 dd inp1
      pin2 dd inp2

    .code

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

    call main
    inkey "Press any key to exit ...."
    exit

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

main proc

    LOCAL var1  :DWORD
    LOCAL var2  :DWORD
    LOCAL rslt  :DWORD
    LOCAL obuf[32]:BYTE

  ; --------------
  ; Get user input
  ; --------------
    print "Enter an integer "
    invoke StdIn,pin1,128

    print "Enter another "
    invoke StdIn,pin2,128
  ; --------------

  ; ------------------
  ; display input data
  ; ------------------
    print "The first integer = "
    print pin1,13,10

    print "The second integer = "
    print pin2,13,10
  ; ------------------

  ; ------------------------------
  ; convert input strings to DWORD
  ; ------------------------------
    invoke atodw,pin1
    mov var1, eax

    invoke atodw,pin2
    mov var2, eax
  ; ------------------------------

  ; --------------------
  ; perform the addition
  ; --------------------
    mov eax, var1
    add eax, var2
  ; --------------------

  ; ------------------------------
  ; load the output buffer address
  ; ------------------------------
    lea edx, obuf
    mov rslt, edx

  ; -------------------------------------
  ; convert the result in eax to a string
  ; -------------------------------------
    invoke dwtoa,eax,rslt
  ; -------------------------------------

  ; ------------------
  ; display the result
  ; ------------------
    print "The total = "
    print rslt,13,10
  ; ------------------

    ret

main endp

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

end start

deeR44

Quote from: hutch-- on August 02, 2022, 03:45:46 PM
I downloaded it and built it but I don't know what input it requires. If I run the exe by itself, nothing happens, tried two numbers on the command line but itt does nothing, how do I input any numbers to it ?


I ran the program and got this result:

C:\masm32\projects\AddTwoInts>addtwointegers

Type your number:  1234

***1234***

Number of input digits is: 4.

Type your number:      000016

***16***

Number of input digits is: 2.

Result: 1250

C:\masm32\projects\AddTwoInts>

The second number was preceded by spaces and a tab, then leading ASCII zeros, the number and, finally, some more spaces and tabs. The program eliminates this garbage.


Does your program include this stuff except the obvious that doesn't belong"

    .NOLIST
      include \masm32\include\masm32rt.inc
      include \masm32\include\fpu.inc
      includelib  \masm32\lib\fpu.lib
      include \masm32\include\winmm.inc
      includelib  \masm32\lib\winmm.lib
    .LIST

; #########################################################################

.data

input_buffer  db  128 DUP (0)

output_buffer db  128 DUP (0)

temp_buffer   db  128 DUP (0)

    ALIGN   4

string_size   dd    0           ; size of a measured input string
valid_sum     dd    0           ; sum of valid digits
nseed_ms      dd    0           ; ms since windows started
input_handle  dd    0           ; token returned by Windows
len_input     dd    0           ; length of input file

number1       DD    442
;  number2       dd     22

; #########################################################################

I loaded the 'zip' from my original post, extracted it, assembled it, and finally ran it producing the above result.



hutch--

I downloaded the zip file, unzipped it, built it as a default console app and it built correctly. When I ran it nothing was displayed at the console. It did not respond to command line arguments so I don't know how to get any output from it.

deeR44

Quote from: hutch-- on August 03, 2022, 02:14:06 PM
I downloaded the zip file, unzipped it, built it as a default console app and it built correctly. When I ran it nothing was displayed at the console. It did not respond to command line arguments so I don't know how to get any output from it.

It wasn't written to accept command line arguments as you can see in the source.
I have no idea why it doesn't run on your system. During development, I assembled this program and ran it
dozens of times. Sometimes with horrific results. But I always got some input questions.   Which are:
"Type your number:  ".

NoCforMe

Your program worked for me. A mystery why Hutch can't seem to get it to run. Not a great program, but it does work.
Assembly language programming should be fun. That's why I do it.

hutch--

OK, I got it to work, it helps if you zip the exe file with it and how to use it.


K:\add2>addtwointegers

Type your number:  160

***160***

Number of input digits is: 3.

Type your number:  140

***140***

Number of input digits is: 3.

Result: 300

K:\add2>


jj2007

Quote from: deeR44 on August 02, 2022, 02:52:06 PMHere is the program that inputs two integers, adds them together, and displays the result.
This time it's in 'zip' format and lines will line up nicely.

Congrats, your first working MASM program :thumbsup:

NoCforMe

OK, if that really is your first MASM program, then mazeltov. It's a good feeling, no?
Assembly language programming should be fun. That's why I do it.

bomz

Hi to all! answer is near the topic
ProcEntry   PROCESSENTRY32 < 296, 0 >
how correct fill structure in data ?

align good idea when you multiple times use string to compare or big table of data