|
tut01en1.asm Source Code
|
format PE GUI
MB_OK = 00h
MB_ICONEXCLAMATION = 30h
push MB_OK + MB_ICONEXCLAMATION
push _caption
push _message
push 0
call [MessageBox]
push 0
call [ExitProcess]
_caption db 'Win32 Assembly Programming',0
_message db 'Hello World!',0
data import
dd 0,0,0,RVA kernel_name,RVA kernel_table
dd 0,0,0,RVA user_name,RVA user_table
dd 0,0,0,0,0
kernel_table:
ExitProcess dd RVA _ExitProcess
dd 0
user_table:
MessageBox dd RVA _MessageBoxA
dd 0
kernel_name db 'KERNEL32.DLL',0
user_name db 'USER32.DLL',0
_ExitProcess dw 0
db 'ExitProcess',0
_MessageBoxA dw 0
db 'MessageBoxA',0
end data
|
|
|
Screenshot
|
|
|
format
This word is a directive. It indicates the type of format the assembler must give to the final file. In other words, the kind of program we want to produce.
| PE GUI
The values given to the directive indicates to produce Portable Executable for the graphical interface.
|
=
The "equal than" sign is another directive. Fasm will substitute given name with the associated numerical value.
In the example, each time "MB_OK" appears in the file, fasm replaces it with a zero (0).
| Numeric values terminated in "h" are interpreted as hexadecimal, that is to say, numbers counted from 0 to 15 instead of 0 to 9 and thus using the first 6 letters of the alphabet. They are so used that we will give a detailed explanation a little later. Number 030h corresponds to 48 decimal.
|
push
This is an assembly instruction that is converted by fasm in a direct order to the processor. Push "pushes" the given value to a stack of values in memory. The stack stores values in the arrival order, with the latest one at top.
| The result of the sum of 0+030h is being pushed into the stack.
In other words, the first value in (top of) stack is 030h
|
The following instruction pushes the value of label into the stack. This value is an address or position in memory. This one is a fundamental concept for the learning of the assembly language.
Fasm maintains the value of position that corresponds to the point where "_caption" label is defined. In order to define a label there are three equivalent options:
1. To use a data directive, in the case of _caption is db
2. To make follow it by a colon, as in the case of kernel_table:
3. To use the label directive, that we will see in another tutorial.
|
db
It is a data directive meanning Data Byte. It indicates the assembler to convert the information that follows in sequences of bytes to write them in the produced file (our program).
One byte corresponds to eight bits, it can represent a value between 0 and 255.
| As information it counts comma separated numbers and/or text strings. A text string is converted character by character using a code where each symbol is paired to a number (ASCII code).
|
call
This is other assembly instruction. Its work is to "call" a function, external or internal. A function is a portion of code doing a particular task and is referred by a meaningful label. Please remember that a label represents an address in memory.
Calling the function is telling the processor to follow the instructions when we call the label.
|
The function MessageBox is an external function. It belongs to Windows and could be used by any program. Its function is to show us the message in a window while waiting for us to click on a button.
In order to work, the function needs to know which message and title to show, how many buttons and the style to use. This information is taken directly from the stack by the function and we need to provide it exactly and in the proper order before calling.
|
The function ExitProcess is another Windows' function. Its work is to properly terminates our program; a well done Windows program always end by calling ExitProcess.
Have you noticed the zero we put on stack before calling this function? It serves to tell Windows how our program ended. Conventionally, zero means that our program does well what it was intended to do.
|
Here the execution of our program finishes indeed.
There are some lines not analyzed. These lines are not being executed. They serve for the correct operation of the program and each one accomplishes a certain task. Although they seem cryptic enough, in fact this is only at first glance. In order to explain them, we will need additional concepts that we will see at the end of this tutorial.
OK, let's go. We are going to see our program in action!
|
|