Fasm x64 MsgBox

2

I want to compile x64 app with simple MsgBox using Fasm. I've wrote the code, it compiles successfully, but when I run it nothing is shown and the program just ends. What's wrong?

format PE64 GUI 4.0
entry main

include 'win64a.inc'

main:
  invoke MessageBox,NULL,'Hello, World!','Fasm message box:',MB_OK
  invoke ExitProcess,0

library kernel32,'kernel32.dll',\
        user32,'user32.dll'

include 'api/kernel32.inc'
include 'api/user32.inc'

If try to debug in VS2017 I get an exception:

Вызвано исключение по адресу 0x0000000000001108 в program.exe: 0xC0000005: нарушение прав доступа при исполнении по адресу 0x0000000000001108.

If translate:

Exception at address 0x0000000000001108 in program.exe: 0xC0000005: access violation when executing address 0x0000000000001108.

windows
assembly
windows-10
64-bit
fasm
asked on Stack Overflow May 5, 2020 by Qwertiy • edited May 5, 2020 by Qwertiy

1 Answer

2

I'm marking this as community wiki so others can fill in a description of why this works. Of note is:

  • .idata section for imports
  • .text section that is executable
  • sub rsp, 8 (or equivalent like push rbp) for stack alignment per the Windows x86-64 calling convention.

The code:

include 'win64a.inc'

format PE64 GUI 4.0
entry main

section '.text' code readable executable
main:
  sub rsp, 8
  invoke MessageBox,NULL,'Hello, World!','Fasm message box:',MB_OK
  invoke ExitProcess,0

;section '.data' data readable writeable
; Data here

section '.idata' import data readable
library kernel32,'kernel32.dll',\
        user32,'user32.dll'

include 'api/kernel32.inc'
include 'api/user32.inc'
answered on Stack Overflow May 5, 2020 by Michael Petch

User contributions licensed under CC BY-SA 3.0