Windows Assembly Tutorial 1
Starting Out In Windows programming
Welcome to often revered and mystical world of Windows assembly language programming. Programming under windows, let alone assembly language, can be an extremely intimidating task. Shortly, you will soon discover that assembly programming on the Win32 platform can be as easy and sometimes easier than in higher level languages like C++.These tutorials will assume a basic understanding assembly (otherwise known as asm). We use MASM32 for all of our assembly language tutorials, but they will also compile in similar environments.
A major advantage in programming in assembly language is the blisteringly fast execution of code. Another huge advantage is the size of the complied executable. You will soon see that you can have a fully functional Windows application in as little as two an a half kilobytes. In this day and age of 'bloatware', purists at heart will find it hard to go past learning assembly.
If there are any concepts or areas you would like to discuss about this assembly tutorial, please feel free to join in and post a discussion in the forums.
Prerequisites
Include files: windows.inc, kernel32.inc, user32.incLibrary files: kernel32.lib, user32.lib
The simplest Windows program - The 'Message box'
The simplest windows application is the good old message box. So, we might as well start our assembly programming journey here. Message boxes are ideal to convey important information to the user. This information can be used to indicate and error or to even prompt the user for a choice. Once the choice is made, the application can then process the decision made by the operator.Here we can see an example of a message box.
To create a Windows message box in assembly language we need to 'invoke' the MessageBox() function.
Invoke? I hear you say. This is simply the term used to execute the MessageBox function. For those familiar with C and C++ you would create a message box like so;
MessageBox(
NULL,
"Do you really want to continue?",
"Are you sure?",
MB_ICONQUESTION);
In assembly, the same function is called this way;
invoke MessageBox,
NULL,
addr MsgBoxText,
addr MsgBoxCaption,
MB_ICONQUESTION
The message box function definition on MSDN as follows;
int MessageBox(HWND hWnd,LPCTSTR lpText,LPCTSTR lpCaption,UINT uType);
hWnd - is the optional handle of the the window that you are using
lpText - the text displayed inside the message box
lpCaption - the message box title
uType - additional properties used for the icon and types of input (eg, Abort, Retry, etc..)
The final parameter is of great influence on how the message box looks and is designed to react. In the examples shown previously, we have specified 'MB_ICONQUESTION' to make the message box display a question mark. The following types are all valid when creating the message box;
Just as important as 'the look', possibly even more so, are the types input that is available to the user (ie, Cancel, Try Again, Continuencel, etc..)
| MB_ABORTRETRYIGNORE | Abort, Retry, and Ignore |
| MB_CANCELTRYCONTINUE | Cancel, Try Again, and Continue |
| MB_HELP | Help |
| MB_OK | OK |
| MB_OKCANCEL | OK and Cancel |
| MB_RETRYCANCEL | Retry and Cancel |
| MB_YESNO | Yes and No |
| MB_YESNOCANCEL | Yes, No, and Cancel |
To define what icons and message buttons we want to use at the same time, we just separate the options with |.
For example MB_ICONQUESTION|MB_OKCANCEL will display the question mark icon with the buttons labelled 'OK' and 'Cancel'.
The message box function will return the value of the button that was pressed. The return values are actually integers, but it is best to use the 'defines' as listed below for readability and fault finding.
| IDABORT | Abort button was selected |
| IDCANCEL | Cancel button has been chosen |
| IDCONTINUE | Continue has been pushed |
| IDIGNORE | Ignore button has been selected |
| IDNO | No has been pressed |
| IDOK | OK button was clicked |
| IDRETRY | Retry button has been selected |
| IDTRYAGAIN | Try Again button has been selected |
| IDYES | Yes selection has been pressed |
There are quite a few other parameters that relate to the formatting of text and the like. But to get you started, the parameters discussed should suffice. Additional information can be sourced on the MSDN website, which has been linked at the end of the tutorial.
In the second assembly tutorial we will progress to basic Window creation and event handlers.
The Full 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 "An example of Cancel,Retry,Continue",0
MsgBoxText db "Hello Message Box!",0
.code
start:
invoke MessageBox,
NULL,
addr MsgBoxText,
addr MsgBoxCaption,
MB_ICONERROR OR MB_ABORTRETRYIGNORE
.IF eax==IDABORT
; Abort was pressed
.ELSEIF eax==IDRETRY
; Retry was pressed
.ELSEIF eax==IDCANCEL
; Cancel was pressed
.ENDIF
invoke ExitProcess,NULL
end start
Things to try
Try replacing the comment lines with additional message boxes to prove that you can get the correct response to the button being pressed.Also, compare the executable size with the same application compiled in C++. You'll be pleasantly surprised!
Additional information
For more information about message boxes, please visit the MSDN link below.Microsoft (MSDN) - Message boxes
MASM32 homepage