flat assembler
Message board for the users of flat assembler.

Index > Windows > Pe header File questions

Author
Thread Post new topic Reply to topic
frankobach26



Joined: 10 Apr 2026
Posts: 19
Location: Kassel
frankobach26 16 Oct 2026, 11:17
Hello. Need a little Help with pe headers. ITS quite new for me and hard to understand. My Dummy Test Exemple I can compile IT Not more. No messagebox No result. There must be a Lot of mistakes sorry ..


Code:
; manual_pe.asm
; Manually constructed PE32 GUI executable
; Target: Windows 10 x86 / x64 (WoW64)
; Assembler: FASM
;
; Expected result:
;   A MessageBox appears.
;   Clicking OK exits with code 0.
;
; No external linker required.


format binary as "exe"
use32
; include 'win32a.inc'

IMAGE_BASE      = 00400000h
SECTION_ALIGN   = 00001000h
FILE_ALIGN      = 00000200h
HEADERS_SIZE    = 00000200h

TEXT_RVA        = 00001000h
RDATA_RVA       = 00002000h
IDATA_RVA       = 00003000h

TEXT_RAW        = 00000200h
RDATA_RAW       = 00000400h
IDATA_RAW       = 00000600h

; ============================================================
; DOS HEADER (64 bytes)
; ============================================================

dos_header:
        dw 'MZ'
        rb 58
        dd 080h

        rb 080h - ($ - $$)

; ============================================================
; PE SIGNATURE + COFF HEADER
; ============================================================

pe_header:
        db 'PE', 0, 0

        dw 014Ch                   ; IMAGE_FILE_MACHINE_I386
        dw 3                       ; NumberOfSections
        dd 0                       ; TimeDateStamp
        dd 0                       ; PointerToSymbolTable
        dd 0                       ; NumberOfSymbols
        dw 224                     ; SizeOfOptionalHeader
        dw 0102h                   ; Executable + 32-bit

; ============================================================
; OPTIONAL HEADER: PE32 (224 bytes)
; ============================================================

optional_header:
        dw 010Bh                   ; PE32
        db 0, 0                    ; Linker version

        dd FILE_ALIGN              ; SizeOfCode
        dd FILE_ALIGN * 2          ; Initialized data
        dd 0                       ; Uninitialized data

        dd TEXT_RVA                ; Entry point
        dd TEXT_RVA                ; BaseOfCode
        dd RDATA_RVA               ; BaseOfData

        dd IMAGE_BASE
        dd SECTION_ALIGN
        dd FILE_ALIGN

        dw 6, 0                    ; OS version
        dw 0, 0                    ; Image version
        dw 6, 0                    ; Subsystem version
        dd 0                       ; Win32VersionValue

        dd 04000h                  ; SizeOfImage
        dd HEADERS_SIZE            ; SizeOfHeaders
        dd 0                       ; CheckSum

        dw 2                       ; Windows GUI subsystem
        dw 0                       ; DllCharacteristics

        dd 100000h                 ; Stack reserve
        dd 01000h                  ; Stack commit
        dd 100000h                 ; Heap reserve
        dd 01000h                  ; Heap commit

        dd 0                       ; LoaderFlags
        dd 16                      ; NumberOfRvaAndSizes

; ============================================================
; DATA DIRECTORIES
; 16 entries, each 8 bytes
; ============================================================

directory_entries:

        ; 0 Export
        dd 0, 0

        ; 1 Import -- IMPORTANT
        dd IDATA_RVA + (import_descriptors - idata_start)
        dd import_end - import_descriptors

        ; 2 Resource
        dd 0, 0

        ; 3 Exception
        dd 0, 0

        ; 4 Security
        dd 0, 0

        ; 5 Base Relocation
        dd 0, 0

        ; 6 Debug
        dd 0, 0

        ; 7 Architecture
        dd 0, 0

        ; 8 Global Pointer
        dd 0, 0

        ; 9 TLS
        dd 0, 0

        ; 10 Load Config
        dd 0, 0

        ; 11 Bound Import
        dd 0, 0

        ; 12 IAT
        dd IDATA_RVA + (iat_exitprocess - idata_start)
        dd (iat_messagebox + 8) - iat_exitprocess

        ; 13 Delay Import
        dd 0, 0

        ; 14 CLR
        dd 0, 0

        ; 15 Reserved
        dd 0, 0

; ============================================================
; SECTION TABLE
; ============================================================

section_table:

        ; .text
        db '.text', 0, 0, 0
        dd text_end - text_start
        dd TEXT_RVA
        dd FILE_ALIGN
        dd TEXT_RAW
        dd 0, 0
        dw 0, 0
        dd 060000020h

        ; .rdata
        db '.rdata', 0, 0
        dd rdata_end - rdata_start
        dd RDATA_RVA
        dd FILE_ALIGN
        dd RDATA_RAW
        dd 0, 0
        dw 0, 0
        dd 040000040h

        ; .idata
        db '.idata', 0, 0
        dd idata_end - idata_start
        dd IDATA_RVA
        dd FILE_ALIGN
        dd IDATA_RAW
        dd 0, 0
        dw 0, 0
        dd 0C0000040h

; Pad headers to exactly 0x200 bytes.
rb HEADERS_SIZE - ($ - $$)

; ============================================================
; .text -- file offset 0x200, RVA 0x1000
; ============================================================

text_start:

start:
        ; MessageBoxA(HWND, lpText, lpCaption, uType)

        push 0
        push IMAGE_BASE + RDATA_RVA + (title - rdata_start)
        push IMAGE_BASE + RDATA_RVA + (message - rdata_start)
        push 0

        call dword [IMAGE_BASE + IDATA_RVA + (iat_messagebox - idata_start)]

        ; ExitProcess(0)

        push 0
        call dword [IMAGE_BASE + IDATA_RVA + (iat_exitprocess - idata_start)]

        ; Should never be reached.
        int 3

text_end:

rb TEXT_RAW + FILE_ALIGN - ($ - $$)

; ============================================================
; .rdata -- file offset 0x400, RVA 0x2000
; ============================================================

rdata_start:

title:
        db 'Manual PE32 Test', 0

message:
        db 'Hello from FASM!', 13, 10
        db 'Windows loaded this manually built PE32.', 0

rdata_end:

rb RDATA_RAW + FILE_ALIGN - ($ - $$)

; ============================================================
; .idata -- file offset 0x600, RVA 0x3000
; ============================================================

idata_start:

; Import descriptors:
; OriginalFirstThunk, TimeDateStamp,
; ForwarderChain, Name, FirstThunk

import_descriptors:

        ; KERNEL32.DLL
        dd IDATA_RVA + (ilt_kernel - idata_start)
        dd 0
        dd 0
        dd IDATA_RVA + (kernel_name - idata_start)
        dd IDATA_RVA + (iat_exitprocess - idata_start)

        ; USER32.DLL
        dd IDATA_RVA + (ilt_user - idata_start)
        dd 0
        dd 0
        dd IDATA_RVA + (user_name - idata_start)
        dd IDATA_RVA + (iat_messagebox - idata_start)

        ; Null descriptor terminates the table.
        dd 0, 0, 0, 0, 0

import_end:

; DLL names

kernel_name:
        db 'KERNEL32.DLL', 0

user_name:
        db 'USER32.DLL', 0

align 4

; Import Lookup Table: KERNEL32

ilt_kernel:
        dd IDATA_RVA + (hint_exitprocess - idata_start)
        dd 0

; Import Lookup Table: USER32

ilt_user:
        dd IDATA_RVA + (hint_messagebox - idata_start)
        dd 0

; Import Address Table: KERNEL32

iat_exitprocess:
        dd IDATA_RVA + (hint_exitprocess - idata_start)
        dd 0

; Import Address Table: USER32

iat_messagebox:
        dd IDATA_RVA + (hint_messagebox - idata_start)
        dd 0

align 2

hint_messagebox:
        dw 0
        db 'MessageBoxA', 0

hint_exitprocess:
        dw 0
        db 'ExitProcess', 0

idata_end:

rb IDATA_RAW + FILE_ALIGN - ($ - $$)

; End of file
    
Post 16 Oct 2026, 11:17
View user's profile Send private message Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2026, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.