This is the first time we've had to use the push function. In assembly there is a memory stack. We will push information onto the stack to be read. Later we can pop the information back off the memory stack but we don't need to worry about popping quite yet. For this tutorial we're only going to push.
Let's start out with our standard program.
The masm32rt include file is set up as a sort of standard library for assembly language programmers. Since assemblers do not have a library, masm32rt was made to try to make up for that. By including this we have access to all of the libraries, includes that we will need as well as the start of a program.
Now that we have that taken care of we're going to need to set up some values for the our messagebox title and message.
.data MyTitle db "ASM is Fun!",0 MyText db "I hope you're learning!",0
.data includes all initialized data. Since our data in this tutorial is not ever going to change values we could use .const instead of .data for it. There is also .data? which hold uninitialized data.
MyTitle and MyText are our string names. db stands for define byte. Our variables hold the text after db. These strings are then terminated by a null terminator character 0 or by using NULL. You can use either as they mean the same thing.
Now that we have that taken care of, we need to get to the actual program.
.code start: push 0 mov eax, offset MyTitle push eax push offset MyText push 0 call MessageBoxA call ExitProcess end start
Well that's a pretty big chunk to wade through. Lets take it a step at a time now.
We start out pushing 0 but before we get into that we need to understand the data stack.
The data stack operates on a last on first off principle. This means that the last thing pushed onto the stack is the first thing popped off the stack. Think of it as a stack of plates. The last one you put onto the stack of plates is the first one you take off the stack. The memory stack works the same way.
Our messagebox function goes like this:
MessageBoxA(Handle Window, Text, Title, Style)
Since we are pushing on the stack we have to work backwords so we will push in the opposite order:
Style, Title, Text, Handle Window
So first we need to push 0 which is the standard messagebox style consisting of an OK button onto the memory stack.
After that we move the offset (location) of our title into the eax register. The eax register is a general purpose register that we can use for virtually everything. We then push the register's value onto the stack.
After we have the style and the text we push the offset of the title and finally the handle window which is none or 0. Instead of moving the offset of the message into eax and pushing the register we just push the offset directly onto the stack. This is just for demonstration purposes. You can do it either way you like.
Now that we have everything for our messagebox on the stack, we need to call the messagebox method. That's what we do with our call MessageBoxA command. It reads the last four pieces of information on the stack and combines it into our messagebox.
Finally we call our ExitProcess method to end the program execution.
Our final code is
include \masm32\include\masm32rt.inc .data MyTitle db "ASM is Fun!",0 MyText db "I hope you're learning!",0 .code start: push 0 push offset MyTitle push offset MyText push 0 call MessageBoxA call ExitProcess end start
Well, that's it for this tutorial. Hopefully you've learned something new or understood something old in a better way.
Keep practicing and have fun!