Tuesday, June 10, 2008

Microsoft Macro Assembler Tutorial (MASM)

Microsoft Macro Assembler Tutorial (MASM)


Perquisites:

• Basic Programming knowledge of Any Language
• Must know what a compiler, interpreter, assembler and linker are.

Introduction:

MASM is Assembler developed by Microsoft for assembly language programmers to be able to create Windows Applications using WINDOWS API’s.
It Contains set of include files and libraries which contains declarations of various windows API’s. In this tutorial I’ll be telling you basic features of MASM and how to code a simple Hello Application using MASM. For writing MASM apps I will use a free IDE (Integrated Development Environment) WinASM. WinASM is free and have good features like intellisence which is need of programmers in this fast coding world.

Basic Building Blocks:

A programming language consists of Data types, Expressions or Statements, Conditionals and Looping. Masm comprises of “Basically” 3 types of Data. I insist on the Word Basically because the Windows Data types declared in the header files like ‘HWND’, ‘LPCTR’ are none other then Aliases used for WORD and DWORD data type in MASM.

So there are basically three Data types in MASM:
1. BYTE: This is the smallest data type and uses 8 bit for its storage. its alias is 'db'.
2. WORD: This data type uses 16 bits and is generally referred to int/integer data type in other languages. its alias is 'dw'.
3. DWORD: This is called ‘Double Word’ and takes 32 bits for its storage and is referred to as long/unsigned long data type in other Languages. its alias is 'dw'.

So you write a program using these three basic data types.
Here I would like to give a general reference to you guys how and when to use these data types.
For storing an integer number you should use WORD data type,
For storing a Long Integer number you should use the DWORD data type and for storing a single character use BYTE Data type.
Strings are handled in different ways,(They are rather Array of bytes).
Now comes the general instructions used in MASM. You all are thinking that Assembly Programming must be tough but guys it isn’t masm contains macros which enables you to use English like statements such as if…else statement, while…end while statement. Now importantly comments are used by ‘;’ sign. All the coders must know how comments are crucial in Big Software Development

e.g. if you want to compare two variables , if they are equal or not, an assembly instruction should be as

CMP EAX, BYTE PTR [szName]
But in MASM you can use Conditions Macro .IF as

.IF EAX==BYTE PTR [szName]

.END IF


General Structure of a MASM Program:

Here I will write a general structure of a MASM program and I will explain it. I will be writing a simple program that will show a message box on the screen


.386
.model stdcall,flat

Include windows.inc
Include kernel32.inc

Includelib kernel32.lib
Includelib user32.lib

.data

szMessage db “Hello !”,0
szTitle db “First MASM Program”,0

.code

Start:
Invoke MessageBoxA, 0, addr szMessage, addr szTitle
Invoke ExitProcess, 0
End Start



Now the first line is ‘.386’ this tell the Assembler that the code will be compatible on Intel 80386 Processor and above. However processor below it will not be able to execute the program 100% accurate and may even crash.

The second line ‘.model stdcall,flat’ tells the Assembler that the code model to be used is for Protected mode and the parameters to the functions will be passed from right to left .

Now in the third line we are using include statement which is similar to C ‘s #include statement, that is this is including a header file ‘Windows.inc’ containing prototypes of WINDOWS API’s and Constants. Note: Header Files in ASM end with .inc extension. Then you will notice another preprocessor ‘Inclidelib’ this is a flexible preprocessor which tells which libraries to be linked while generating the EXE.

Now most importantly All the variables in MASM are declared in ‘.data’ section and all code is written in ‘.code’ section so I have declared 2 string variables in data section which are holding strings to be used in messagebox API.

Then I am writing the code in ‘.code’ section. The code section starts with an Entry point Label which you can name anything you wish. I named it Start. Note: The label should end with semi colon. All the code is written with in this label blocks. The label is then at the ended with the ‘end’ statement as end label.

Now I have Called the MessageBox function using Invoke macro. And this is the general basic tutorial of MASM. I hope I have given a brief intro of MASM to the existing programmers.

Regards,
Rohit

No comments: