Well some people say that using the invoke function and other macros are not ASM enough so lets make a hello world without them. We'll just use pushing instead.
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.
If this is your first experience with assembly, please visit this tutorial for very in-depth explaining of the program beginning.
Let's start out with our standard program.
Code:
.386
.model flat, stdcall
option casemap :none
include \masm32\include\kernel32.inc
includelib \masm32\lib\kernel32.lib
include \masm32\include\user32.inc
includelib \masm32\lib\user32.lib
Now that we have that taken care of we're going to need to set up some values for the messagebox style, the handle window, the text, and the title.
Code:
mb_ok equ 0
hWnd equ 0
.data
MyTitle db "Jumps are Fun!",0
MyText db "I hope you're learning!",0
mb_ok equ 0 will give our messagebox the style 0 which is a standard messagebox having only an OK button.
hWnd equ 0 will set the handle window. We don't have a window that loads the messagebox. Since we don't have a window we give it the value 0.
You already know what is happening in the data section from my last tutorial.
Now that we have that taken care of, we need to get to the actual program.
Code:
.code
start:
push mb_ok
push offset MyTitle
push offset MyText
push hWnd
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 mb_ok 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.
If you remember, 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 mb_ok which is the messagebox style onto the memory stack.
After that we push the text. We have to use the offset command to push the text's position in memory. We can't push a whole string onto the stack.
After we have the style and the text we push the offset or address of the title and finally the handle window which is none.
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.
Finally we call our ExitProcess method to end the program execution.
Our final code is
Code:
.386
.model flat, stdcall
option casemap :none
include \masm32\include\kernel32.inc
includelib \masm32\lib\kernel32.lib
include \masm32\include\user32.inc
includelib \masm32\lib\user32.lib
mb_ok equ 0
hWnd equ 0
.data
MyTitle db "Jumps are Fun!",0
MyText db "I hope you're learning!",0
.code
start:
push mb_ok
push offset MyTitle
push offset MyText
push hWnd
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!