Remove Ads

Share on Facebook Share on Twitter

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tutorial] MASM Hello World
#1
This tutorial will cover pushing, strings, moving, registers, and messageboxes. Since I don't really like using the macros as much as pure mnemonics that's what we'll use.

This is the first time we've had to use the push function. In assembly there is a memory stack. We will [i]push[/i] information onto the stack to be read. Later we can [i]pop[/i] 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.

[code]include \masm32\include\masm32rt.inc[/code]

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.

[code]
.data
MyTitle db "ASM is Fun!",0
MyText db "I hope you're learning!",0[/code]

.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].code
start:
push 0
mov eax, offset MyTitle
push eax
push offset MyText
push 0
call MessageBoxA
call ExitProcess
end start[/code]

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

[code]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[/code]


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!
Reply
#2
Thanks for this - I've been meaning to take a look at x86 ASM.
Reply
#3
Interesting tutorial, thank you.
Reply
#4
Thanks. 32 bit assembly is difficult to learn but well worth it once you get into it. Your programs will be smaller (file size) and faster.
Reply
#5
Also almost forgot to say it, feel free to add an avatar if you wish, you'll find it at User CP on the left (just in case if you don't know already).
Reply
#6
I actually forgot about adding one. I'll go ahead and do that now.
Reply
#7
another way in masm is to call the api, for future reference.

[code].386
.model flat,stdcall
option casemap:none
include \masm32\include\windows.inc
include \masm32\include\kernel32.inc
includelib \masm32\lib\kernel32.lib
include \masm32\include\user32.inc
includelib \masm32\lib\user32.lib

.data
MsgBoxCaption db "codershed.us",0
MsgBoxText db "Hello World",0

.code
start:
invoke MessageBox, NULL, addr MsgBoxText, addr MsgBoxCaption, MB_OK
invoke ExitProcess, NULL
end start [/code]

i am learning myself..
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)