Mastering Assembly Programming
上QQ阅读APP看书,第一时间看更新

The Windows Assembly template (32-bit)

A Windows executable consists of several sections (the structure of a PE executable/object file will be covered in more detail in Chapter 9, Operating System Interface); usually, one section for code, one for data, and one for import data (this contains information on external procedures, which are imported from dynamic link libraries). Dynamic-link libraries (DLL) also have an export section, which contains information on procedures/objects publicly available in the DLL itself. In our template, we simply define the sections and let the assembler do the rest of the work (write headers and so on).

Now, let's take a look at the template itself. See further explanation of PE specifics in the comments:

; File: srctemplate_win.asm

; First of all, we tell the compiler which type of executable we want it
; to be. In our case it is a 32-bit PE executable.
format PE GUI

; Tell the compiler where we want our program to start - define the entry
; point. We want it to be at the place labeled with '_start'.
entry _start

; The following line includes a set of macros, shipped with FASM, which
; are essential for the Windows program. We can, of course, implement all
; we need ourselves, and we will do that in chapter 9.
include 'win32a.inc'

; PE file consists of at least one section.
; In this template we only need 3:
; 1. '.text' - section that contains executable code
; 2. '.data' - section that contains data
; 3. '.idata' - section that contains import information
;
; '.text' section: contains code, is readable, is executable
section '.text' code readable executable
_start:
;
; Put your code here
;


; We have to terminate the process properly
; Put return code on stack
push 0
; Call ExitProcess Windows API procedure
call [exitProcess]

; '.data' section: contains data, is readable, may be writeable
section '.data' data readable writeable
;
; Put your data here
;

; '.idata' section: contains import information, is readable, is writeable
section '.idata' import data readable writeable

; 'library' macro from 'win32a.inc' creates proper entry for importing
; procedures from a dynamic link library. For now it is only 'kernel32.dll',
; library kernel, 'kernel32.dll'

; 'import' macro creates the actual entries for procedures we want to import
; from a dynamic link library
import kernel,
exitProcess, 'ExitProcess'