官术网_书友最值得收藏!

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'
主站蜘蛛池模板: 吴桥县| 中阳县| 诸暨市| 五家渠市| 称多县| 名山县| 邯郸市| 桂林市| 当阳市| 时尚| 东方市| 女性| 依安县| 美姑县| 酉阳| 东乡县| 南溪县| 怀安县| 蒙阴县| 江油市| 尼木县| 巴塘县| 城固县| 昌宁县| 光泽县| 弋阳县| 密山市| 锡林郭勒盟| 舟曲县| 施秉县| 昌宁县| 上栗县| 浮山县| 田阳县| 吉安县| 扎鲁特旗| 合作市| 沂水县| 玉屏| 宿州市| 新河县|