Win32 assembly tutorial two Win32 assembler structure and grammar

zhaozj2021-02-08  235

Structure and syntax of Win32 assembler

-------------------------------------------------- ------------------------------

Win32ASM program structure and syntax

Let's take a look at a simplest Win32 assembler:

.386

.Model flat, stdcall

Option Casemap: None; Case Sensitive

INCLUDE Windows.inc

INCLUDE KERNEL32.INC

IncludeLib kernel32.lib

.DATA

SZCAPTION DB 'WIN32 Assembly Examples', 0

Sztext DB 'Win32 Compilation, 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 implemented. I will simply introduce the role of each part:

.386

The same is true for this statement and DOS, which is to tell the compiler that we need to use the 80386 instruction set, because 32-bit assemblers use 32-bit registers such as EAX, EBX, etc., this sentence is must, of course You can also use .486, .586, etc., use .386p, .486p, etc.

.Model flat, stdcall

.MODEL tells the compiler program mode, people who have edited DOS assembly may know that there is Tiny, Small, ... huge, etc. in the DOS program, which specifies the program memory logo, in huge and other modes, memory find Site and subroutine calls will use FAR format, but in Win32 assembly, you can only use a mode, FLAT mode, because the Win32 program is used, memory is a continuous 4GB segment, does not matter small or large mode. The stdcall tells the transfer method of the compiler parameter. When the subroutine is called, the parameters are three types passing through the stack. Stdcall, C and PASCAL, and STDCALL specifies the parameters from right to left into the stack, For example, for a Windows API such as MessageBox, this is defined in the manual:

Int messagebox

HWND HWND, // Handle of Owner Window

LPCTSTR LPTEXT, / / ​​Address of Text In Message Box

LPCTSTSTR LPCAPTION, / / ​​Address of Title of Message Box

Uint utype // style of message box

);

So we can call it like this in assembly:

Push utype

Push LPCAPTION

Push lptext

Push hwnd

Call MessageBox

Everyone should pay attention to the rightmost parameters is the last pile of stack. Of course, we don't have to call an API like this, because a macro statement in Masm not only helps us complete all the stack operations, and help us check the parameters Whether the number is correct, that is, the Invoke statement, we can change the above statement into invoke messagebox, hwnd, lptext, lpcaption, utype. If the actual parameters in this program become Invoke Messagebox, NULL, AddR Sztext, AddR Szcaption, MB_OK. INCLUDE statement

The include statement contains some system definitions and API letters, where all Windows data structures definitions and constant definitions are included in Windows.inc, and other API functions are included in xxx.inc, such as check Microsoft Win32 Programmer's Reference knows EXITPROCESS is included in kernel32.dll, then we will include include the include Kernel32.inc and includeLIDELIB KERNEL32.LIB statement in the program, otherwise an error that does not define an API function when compiling. MessageBox is in user32.dll, then we have to include include Users32.inc and includeelib user32.lib statements in the program

.DATA or .DATA?

The next is the data segment ,.data defines a predefined variable ,.data? Defines the unmelted variable, the difference between the two is .DATA? The variable defined is not occupied .EXE file size, and It is dynamically allocated when the program is executed, so the data that does not specify the initial value can be placed in the .DATA section, such as a 1K size buffer, put it in .Data?, The program will not add one byte.

.code

Indicates that the next is the code segment, all of our code is placed here. The last START statement specifies the statement that the program starts executing. EXITPROCESS in the program is a standard WIN32 API, corresponding to INT 20H or MOV AH, 4CH / INT 21H in DOS assembly, that is, the program exits. Messagebox is also a standard API. The function is to display a message box on the screen. The specific parameters have explained that there is also note that invoke messagebox, null, addr sztext, addr szcaption, MB_OK statement, MB_OK and NULL It has predefined in Windows.inc.

转载请注明原文地址:https://www.9cbs.com/read-1427.html

New Post(0)