Hallo Win32 Assemblers,
habe ein Problem mit der Erstellung einer ExE mit NASM.EXE.
der unten stehende Code wird prima compiliert:
nasm -f bin test.asm -o test.exe
aber wenn es gestartet wird, crasht es.
kann da einer mal ein Tipp geben warum das so ist?
Danke
habe ein Problem mit der Erstellung einer ExE mit NASM.EXE.
der unten stehende Code wird prima compiliert:
nasm -f bin test.asm -o test.exe
aber wenn es gestartet wird, crasht es.
kann da einer mal ein Tipp geben warum das so ist?
Danke
Code:
BITS 16
IMAGEBASE equ 0x400000
;
; MZ header
;
; The only two fields that matter are e_magic and e_lfanew
mzhdr:
dw "MZ" ; e_magic
dw 0 ; e_cblp UNUSED
dw 0 ; e_cp UNUSED
dw 0 ; e_crlc UNUSED
dw 0 ; e_cparhdr UNUSED
dw 0 ; e_minalloc UNUSED
dw 0 ; e_maxalloc UNUSED
dw 0 ; e_ss UNUSED
dw 0 ; e_sp UNUSED
dw 0 ; e_csum UNUSED
dw dos_start ; e_ip
dw 0 ; e_cs UNUSED
dw 0 ; e_lsarlc UNUSED
dw 0 ; e_ovno UNUSED
times 4 dw 0 ; e_res UNUSED
dw 0 ; e_oemid UNUSED
dw 0 ; e_oeminfo UNUSED
times 10 dw 0 ; e_res2 UNUSED
dd pesig ; e_lfanew
dos_start:
mov ax, cs
mov ds, ax
mov dx, _text
mov ah, 9
int 0x21
mov ax,0x4c00
int 0x21
mov eax, 0
ret
_text: db 'Nice to meet somebody who is still using DOS,',13,10
db 'but his program requires Win32.',13,10,'$'
dos_end:
;
; PE signature
;
bits 32
pesig:
dd "PE"
;
; PE header
;
pehdr:
dw 0x014C ; Machine (Intel 386)
dw 1 ; NumberOfSections
dd 0x4545BE5D ; TimeDateStamp UNUSED
dd 0 ; PointerToSymbolTable UNUSED
dd 0 ; NumberOfSymbols UNUSED
dw opthdrsize ; SizeOfOptionalHeader
dw 0x10f ; Characteristics (no relocations, executable, 32 bit)
;
; PE optional header
;
filealign equ 1
sectalign equ 1
%define round(n, r) (((n+(r-1))/r)*r)
opthdr:
dw 0x10B ; Magic (PE32)
db 8 ; MajorLinkerVersion UNUSED
db 0 ; MinorLinkerVersion UNUSED
dd round(codesize, filealign) ; SizeOfCode UNUSED
dd 0 ; SizeOfInitializedData UNUSED
dd 0 ; SizeOfUninitializedData UNUSED
dd _start ; AddressOfEntryPoint
dd code ; BaseOfCode UNUSED
dd round(filesize, sectalign) ; BaseOfData UNUSED
dd 0x400000 ; ImageBase
dd sectalign ; SectionAlignment
dd filealign ; FileAlignment
dw 4 ; MajorOperatingSystemVersion UNUSED
dw 0 ; MinorOperatingSystemVersion UNUSED
dw 0 ; MajorImageVersion UNUSED
dw 0 ; MinorImageVersion UNUSED
dw 4 ; MajorSubsystemVersion
dw 0 ; MinorSubsystemVersion UNUSED
dd 0 ; Win32VersionValue UNUSED
dd round(filesize, sectalign) ; SizeOfImage
dd round(hdrsize, filealign) ; SizeOfHeaders
dd 0 ; CheckSum UNUSED
dw 3 ; Subsystem (Win32 GUI) ; gui = 2
dw 0x400 ; DllCharacteristics UNUSED
dd 0x100000 ; SizeOfStackReserve UNUSED
dd 0x1000 ; SizeOfStackCommit
dd 0x100000 ; SizeOfHeapReserve
dd 0x1000 ; SizeOfHeapCommit UNUSED
dd 0 ; LoaderFlags UNUSED
dd 16 ; NumberOfRvaAndSizes UNUSED
; virtual address size
@b equ $
dd 0, 0 ; Export
dd imp_start, imp_size ; Import
dd 0, 0
dd 0, 0
dd 0, 0
dd 0, 0
dd 0, 0
dd 0, 0
dd 0, 0
dd 0, 0
dd 0, 0
dd 0, 0
dd iat_start, iat_size ; Import Adress Table
dd 0, 0
dd 0, 0
dd 0, 0
opthdrsize equ $ - opthdr
;
; PE code section
;
db ".text", 0, 0, 0 ; Name
dd codesize ; VirtualSize
dd round(hdrsize, sectalign) ; VirtualAddress
dd round(codesize, filealign) ; SizeOfRawData
dd code ; PointerToRawData
dd 0 ; PointerToRelocations UNUSED
dd 0 ; PointerToLinenumbers UNUSED
dw 0 ; NumberOfRelocations UNUSED
dw 0 ; NumberOfLinenumbers UNUSED
dd 0x60000020 ; Characteristics (code, execute, read) UNUSED
hdrsize equ $ - $$
;
; PE code section data
;
align filealign, db 0
code:
bits 32
; Entry point
global _start
_start:
; push byte 0
; call [__imp__ExitProcess] ; crash
mov eax,0
ret
data:
msg_hello: db "Hello World",0
iat_start equ $ - IMAGEBASE
USER32_thunk:
__imp__MessageBoxA:
dd USER32_MessageBoxA
dd 0
KERNEL32_thunk:
__imp__ExitProcess:
dd KERNEL32_ExitProcess
dd 0
iat_size equ $ - IMAGEBASE - iat_start
; Import Directory
imp_start equ $ - IMAGEBASE
imp:
dd USER32_import - IMAGEBASE
dd 0
dd 0
dd USER32_name - IMAGEBASE
dd USER32_thunk - IMAGEBASE
dd KERNEL32_import - IMAGEBASE
dd 0
dd 0
dd KERNEL32_name - IMAGEBASE
dd KERNEL32_thunk - IMAGEBASE
dd 0
dd 0
dd 0
dd 0
dd 0
imp_size equ $ - imp
USER32_name:
db "user32.dll", 0
db 0
USER32_import:
dd USER32_MessageBoxA - IMAGEBASE
dd 0
db 0
USER32_MessageBoxA:
dw 0
db "MessageBoxA", 0
db 0
KERNEL32_name:
db "kernel32.dll", 0
db 0
KERNEL32_import:
dd KERNEL32_ExitProcess
dd 0
db 0
KERNEL32_ExitProcess:
dw 0
db 'ExitProcess', 0
db 0
codesize equ $ - code
filesize equ $ - $$