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

avcaballero

Here another tiny version. The key is that for sum two numbers in asm you need use the registers:

Something like this:

mov  eax, [number1]
add  eax, [number2]


What you can't do is (I guess this is your question in fact):

add  [number1], [number2]


Here is my example and its result:

.386
.MODEL     flat, stdcall
OPTION     casemap :none
         
INCLUDE    windows.inc
INCLUDE    kernel32.inc
INCLUDE    masm32.inc
         
INCLUDELIB kernel32.lib
INCLUDELIB masm32.lib
 
.DATA
  number1   DD 442
  number2   dd 22
  msg       db 11 dup(0)   ; 11 for sum 10 to its begining (1 zero at the end)

.CODE
  num2str proc
    ; In: eax, number value
    ; Out: eax, offset of value in string format
    mov     esi, offset msg + 10
    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

start:
  mov        eax, [number1]
  add        eax, [number2]
  call       num2str
  invoke     StdOut, eax
  INVOKE     ExitProcess, 0

END start

> SumaWM01
Result: 464


I attach it also.

nidud

#16
deleted

hutch--

Win10 64 bit has all sorts of unusual connections so I would not be surprised what calls what. If a system DLL calls MSVCRT you are not directly dependent as it is an OS based connection that was not in all earlier versions and may not be in future versions. As it is part of the OS, it can be called directly and it has been so from later version of Win95 onwards.

aw27

Quote from: hutch-- on August 03, 2018, 10:00:20 PM
Win10 64 bit has all sorts of unusual connections so I would not be surprised what calls what. If a system DLL calls MSVCRT you are not directly dependent as it is an OS based connection that was not in all earlier versions and may not be in future versions. As it is part of the OS, it can be called directly and it has been so from later version of Win95 onwards.

MSVCRT.DLL is considerd a OS DLL - user applications are not expected to use it.
User applications are expected to use the MSVCRXX.DLLs from VC++ redistributables, recently UCRTBASE.DLL and other confusing DLLs.

However, MSVCRT.DLL is used internally by the OS and also by devices drivers developers - it would a general debacle if it is removed.
All right, MASM32 users come to use it as well  :t

jj2007

Raymond Chen:
QuoteAlthough MSVCRT.DLL has been an operating system DLL for a long time, and has been documented as off-limits to applications, there are still a lot of people who treat it as a C runtime delivery channel, and those programs create a lot of grief for the product team.

I love that: creates a lot of grief. That's exactly what I wish to the team - a small revenge for all the Windows bugs :P

Seriously: Of course, msvcrt.dll works just fine. What they really want is that we create applications that run only with the latest Windows versions. That's the most prominent reason for the msvcrXX DLL hell 8)

sunshine33

Thanks a lot for all the replies

I was looking for a program that asks me to enter numbers to add like this ,





So which one of the above is the most appropriate MASM32 Assembly language code that does this ?

hutch--

Jose,

As MSVCRT has been there for over 20 years, I have not seen a reason not to use parts of it. Much of it is not a lot of use to assembler programmers, the OS APIs can do an enormous number of tasks but if you want a function that it has, its cheap and easy to use and extends the range of what you can do for very little effort. It has been used in MASM32 since the late 90s and it works fine in 64 bit MASM as well.

As its a core DLL over a long time that I doubt they would try and change, using it makes sense, you can avoid the version specific addons to it that cause so much grief with dependent higher level languages by using the old and well tested version.

hutch--

sunshine,

While various member were happy enough to try and help you out, you will have to write your own code. Have a look through the examples and see what best suits what you are after then write the code you want, no one here will do it for you.

sunshine33

Sorry about some basic questions again , I have only learned c in college

I am only beginning to learn Assembly language

I had to research a bit of assembly on Linux , MS DOS before i finally made up my mind to learn Assembly in MASM32 .



C code to enter two numbers and Add .


#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;
}


OK , i will try to find the appropriate examples from above myself

The include files in MASM32 can be a bit confusing for a beginner like me .

But , Thanks for all the support and examples

sunshine33

I wish i could seriously get a clearer picture of this code in Assembly language for a start ,

int firstNumber, secondNumber, sumOfTwoNumbers;
   
    printf("Enter two integers: ");

    // Two integers entered by user is stored using scanf() function
    scanf("%d %d", &firstNumber, &secondNumber);


How do you do this in assembly language ?

Quoteprintf("Enter two integers: ");






sunshine33

My main problem right now is , i am not sure about the differences of these right now .

C runtime , emulated basic and MASM32

?

hutch--

The rough distinction is that the C language has a number of callable libraries as well as DLLs which generally have overlapping functions. You use them in C at runtime thus C runtime libraries.

One of our members, JJ has designed an emulation of BASIC written in MASM thus emulated basic.

MASM is a direct assembler and the MASM32 SDK extends that with libraries and macros to help introduce people to assembler programming. The real action in MASM is coding in assembler instructions but as that is complicated to start with, the libraries and macros make it a lot easier to start with and with practice a lot faster to code.

sunshine33

Thanks for the reply

By nidud ,

; adding.asm
; assemble with: Asmc -pe adding.asm
;
include conio.inc

    .code

main proc

    local firstNumber, secondNumber, sumOfTwoNumbers;
   
    _cputs("Enter two integers: ")

    ;; Two integers entered by user is stored using scanf() function
    _cscanf("%d %d", &firstNumber, &secondNumber)

    ;; sum of two numbers in stored in variable sumOfTwoNumbers
    mov eax,firstNumber
    add eax,secondNumber
    mov sumOfTwoNumbers,eax

    ;; Displays sum
    _cprintf("\r\n%d + %d = %d\r\n", firstNumber, secondNumber, sumOfTwoNumbers)

    xor eax,eax
    ret

main endp

    end main


; adding.asm
; assemble with: Asmc -pe adding.asm
;
    .486
    .model flat, c

    option dllimport:<msvcrt>
    printf proto :ptr, :vararg
    scanf  proto :ptr, :vararg

    .code

main proc

    local 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
    mov eax,firstNumber
    add eax,secondNumber
    mov sumOfTwoNumbers,eax

    ;; Displays sum
    printf("%d + %d = %d\n", firstNumber, secondNumber, sumOfTwoNumbers)

    xor eax,eax
    ret

main endp

    end main


By jj2007 ,

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



By hutch--

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



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


By zedd151 ,

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



nidud's example is using C run time library , That one looks a bit easy to read and understand .
JJ2007's example is an emulation of basic written in MASM , can understand but still looks complicated
zedd151 's example is a bit hard to understand too at this level of understanding ,
hutch--'s example is MASM  , looks the hardest to me ,

Only in the nidud's example the user is asked to enter numbers ?





dedndave

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

in the earlier replies, they have used macros, subroutines, or libraries to accomplish this
it is a little bit involved to understand the inner workings of how this is accomplished under win-32
it is basically a two-part process:
1 - get the ASCII numeric string from the user
    (what happens if the user enters a non-numeric value, or a value that is too large?)
2 - convert the value entered (an ASCII string) into a binary number to work with
    (do we want to support real numbers or just integers - signed or unsigned?)

you can look at the code for the "val" macro" to see the guts of the code  :t

once you have the two values...
    mov     eax,dwValue1
    add     eax,dwValue2            ;EAX = dwValue1 + dwValue2


now, it is time to convert the value to an ASCII numeric string and display it
again, a two-part process - and, again, you can examine the code to see how it works

the macro code may be found in \Masm32\Macros\Macros.asm
the masm32 library source code for individual modules may be found in \Masm32\M32Lib

aw27

sunshine,

If this is for a school assignment, you must follow your teacher guidelines.
Most schools follow the Irvine book and its way of doing things. Although there is much room for criticism against it and its method of doing things it really has a comprehensive SDK with ready made library routines for a lot of things including inputing integers or floats from the command line without using the C runtime.
In other words, if your teacher follows the Irvine book odds are that you will have to follow it.  :(