Search code examples
x86-64fasm

64bit Hello world freezes after compiling


Program compiles, but freezes after starting. If replace the format and include with 32-bit versions or comment out the MessageBox, then everything works fine.

format PE64 GUI

include 'E:\Fresh\include\win64a.inc'

entry start

section '.data' data readable writeable

        text db 'Hello world!',0

section '.text' code readable executable
start:
        invoke MessageBox,0,text,text,0
        invoke ExitProcess,0

section '.idata' import data readable writeable

        library kernel32,'KERNEL32.DLL', user32, 'USER32.DLL'
        import kernel32, ExitProcess, 'ExitProcess'
        import user32, MessageBox, 'MessageBoxA'  

Solution

  • Your stack is not aligned to 16 bytes, as the ABI requires. Add and rsp, -16 to the beginning of your code, and it will work.

    Regarding this exchange in the comments:

    Ruslan: What does the disassembly look like? Are invoke macros expanded as expected?

    rancid_rot: Not sure, there is MessageBox in cs instead of ds. And mov rcx,0 instead push 0.

    I'd recommend avoiding invoke and similar macros until you learn what they should expand to. Otherwise you think you write in assembly, but actually you write in a high-level language only resembling assembly, not even knowing what code you will get in the end—thus defying the whole purpose of using an assembler.

    To actually learn to call functions in Win64 assembly, see the documentation on Win64 calling conventions.