An example of a Win32 assembly language program

Source: Internet
Author: User
Tags api manual execution function prototype include prototype definition win32

First, we look at a "complex" Win32 assembler.
Program to display a message box
--------------------------------------------------
; File name: 3.asm

.386
. Model Flat, StdCall

NULL equ 0
MB_OK equ 0

ExitProcess PROTO:D Word
MessageBoxA PROTO:D Word,:D Word,:D Word,:D Word

Includelib Kernel32.lib
Includelib User32.lib

. Data
Sztext db "Hello, world!", 0
Szcaption db "Win32asm", 0

. Code
Start
Push MB_OK
Lea Eax,szcaption
Push EAX
Lea Eax,sztext
Push EAX
Push NULL
Call MessageBoxA
XOR Eax,eax
Push EAX
Call ExitProcess
End Start
--------------------------------------------------
Compile Link:
In the following two steps:
ML/C/coff 3.asm
Link/subsystem:windows/libpath:d:\masm7\lib 3.obj

The first step is to compile the 3.obj file
/C means compile only, not link
/coff represents the target file for generating COFF format

The second chain delivered into 3. exe files
/subsystem:windows to generate Windows files
/libpath:d:\masm7\lib indicates that the path to the citation library is: D:\masm7\lib.
After installing Masm32, the introduction library is located in the Masm32\lib directory.

You can also set the value of environment variable LIB: Type set lib=d:\masm7\lib at a DOS prompt, so that "link" can be simply written:
Link/subsystem:windows 3.obj, imagine, in the process of debugging, modify the source program is commonly used, each compile link to bring/libpath: ... How annoying would that be? Of course, we can also in the source program directly to the location of the input, so that the link is convenient, as follows:

Includelib D:\masm7\lib\kernel32.lib
Includelib D:\masm7\lib\user32.lib
--------------------------------------------------
Execution: At the DOS prompt, type 3, enter, a message box, haha, the real Win32 program!

--------------------------------------------------
In-depth analysis:
Look at the source program, there are so two lines: Call Messageboxa\call exitprocess. As you can see, this is a subroutine call, but we don't write subroutines like this, in fact, these are API functions. As a function, we may need to pass some arguments to the function when we call, how does the program know what the parameters are and what type is it? is through the function prototype definition, as follows:
ExitProcess PROTO:D Word
MessageBoxA PROTO:D Word,:D Word,:D Word,:D Word
As you can see, ExitProcess has one parameter, MessageBoxA has four parameters, all of which are DWORD types.

In Win32, the passing of parameters is done through the stack. Like MessageBoxA This function has four parameters, whether it is the left side of the stack or on the right first into the stack? Model Flat,stdcall gives the answer. STDCALL specifies that the parameter is pressed from right to left on the stack, and that the adjustment stack is completed when the subroutine returns. You do not need to use the "Add SP, Value" in the source program to keep the stack balanced. For MessageBox, this is defined in the API manual:
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 there will be a segment of our program:
Push MB_OK
Lea Eax,szcaption
Push EAX
Lea Eax,sztext
Push EAX
Push NULL
Call MessageBoxA

Look at the above program, it is not difficult to think, if you write a program, less to the stack into a data, it will be a fatal error. Can this check the number of parameters to match the work to the computer to complete it? This is OK, and the invoke instruction can help us to do the work. If you have an incorrect number of parameters, the connector will give you a bad indication. Therefore, it is strongly recommended that you use Invoke instead of call to invoke the subroutine, which, of course, is not absolute. Use invoke the above instructions can be written in the following way, it looks like a lot of simplicity, check the wrong is also convenient!
Invoke MessageBoxA, null,addr sztext,addr SZCAPTION,MB_OK

In addition, like NULL,MB_OK are some constants, there are a lot of constants, there are many structures, if in our program in the beginning to write so many things, may suddenly frighten you, it is easy to make mistakes, but also not easy to see the main part of the program. The hutch Windows.inc contains the definitions of constants and structs required for WIN32 programming, and we can simply insert these constants and struct definitions into our files with an include directive:
Include D:\masm32\include\windows.inc

However, the windows.inc does not contain the declaration of the function prototype, but also obtains the declaration of the function prototype from other headers, such as: MessageBoxA's prototype declaration in the User32.inc file, exitprocess in the Kernel32.inc file. These header files are placed under the \masm32\include folder.

Also, to use Windows.inc, you must use option Casemap:none, which means telling MASM to differentiate between the case of the symbol, for example, start and start are different. Otherwise, a small program, there may be hundreds of mistakes!

The other, no longer in detail, to this, the above procedures can be modified as follows:
-----------------------------------------------------------------
; The final result
.386; Indicates that 386 directives are used
Model Flat,stdcall 32-bit program, to use flat!;stadcall, standard call
option Casemap:none; difference case

Include Windows.inc, including constants and structure definitions

Include Kernel32.inc; function prototype declaration
Include User32.inc

Includelib kernel32.lib; used in the introduction of the storage
Includelib User32.lib

. Data area, defining 2 strings
Sztext db "Hello, world!", 0
Szcaption db "Win32asm", 0

. Code; Start the Execution office
Start
Invoke Messagebox,null,addr sztext,addr SZCAPTION,MB_OK

; Call the MESSAGEBOXAPI function
Invoke Exitprocess,null; program exit
End start;
------------------------------------
Compile Link:
ML/C/coff/i d:\masm7\include 3.asm; Note the case of the switch character recognition

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.