Objective
We learned about the use environment of the WIN32 assembly, and in this section we start with the simplest Win32 assembler to understand the basic structure and syntax of the WIN32 assembler.
I. Structure and grammar of the WIN32ASM program
Let's take a look at one of the simplest WIN32 assembler programs:
.386
. Model Flat, StdCall
Option Casemap:none; Case sensitive
Include Windows.inc
Include Kernel32.inc
Includelib Kernel32.lib
. Data
Szcaption DB ' Win32 Assembly example ', 0
Sztext DB ' Win32 assembly, Simple and powerful! ', 0
. Code
Start
Invoke Messagebox,null,addr sztext,addr SZCAPTION,MB_OK
Invoke Exitprocess,null
End Start
This is the simplest Win32 assembler that can be executed, and I'll briefly explain the roles of each part:
.386
This statement is the same as a DOS assembly, is to tell the compiler we want to use 80386 of the instruction set, because the 32-bit assembler to use 32-bit registers such as EAX,EBX, so this sentence is necessary, of course, you can also use. 486,.586, when you use a privileged command, you can use it. 386p,.486p and so on.
. Model Flat,stdcall
Model tells the compiler the mode of the program, compiled DOS assembly of the person may know in the DOS program mode has tiny,small,... huge, etc., it specifies the program memory addressing mode, in huge and other modes, memory addressing and subroutine calls will be in far format, But in the Win32 assembly, you can only use one pattern, the flat mode, because the memory is a continuous 4GB segment for the WIN32 program, regardless of the small or large pattern. Instead, StdCall tells the compiler how the parameters are passed, and when the subroutine is invoked, the parameters are passed through the stack, and the parameters are passed in three ways, Stdcall,c and Pascal,stdcall specify that the parameters are pressed from right to left on the stack, such as for a Windows API such as MessageBox, which is so defined in the handbook:
int MessageBox (
HWND hwnd,//Handle of owner window
LPCTSTR Lptext,//address of the text in message box
LPCTSTR lpcaption,//address of the title of message box
UINT Utype//style of message box
);
So in the assembly we can call it this way:
Push Utype
Push Lpcaption
Push Lptext
Push HWnd
Call MessageBox
We should note that the most right parameter is the last one into the stack, of course, we do not have to bother to call an API, because a macro statement in MASM not only help us to complete all the stack operations, but also to help us check the number of parameters is correct, that is the invoke statement, We can change the above statement to invoke Messagebox,hwnd,lptext,lpcaption,utype. If the actual parameter in this procedure is the invoke Messagebox,null,addr sztext,addr SZCAPTION,MB_OK.
Include statement
The INCLUDE statement contains system definitions and API descriptions, where all windows data structure definitions and constant definitions are contained in Windows.inc, while the descriptions of other API functions are contained in xxx.inc, such as the Microsoft Win32 progr Ammer ' s Reference knows ExitProcess is included in Kernel32.dll, then we'll include included Kernel32.inc and Includelib in the program. Kernel32.lib statement, otherwise an error that is not defined by the API function will occur at compile time. and MessageBox in User32.dll, then we're going to include included User32.inc and includelib user32.lib statements in the program
. data or. Data?
Indicates that the next is the data segment,. The. Data defines the predefined variables, and the. Data defines the uninitialized variables, and the difference between the two is. Data? The defined variables do not occupy the size of the. exe file, but are dynamically allocated when the program executes, so that data that does not specify an initial value can be placed in. Data. Segment, such as a buffer of 1K size, in. Data, the program will not add one byte.
. Code
Indicates that the code snippet is next, and all of our code is here. The last start statement specifies the statement that the program starts executing. The ExitProcess in the program is a standard Win32 API that corresponds to the int 20h or MOV ah,4ch/int 21h in the DOS assembly, which is the program exit. And MessageBox is also a standard API, the function is to display a message box on the screen, the specific parameters have been explained above also have to note that invoke Messagebox,null,addr sztext,addr SZCAPTION,MB_OK Statement, MB_OK and NULL are already predefined in Windows.inc.
Cond...