The MASM Forum Archive 2004 to 2012

General Forums => The Campus => Topic started by: HiddenDragon on November 27, 2010, 06:29:09 PM

Title: Adding Numbers
Post by: HiddenDragon on November 27, 2010, 06:29:09 PM
Alright well I have no idea how to add. For example, if I want to add 10 and 12 then display the result in a messagebox, how would I do that? Would I move 10 into one register and 12 into another register then add the two? What register would the result be displayed in? All I've got is
mov eax, 10
mov ebx, 12
add eax, ebx


Thanks.
Title: Re: Adding Numbers
Post by: brethren on November 27, 2010, 06:35:39 PM
thats right:)
mov eax, 10  ;eax=10
mov ebx, 12  ;ebx=12
add eax,ebx  ;eax=eax+ebx (eax=10+12)

download masm32 for input/output functions
Title: Re: Adding Numbers
Post by: Glenn9999 on November 27, 2010, 06:48:54 PM
Quote from: HiddenDragon on November 27, 2010, 06:29:09 PM
Would I move 10 into one register and 12 into another register then add the two? What register would the result be displayed in? All I've got is
mov eax, 10
mov ebx, 12
add eax, ebx


Or even better with constants you can apply them to the add instruction (or any other, usually).  One less instruction and a little less confusing. :)


mov eax, 10
add eax, 12
Title: Re: Adding Numbers
Post by: HiddenDragon on November 27, 2010, 06:56:40 PM
But where is the answer located? A separate register? Which one?
Title: Re: Adding Numbers
Post by: dedndave on November 27, 2010, 06:56:48 PM
there are many ways to do this
but, if you want to write GUI mode programs before you understand how to add 2 values, you have the cart in front of the horse   :P

as brethren says, download and install the masm32 package

http://masm32.com/index.htm

here is a good reference by Randy Hyde that explains data types and instructions
there are also many examples and references in the masm32\examples and masm32\help folders

http://www.arl.wustl.edu/~lockwood/class/cs306/books/artofasm/toc.html

to learn windows GUI mode programming, Iczelion's tutorials are good

http://website.masm32.com/

once you have the masm32 package installed, use BUILD.BAT to assemble...
       INCLUDE \masm32\include\masm32rt.inc

       .DATA

AnswerTxt db 'The Answer is '
AnswerNum db 11 dup(0)

       .CODE

Start:
       mov     eax,10
       add     eax,12
       INVOKE  dwtoa,eax,offset AnswerNum
       INVOKE  MessageBoxA,NULL,offset AnswerTxt,offset AnswerTxt,NULL
       INVOKE  ExitProcess,0

       END     Start


program with source attached...
Title: Re: Adding Numbers
Post by: HiddenDragon on November 27, 2010, 07:02:52 PM
Thanks dedndave. I haven't gotten into GUI stuff yet. I've got masm installed already too. I have that 2nd link as an ebook that I'm working my way through. Slow but steady. Thanks for the help.
Title: Re: Adding Numbers
Post by: Vortex on November 27, 2010, 07:04:24 PM
Another version :


.386
.model flat,stdcall
option casemap:none

include     \masm32\include\windows.inc
include     \masm32\include\kernel32.inc
include     \masm32\include\user32.inc

includelib  \masm32\lib\kernel32.lib
includelib  \masm32\lib\user32.lib

.data

template    db "Sum = %d",0
capt        db "Calculation",0

.data?

buffer      db 32 dup(?)

.code

start:

    mov     eax,10
    add     eax,12

    invoke  wsprintf,ADDR buffer,ADDR template,eax
    invoke  MessageBox,0,ADDR buffer,ADDR capt,MB_OK

    invoke  ExitProcess,0

END start

Title: Re: Adding Numbers
Post by: dedndave on November 27, 2010, 07:07:11 PM
hiyas Erol
mine is shorter   :lol
Title: Re: Adding Numbers
Post by: Glenn9999 on November 27, 2010, 07:11:37 PM
Quote from: HiddenDragon on November 27, 2010, 06:56:40 PM
But where is the answer located? A separate register? Which one?

If it helps, the way I think of it is that the register on the left side is always what is operated on, and where the answer is.  For example, what I posted has the final answer in EAX.


mov eax, 10             // EAX := 10;   move 10 to EAX
add eax, 12              // EAX := EAX + 12;  add 12 to the value in EAX



Title: Re: Adding Numbers
Post by: HiddenDragon on November 27, 2010, 07:25:06 PM
So see if I have this right:

include \masm32\include\masm32rt.inc

.data

AnswerTxt db 'The Answer is '
AnswerNum db 11 dup(0)

.code

start:  mov  eax,10
        add  eax,12
        push offset AnswerNum
        push eax
        call dwtoa
        push 0
        push offset AnswerTxt
        push offset AnswerTxt
        push 0
        call MessageBoxA
        call ExitProcess
end start


dwtoa will convert a doubleword to an ascii string right? So in this I will add 10 and 12 then take that answer and convert dword to string and store that in the AnswerNum? AnswerNum is a part of AnswerTxt?

I feel pretty dumb now.
Title: Re: Adding Numbers
Post by: dedndave on November 27, 2010, 07:28:21 PM
Quotedwtoa will convert a doubleword to an ascii string right? So in this I will add 10 and 12 then take that
answer and convert dword to string and store that in the AnswerNum? AnswerNum is a part of AnswerTxt?

that is correct
both Erol (Vortex) and i attached working programs underneath our replies

no need to feel dumb - we all started someplace
Title: Re: Adding Numbers
Post by: RuiLoureiro on November 27, 2010, 07:32:21 PM
Hi HiddenDragon,

             If you want to add 2 signed/unsigned integers like 10 and 12
             this is the correct way
             
             mov    eax, 10
             mov    ebx, 12
             add    eax, ebx

             The result 22 is in the register eax, in this case.
             Now you need to convert 22 into ascii characters and put
             the result into a string like AnswerNum (see dedndave).
             
             To do this you can use the masm procedure dwtoa

             INVOKE  dwtoa, eax, offset AnswerNum

.data
AnswerNum   db 11 dup(0)

              If you have the string AnswerNum than you can show the result
              using MessageBox;

            Another thing is this: If you type strings Num1 and Num2 like

StrNum1     db "10", 0
StrNum2     db "12", 0

            first you need to convert StrNum1 to 10 and StrNum2 to 12 and than
            use add like above. To add 2 signed integers you have more problems to
            solve

«dwtoa will convert a doubleword to an ascii string right? » ------ YES
«AnswerNum is a part of AnswerTxt?» ----- YES
because it was defined one after another
AnswerTxt db 'The Answer is '
AnswerNum db 11 dup(0)

RuiLoureiro
Title: Re: Adding Numbers
Post by: HiddenDragon on November 27, 2010, 07:35:09 PM
Thanks.  I've gotten them working fine I'm just trying to actually understand what's going on there.

Can you explain the
AnswerTxt db 'The Answer is '
AnswerNum db 11 dup(0)

a bit?

I think that you've got the string for AnswerTxt that includes AnswerNum in it. I looked up the dup command and it's duplicate? Why are you duplicating 11 bytes with a 0 in them? What's the difference between using 'The Answer is ' and "The Answer is "?
Title: Re: Adding Numbers
Post by: dedndave on November 27, 2010, 07:39:06 PM
you have the idea

the 11 duplicated 0's are just an empty buffer that gets filled by dwtoa
as you said, dwtoa takes the binary dword value and converts it to an ASCII decimal string
in windows, most strings are terminated with a 0 (often called a null)
the answer could have 10 digits if you changed the values, so i made the buffer 11 bytes long
a bit of a mistake, as it could have a minus sign, as well - i should have used 12 dup(0)

there is no real difference between 'The Answer is ' and "The Answer is "
however, if i want to use double quotes inside the text, it needs to be delimited by single quotes and visa versa
you are more likely to use double quotes in text...
...and, it's easier to type (no shift key)   :P
Title: Re: Adding Numbers
Post by: HiddenDragon on November 27, 2010, 07:41:48 PM
Thanks, I think I understand now.

Off topic: How long have you been doing ASM?
Title: Re: Adding Numbers
Post by: dedndave on November 27, 2010, 07:48:31 PM
i wrote my first assembly programs in 1976   :bg  (actually, it was machine language to be more correct)
they were mostly small pieces of test code used to help troubleshoot smart terminals that used the 4004 and 8008
Title: Re: Adding Numbers
Post by: RuiLoureiro on November 27, 2010, 07:53:57 PM
Quote from: dedndave on November 27, 2010, 07:48:31 PM
i wrote my first assembly programs in 1976   :bg  (actually, it was machine language to be more correct)
they were mostly small pieces of test code used to help troubleshoot smart terminals that used the 4004 and 8008
                i wrote (asm) some years later ...dedndave  :bg
Title: Re: Adding Numbers
Post by: dedndave on November 27, 2010, 08:00:38 PM
well - nearly all members of the forum are more experienced than i am with 32-bit code - and with windows GUI   :P
i still tend to write some things as though it was for an 8088 DOS machine - lol
but, i am learning