News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

Adding Numbers

Started by HiddenDragon, November 27, 2010, 06:29:09 PM

Previous topic - Next topic

HiddenDragon

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.

brethren

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

Glenn9999

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

HiddenDragon

But where is the answer located? A separate register? Which one?

dedndave

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

HiddenDragon

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.

Vortex

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


dedndave

hiyas Erol
mine is shorter   :lol

Glenn9999

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




HiddenDragon

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.

dedndave

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

RuiLoureiro

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

HiddenDragon

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

dedndave

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

HiddenDragon

Thanks, I think I understand now.

Off topic: How long have you been doing ASM?