Computer Tech

Full Version: [Tutorial] MASM Adding Numbers (DUP, ADD, DWTOA)
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Well this tutorial over adding numbers is going to cover a couple of new topics. These are DUP, ADD, and DWTOA. Let's start with a standard program.

include \masm32\include\


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

Now in our data section we have our answer. AnswerNum is a part of AnswerTxt and we will use AnswerTxt to display our answer when we add. Now the DUP command stands for duplicate. We are duplicating 11 bytes. In the parentheses(), we hold the value we are going to put into those bytes. We do this duplication of 11 bytes filling them with zeros because they are going to be used later on in our program.

start:   mov  eax,14
         add  eax,27
         push offset AnswerNum
         push eax
         call dwtoa

Alright so once we start our program we are moving the value 14 into our eax general purpose register. After that we are adding the value in eax and 27. This answer is stored in eax as it is the first register listed before the comma (,).

We then push AnswerNum's location onto the stack to be read. After that we push our eax register that now contains 41. Then we call dwtoa.

dwtoa converts DoubleWord TO Ascii. With this we can take a numeric value and convert it to an ascii string, then store it somewhere. We take the value at eax (41) and convert it to a string to be stored at AnswerNum.

push 0
     push offset AnswerTxt
     push offset AnswerTxt
     push 0
     call MessageBoxA
     call ExitProcess
end start

Next we push onto the stack everything that we need for a messagebox. In order, we push 0 for our messagebox style. 0 will give us a messagebox with just an ok button. Then we push our AnswerTxt twice. Once for the title, then again for the text of the messagebox. Finally, we push the handle window. Since we don't have a handle window we just put 0. After we have all the 'ingredients' for the messagebox, we call the messagebox command. Then we call ExitProcess to end execution of our program.

That's it for this tutorial. Hopefully you'll be able to add numbers now. I hope you've learned something.
I fail at assembly and I understood that, so good job. I would rep if I could find the button.
You have to click on the user and scroll down a little.

Glad you understood it, that's the whole goal of having a tutorial Smile
Woah. I looked in mod CP and it says your default user title is Moderator but your primary group is registered. You're both at the same time apparently. Open a topic in bug reports.
I PMed Ironside an hour ago. I'll wait for his response.
Reference URL's