x86 Assembler using MASM32 - Default Code Template



Watch the video or follow the tutorial.



If you haven't already installed VIsual MASM and the MASM32 SDK which we will be using for all of our assembler coding then please have a look at my first tutorial which will show you where to get them and how to install them. Once complete then you can do this tutorial.

In this tutorial we will create the default code template for all our programming that we will do in Visual MASM and we will save it to the Visual MASM program folder so that it is available to us through the program itself by clicking "Create New Application".

So, first I will simply give you the full code that will be used in the template and then I will explain what each section does and how we will use it as we move forward and then I will show you what to do with the finished code. So first of all here is the 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



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



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



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



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

start:

    Push 0
    Call ExitProcess

end start


The above code is the default code that we will be using for all of our assembler programs and before we save it to the VisualMASM program files folder I will give you a brief explanation of hat each line of code means:

; *************************************************************************
; 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


The semi-colon is a marker that tells the compiler that the rest of this line is a comment and should not be used in compilation as part of the code. Comments are ignored by the compiler. .686 tells the compiler which CPU instruction set to use and in the case it is the 686 instruction set from the x86 family of Intel CPUs. The .model flat, stdcall tells the compiler that our memory will not be segmented as it is in 16-bit assembler but instead will be flat as it is in all 32-bit machines. The stdcall refers to the x86 function calling convention and in particular says that the in calling convention in use here is a variation on the Pascal calling convention in which the callee is responsible for cleaning up the stack, but the parameters are pushed onto the stack in right-to-left order, as in the _cdecl calling convention. Registers EAX, ECX, and EDX are designated for use within the function. Return values are stored in the EAX register.

option casemap: none means that small letters and their corresponding capital letters like a and A will not be mapped onto each other to be equivalent and so our code is case sensitive, meaning that a variable name strTitle is a different variable from Strtitle.

; *************************************************************************
; 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

The next twp sections are the library and include files that will hold all the definitions for objects and standard function that we may use in our program. All lot of the function that we will use are already written for us and we import them into our program here. When the program is compiled the compiler replaces these include statements with the data from the actual files.

.data

.data?

.const

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

The .data section is where we will create variables that are assigned a value before compilation. The .data? section is where we will create uninitialised variables, that is, variables that are not assigned a value when they are created but will receive a value during the running of the program. The .const section is where objects are created that have values that wont change at any time during program operation and the Macros section holds blocks of code that have specific functions that we store there so we don't have to rewrite them every time we want to use them.

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

start:

     ; Exit app
     Push 0
     Call ExitProcess

end start


.code is the start of our actual code section where we write the operations that we want the CPU to perform. start: is the entry point for the program and the program runs line by line after this point. ; Exit app is a comment describing the nature of the upcoming function. You should ALWAYS use comments as much as possible to explain yo9ur code. Push 0 is a CPU operation where the value 0 is moved onto the section of memory called the stack and Call ExitProcess is the function call to the standard function ExitProcess which exits the application.

That is the basic code template that we will use for our future projects, the only thing left to do is to save it into the appropriate file in the VisualMASM program files folder so copy the program code above and locate the following folder.



As long as you installed Visual MASM into the default folder the location you are looking for will be - "C:\Program Files (x86)\Visual MASM\Templates" and the file you want to open is - Masm32HelloWorld.asm. Open this file in Notepad and delete all the existing text and paste in the default code from above. Save and close the file. Now open Visual MASM.

You can open a new project in two ways - 1 - go to File/New/32-bit Windows MessageBox Application


or Right click on the Project Manager and select Add New Project


and in the next window select 32-bit Windows EXE MessageBox Application and press OK.


When the template opens you will be presented with the default code that we wrote above. If you are not then make sure your copied the code to the correct file. We are now ready to begin coding our first program.

So, thank you for following this tutorial I hope you found it interesting, in the next tutorial we will write our first program using the MessageBox function. So until then, enjoy.