Post Reply 
Social Buttons
 
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tutorial] MASM Hello World
Author Message
Hidden Dragon Offline
Amazing
*******

Trade Count: (0)
Posts: 63
Joined: Jan 2011
Reputation: 4
Post: #1
[Tutorial] MASM Hello World
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 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.

Code:
include \masm32\include\masm32rt.inc

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

.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

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


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!
(This post was last modified: 01-15-2011 07:34 PM by Hidden Dragon.)
01-15-2011 07:32 PM
Find all posts by this user Quote this message in a reply
Ethereal Offline
Moderator
*******

Trade Count: (0)
Posts: 39
Joined: Jan 2011
Reputation: 5
Post: #2
RE: [Tutorial] MASM Hello World
Thanks for this - I've been meaning to take a look at x86 ASM.
01-15-2011 07:34 PM
Find all posts by this user Quote this message in a reply
Ironside Offline
Site Owner
**********

Trade Count: (0)
Posts: 170
Joined: Nov 2010
Reputation: 7
Post: #3
RE: [Tutorial] MASM Hello World
Interesting tutorial, thank you.
01-15-2011 07:34 PM
Find all posts by this user Quote this message in a reply
Hidden Dragon Offline
Amazing
*******

Trade Count: (0)
Posts: 63
Joined: Jan 2011
Reputation: 4
Post: #4
RE: [Tutorial] MASM Hello World
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.
01-15-2011 07:38 PM
Find all posts by this user Quote this message in a reply
Ironside Offline
Site Owner
**********

Trade Count: (0)
Posts: 170
Joined: Nov 2010
Reputation: 7
Post: #5
RE: [Tutorial] MASM Hello World
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).
(This post was last modified: 01-15-2011 07:45 PM by Ironside.)
01-15-2011 07:45 PM
Find all posts by this user Quote this message in a reply
Hidden Dragon Offline
Amazing
*******

Trade Count: (0)
Posts: 63
Joined: Jan 2011
Reputation: 4
Post: #6
RE: [Tutorial] MASM Hello World
I actually forgot about adding one. I'll go ahead and do that now.
01-15-2011 07:47 PM
Find all posts by this user Quote this message in a reply
codershed Offline
New Member
**

Trade Count: (0)
Posts: 1
Joined: Jul 2011
Reputation: 0
Post: #7
RE: [Tutorial] MASM Hello World
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

i am learning myself..
07-28-2011 12:24 AM
Find all posts by this user Quote this message in a reply
Post Reply 


Forum Jump:


User(s) browsing this thread: