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

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'
主站蜘蛛池模板: 海阳市| 庄浪县| 九龙城区| 城固县| 和林格尔县| 巴南区| 长葛市| 吉安县| 莲花县| 高淳县| 九寨沟县| 信丰县| 大姚县| 林甸县| 潞城市| 青州市| 江达县| 新乡县| 中宁县| 东兰县| 鄂尔多斯市| 北海市| 板桥市| 镶黄旗| 舟山市| 三台县| 侯马市| 葫芦岛市| 阿巴嘎旗| 金湖县| 泉州市| 开封市| 平江县| 湘潭县| 江口县| 威海市| 驻马店市| 绿春县| 印江| 秀山| 台中县|