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

deeR44

  • Member
  • **
  • Posts: 98
Re: Adding two numbers with MASM32 ?
« Reply #75 on: August 01, 2022, 02:19:40 PM »

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

hutch--

  • Administrator
  • Member
  • ******
  • Posts: 9816
  • Mnemonic Driven API Grinder
    • The MASM32 SDK
Re: Adding two numbers with MASM32 ?
« Reply #76 on: August 01, 2022, 02:46:37 PM »
Zip the file THEN post it.
hutch at movsd dot com
http://www.masm32.com    :biggrin:  :skrewy:

deeR44

  • Member
  • **
  • Posts: 98
Re: Adding two numbers with MASM32 ?
« Reply #77 on: August 01, 2022, 03:11:25 PM »

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

HSE

  • Member
  • *****
  • Posts: 2200
  • AMD 7-32 / i3 10-64
Re: Adding two numbers with MASM32 ?
« Reply #78 on: August 01, 2022, 11:07:58 PM »
"Attachments and other options"
Equations in Assembly: SmplMath

deeR44

  • Member
  • **
  • Posts: 98
Re: Adding two numbers with MASM32 ?
« Reply #79 on: August 02, 2022, 02:52:06 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--

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

hutch--

  • Administrator
  • Member
  • ******
  • Posts: 9816
  • Mnemonic Driven API Grinder
    • The MASM32 SDK
Re: Adding two numbers with MASM32 ?
« Reply #81 on: August 02, 2022, 05:11:55 PM »
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
hutch at movsd dot com
http://www.masm32.com    :biggrin:  :skrewy:

deeR44

  • Member
  • **
  • Posts: 98
Re: Adding two numbers with MASM32 ?
« Reply #82 on: August 03, 2022, 02:00:08 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--

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

deeR44

  • Member
  • **
  • Posts: 98
Re: Adding two numbers with MASM32 ?
« Reply #84 on: August 03, 2022, 02:27:41 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

  • Member
  • ****
  • Posts: 726
Re: Adding two numbers with MASM32 ?
« Reply #85 on: August 03, 2022, 04:30:55 PM »
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.

hutch--

  • Administrator
  • Member
  • ******
  • Posts: 9816
  • Mnemonic Driven API Grinder
    • The MASM32 SDK
Re: Adding two numbers with MASM32 ?
« Reply #86 on: August 03, 2022, 05:40:38 PM »
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>

hutch at movsd dot com
http://www.masm32.com    :biggrin:  :skrewy:

jj2007

  • Member
  • *****
  • Posts: 13029
  • Assembler is fun ;-)
    • MasmBasic
Re: Adding two numbers with MASM32 ?
« Reply #87 on: August 03, 2022, 06:27:25 PM »
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.

Congrats, your first working MASM program :thumbsup:

NoCforMe

  • Member
  • ****
  • Posts: 726
Re: Adding two numbers with MASM32 ?
« Reply #88 on: August 03, 2022, 06:30:06 PM »
OK, if that really is your first MASM program, then mazeltov. It's a good feeling, no?

bomz

  • Member
  • **
  • Posts: 122
Re: Adding two numbers with MASM32 ?
« Reply #89 on: August 04, 2022, 12:57:03 AM »
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