Page 1 of 1

MASM Adding Numbers How to add numbers in MASM. Covers DUP and DWTOA commands

#1 HiddenDragon   User is offline

  • D.I.C Head
  • member icon

Reputation: 10
  • View blog
  • Posts: 70
  • Joined: 25-November 10

Posted 27 November 2010 - 05:31 PM

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 perentheses(), 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.

Is This A Good Question/Topic? 0
  • +

Page 1 of 1