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

#### sunshine33

• Regular Member
• Posts: 46
• MASM32
##### Adding two numbers with MASM32 ?
« on: August 03, 2018, 08:01:30 AM »
Can somebody give me an example of adding two numbers with assembly language in MASM32 ?

Code: [Select]
`#include <stdio.h>int main(){    int firstNumber, secondNumber, sumOfTwoNumbers;        printf("Enter two integers: ");    // Two integers entered by user is stored using scanf() function    scanf("%d %d", &firstNumber, &secondNumber);    // sum of two numbers in stored in variable sumOfTwoNumbers    sumOfTwoNumbers = firstNumber + secondNumber;    // Displays sum          printf("%d + %d = %d", firstNumber, secondNumber, sumOfTwoNumbers);    return 0;}`

#### RuiLoureiro

• Member
• Posts: 820
##### Re: Adding two numbers with MASM32 ?
« Reply #1 on: August 03, 2018, 08:15:13 AM »
Hi sunshine33,
As far as i understood, you need to write a procedure to get number 1 and number 2 that we type. This procedure must convert each number to INTEGER32, REAL4, REAL8 or REAL10
format. I dont know what you want to do. After it we may sum it.
If integer32, we use add, if real we may use FPU  fadd. To convert a string to real number format you may get a converter that i wrote. It is here elsewhere. Search.
After having the sum we need to use a converter real to string to print the result.
Good luck

#### nidud

• Member
• Posts: 2388
##### Re: Adding two numbers with MASM32 ?
« Reply #2 on: August 03, 2018, 08:26:04 AM »
deleted
« Last Edit: February 25, 2022, 01:13:32 AM by nidud »

#### jj2007

• Member
• Posts: 13299
• Assembly is fun ;-)
##### Re: Adding two numbers with MASM32 ?
« Reply #3 on: August 03, 2018, 08:34:35 AM »
That's easy, in purest Masm:

SetGlobals firstNumber, secondNumber, sumOfTwoNumbers
Init
mov firstNumber, Val(Input\$("First number:\t", "123"))
mov secondNumber, Val(Input\$("Second number:\t", "321"))
mov sumOfTwoNumbers, eax
Inkey Str\$("The sum of %i", firstNumber), Str\$(" plus %i", secondNumber), Str\$(" is %i", sumOfTwoNumbers)
EndOfCode

Code: [Select]
`First number:   123Second number:  321The sum of 123 plus 321 is 444`
Perhaps you should show us what you have coded so far...?

#### nidud

• Member
• Posts: 2388
##### Re: Adding two numbers with MASM32 ?
« Reply #4 on: August 03, 2018, 09:03:11 AM »
deleted
« Last Edit: February 25, 2022, 01:13:47 AM by nidud »

#### hutch--

• Member
• Posts: 10005
• Mnemonic Driven API Grinder
##### Re: Adding two numbers with MASM32 ?
« Reply #5 on: August 03, 2018, 11:00:46 AM »
I gether the original poster wanted MASM, not C runtime or emulated basic, here is a very simple integer ADD in MASM.

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

comment * -----------------------------------------------------
Build this  template with
----------------------------------------------------- *

.data?
value dd ?

.data
item dd 0

.code

start:

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

call main
inkey                       ; pause so you can see the result
exit

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

main proc

mov eax, 1234               ; put integer into eax

print str\$(eax),13,10       ; display the result

main endp

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

end start
hutch at movsd dot com
http://www.masm32.com

#### aw27

• Guest
##### Re: Adding two numbers with MASM32 ?
« Reply #6 on: August 03, 2018, 02:06:53 PM »
Quote
I gether the original poster wanted MASM, not C runtime or emulated basic

#### nidud

• Member
• Posts: 2388
##### Re: Adding two numbers with MASM32 ?
« Reply #7 on: August 03, 2018, 03:36:58 PM »
deleted
« Last Edit: February 25, 2022, 01:14:02 AM by nidud »

#### hutch--

• Member
• Posts: 10005
• Mnemonic Driven API Grinder
##### Re: Adding two numbers with MASM32 ?
« Reply #8 on: August 03, 2018, 05:35:25 PM »

Have I missed something here ?

mov eax, 1234               ; put integer into eax

Add two integers using a register ? Why the hell would you need MSVCRT to add 2 numbers.
hutch at movsd dot com
http://www.masm32.com

#### jj2007

• Member
• Posts: 13299
• Assembly is fun ;-)
##### Re: Adding two numbers with MASM32 ?
« Reply #9 on: August 03, 2018, 05:39:22 PM »
I gether the original poster wanted MASM, not C runtime or emulated basic

inkey                       ; pause so you can see the result
exit

print str\$(eax),13,10       ; display the result

Right! He also wanted to read in a string from the console, and convert it to numbers.

Perhaps you should show us what you have coded so far...?
That's why I posted "pure Masm" :lol:

#### zedd151

• Member
• Posts: 1595
##### Re: Adding two numbers with MASM32 ?
« Reply #10 on: August 03, 2018, 06:07:08 PM »
I might as well post too.    :P

Ascii Adder. Directly adds two ascii integers, no matter the length (size) of the integers, well tested in a fibonacci generator - so that the limit is known to be far beyond capability of simple 32 or 64 bit registers. (and most calculators.)

Integer 1 (src1), integer 2 (src2), destination(dst) all have seperate buffers.  credit due to jimg for showing a better way to implement what I was originally trying.   8)
Code: [Select]
`     ascii_adder proc src1:dword, src2:dword, dst:dword, lent:dword ;(src1, src2, dst are pointers to already allocated memory. lent is the length of the largest value to be summed)        local carrie:dword            push esi            push edi            push ebx            mov esi, src1            mov edi, src2            mov ebx, dst            mov ecx, lent            mov carrie, 0            top:                mov eax,0                mov al,byte ptr [esi+ecx-1]                mov dl,byte ptr [edi+ecx-1]                add al,dl                sub al, 30h                add eax, carrie                mov carrie, 0                cmp al, 39h                                jbe @f                    mov carrie, 1                    sub al, 10                @@:                                mov [ebx+ecx-1], al                dec ecx                cmp ecx, 0            jnz top            pop ebx            pop edi            pop esi            ret        ascii_adder endp`
Nugget and Gypsy Youtube channel. New videos twice a week or so. If you like the videos, please subscribe and comment.

#### hutch--

• Member
• Posts: 10005
• Mnemonic Driven API Grinder
##### Re: Adding two numbers with MASM32 ?
« Reply #11 on: August 03, 2018, 06:10:59 PM »

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

comment * -----------------------------------------------------
Build this  template with
----------------------------------------------------- *

.code

start:

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

call main
inkey                       ; pause so you can see the result
exit

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

main proc

LOCAL num1  :DWORD
LOCAL num2  :DWORD
LOCAL pbuf  :DWORD
LOCAL buff[128]:BYTE

push esi

; -----------
; -----------
mov num1, 1234
mov num2, 5678

; -----------------
; -----------------
push num2
push num1
mov esi, eax

; -------------------------
; -------------------------
lea eax, buff
mov pbuf, eax

; --------------
; display result
; --------------
print cat\$(pbuf,"The sum of ",str\$(num1)," plus ",str\$(num2)," = ",str\$(esi),chr\$(13,10))

pop esi

main endp

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

OPTION PROLOGUE:NONE
OPTION EPILOGUE:NONE

mov eax, [esp+4]

ret

OPTION PROLOGUE:PrologueDef
OPTION EPILOGUE:EpilogueDef

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

end start
hutch at movsd dot com
http://www.masm32.com

#### aw27

• Guest
##### Re: Adding two numbers with MASM32 ?
« Reply #12 on: August 03, 2018, 06:29:35 PM »
I was not thinking about msvcrt.dll, it is normally linked - like it or not. Even hutch's examples here will link with msvcrt.dll, even though nothing would make us expect that. It is easy to see that with dependency walker or another similar tool.
It is possible to make an .exe without linking to msvcrt.dll, although it will not make anything useful.

#### hutch--

• Member
• Posts: 10005
• Mnemonic Driven API Grinder
##### Re: Adding two numbers with MASM32 ?
« Reply #13 on: August 03, 2018, 06:43:23 PM »
This is all it gets from MSVCRT.

Imp Addr Hint Import Name from msvcrt.dll - Not Bound
-------- ---- ---------------------------------------------------------------
00002018  111 _kbhit
0000201C   CE _getch

Because it is such an uncritical operation, dumping it off to MSVCRT makes sense.
hutch at movsd dot com
http://www.masm32.com

#### jj2007

• Member
• Posts: 13299
• Assembly is fun ;-)
##### Re: Adding two numbers with MASM32 ?
« Reply #14 on: August 03, 2018, 07:30:47 PM »
Attached the exe of my snippet above. With PEview, I can see Kernel, user, gdi, shell, comdlg, ole32 but no msvcrt.
However, José is right: Dependency walker shows that some of the OS dlls rely on msvcrt.