x86 Assembler using MASM32 - MessageBox & Hello World



Watch the video or follow the tutorial.



Here we have tutorial 4 where we will create our first application in x86 assembler, the Hello World application. We will create this app using the windows API function MessageBox, it will be brief, quick and clean so let's get going. First of all open Visual MASM, if you haven't installed Visual MASM yet you can learn how to do it here. Once installed open it and learn create the default code template needed for all my tutorials here. This default code will be the starting point for most of our tutorials including this one. So, once all this is done you should be looking at the code window as seen below:


Scroll down until you see the .data section and add the following code:

strTitle DB "Message", 0
strMessage DB "Hello World", 0


Then scroll down until you see the .code section and underneath the label start: add the following code:

Push MB_OK
Push Offset strTitle
Push Offset strMessage
Push 0
Call MessageBox

I prefer not to use the Invoke keyword when calling functions, instead I like to use Call. Because of this I have to Push the function parameters onto the stack in reverse order manually and then call the function itself using the Call keyword. If you wish to use Invoke instead of call then that is fine it is simply a matter of personal preference but since I do a lot of disassembly I like to have my assembler code look as close to my disassembly code as possible, therefore I use Call and not Invoke.


The code is now complete, here is the full code:

; *************************************************************************
; 32-bit Windows Program
; *************************************************************************

.686                                      ; Enable 80686+ instruction set
.model flat, stdcall                ; Flat, 32-bit memory model (not used in 64-bit)
option casemap: none         ; Case sensitive syntax

; *************************************************************************
; MASM32 proto types for Win32 functions and structures
; *************************************************************************
include c:\masm32\include\windows.inc
include c:\masm32\include\user32.inc
include c:\masm32\include\kernel32.inc
include c:\masm32\include\masm32rt.inc     ; for using ustr$() and such like

; *************************************************************************
; MASM32 object libraries
; *************************************************************************
includelib c:\masm32\lib\user32.lib
includelib c:\masm32\lib\kernel32.lib

; *************************************************************************
; Our data section.
; *************************************************************************
.data

strTitle DB "Message", 0
strMessage DB "Hello World", 0

; *************************************************************************
; Our unintialised data section.
; *************************************************************************
.data?



; *************************************************************************
; Our constant section.
; *************************************************************************
.const



; *************************************************************************
; Macros
; *************************************************************************



; *************************************************************************
; Our executable assembly code starts here in the .code section
; *************************************************************************
.code

start:

     Push MB_OK
     Push Offset strTitle
     Push Offset strMessage
     Push 0
     Call MessageBox

     ; Exit app
     Push 0
     Call ExitProcess

end start


Press F9, the code will link and build the exe and once complete it will run it.


You may have to save the project files before you can build the app. Choose a location and press save, you'll have to save each project file independently. Once the files are saved the app will run.



As you can see it worked and the app displayed a messagebox with the text Message in the title bar and Hello World as the main message. Congratulations you have just created your first assembler app. For your information, if you search the web for the function name MessageBox it should bring up the MSDN page for that function as one of the first results. Here you can find alternative options for the buttons and such like. This will also work for any windows API and you should experiment by changing the available options and seeing what happens.

Anyway in the next tutorial we will have a look at the IF statement in assembler so until then, enjoy.

Link to a text file with complete source code and more comments - here.