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

sunshine33

Can somebody give me an example of adding two numbers with assembly language in MASM32 ?

#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

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

#2
deleted

jj2007

That's easy, in purest Masm:

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


First number:   123
Second number:  321
The sum of 123 plus 321 is 444


Perhaps you should show us what you have coded so far...?

nidud

#4
deleted

hutch--

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
                       "CONSOLE ASSEMBLE AND LINK"
        ----------------------------------------------------- *

    .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
    add eax, 5678               ; add integer to eax

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

    ret                         ; return to caller

main endp

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

end start

aw27

Quote
I gether the original poster wanted MASM, not C runtime or emulated basic

nidud

#7
deleted

hutch--

 :biggrin:

Have I missed something here ?

    mov eax, 1234               ; put integer into eax
    add eax, 5678               ; add integer to eax

Add two integers using a register ? Why the hell would you need MSVCRT to add 2 numbers.

jj2007

Quote from: hutch-- on August 03, 2018, 11:00:46 AM
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.

Quote from: jj2007 on August 03, 2018, 08:34:35 AMPerhaps you should show us what you have coded so far...?
That's why I posted "pure Masm" :lol:

zedd151

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)

     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


hutch--

 :biggrin:

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

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

    AddemUp PROTO STDCALL :DWORD, :DWORD

    .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

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

  ; -----------------
  ; call the add proc
  ; -----------------
    push num2
    push num1
    call AddemUp
    mov esi, eax

  ; -------------------------
  ; get output buffer address
  ; -------------------------
    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

    ret                         ; return to caller

main endp

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

OPTION PROLOGUE:NONE
OPTION EPILOGUE:NONE

AddemUp proc num1:DWORD,num2:DWORD

    mov eax, [esp+4]
    add eax, [esp+8]

    ret

AddemUp endp

OPTION PROLOGUE:PrologueDef
OPTION EPILOGUE:EpilogueDef

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

end start

aw27

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--

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.

jj2007

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.