Reputation: 49
I am trying to learn (or at least understand) assembly on windows. I found this Hello World example somewhere and I want to make an .exe out of it just to be sure I have everything set up correctly. For people that will post other Hello World examples I want to make clear I am looking for one with direct windows api calls.
global _start
extern _ExitProcess@4
extern _GetStdHandle@4
extern _WriteConsoleA@20
section .data
msg: db 'Hello, World', 10
handle: db 0
written:
db 0
section .text
_start:
; handle = GetStdHandle(-11)
push dword -11
call _GetStdHandle@4
mov [handle], eax
; WriteConsole(handle, &msg[0], 13, &written, 0)
push dword 0
push written
push dword 13
push msg
push dword [handle]
call _WriteConsoleA@20
; ExitProcess(0)
push dword 0
call _ExitProcess@4
I assemble it successfully like so
nasm -fwin32 hello.asm
Then when I try to link it with ld like so
ld hello.obj -lkernel32
It produces an executable but when I try to run it I get this
Program 'a.exe' failed to run: The specified executable is not a valid application for this OS platform.At line:1 char:1
+ .\a.exe
+ ~~~~~~~.
At line:1 char:1
+ .\a.exe
+ ~~~~~~~
+ CategoryInfo : ResourceUnavailable: (:) [], ApplicationFailedException
+ FullyQualifiedErrorId : NativeCommandFailed
I even downloaded ALINK but whenever I try to link it like so
ALINK hello.obj C:\Windows\System32\kernel32.dll
I get this output
ALINK v1.6 (C) Copyright 1998-9 Anthony A.J. Williams.
All Rights Reserved
Loading file .\programs\hello.obj
Loading file C:\Windows\System32\kernel32.dll
Unsupported CPU type for module
what flags do I have to pass to the linkers, what am I doing wrong?
Upvotes: 1
Views: 920
Reputation: 5775
You are linking it wrong. Unlike FASM or €ASM, the linker ALINK is not capable to fetch import information from Dynamic Linked Library on the fly, it needs an import library. One such library WIN32.lib
is shipped with ALINK or you can generate it yourself. e.g. by dll2lib or similar tool.
Then you should fix errors in your source:
handle
and written
should be DWORD, not BYTE.ExitProcess
instead of _ExitProcess@4
etc., exactly as they are defined at Microsoft API docMake your executable with
nasm -fwin32 barbo24.asm
alink barbo24.obj WIN32.lib -oPE -entry _start -subsys con
barbo24.exe
If it doesn't write Hello, world
, check and debug it with
peview.exe barbo24.exe
ollydbg barbo24.exe
Upvotes: 3