I am trying to write a 64-bit NASM assembly program in Windows to simply print some output to the console. I would like to do this with the most pure assembly possible, so that excludes any standard C libraries. I'm also trying to automate the assembly and linking process, so opening Visual Studio and doing it manually is not an option.
I know that I have to use the Windows APIs like _GetStdHandle and _WriteFile, but I can't figure out how to link those libraries, or how to use them in a 64-bit program.
This is the "Hello World" assembly code I am trying to run:
global _main
extern _GetStdHandle@4
extern _WriteFile@20
extern _ExitProcess@4
section .text
_main:
; DWORD bytes;
mov ebp, esp
sub esp, 4
; hStdOut = GetstdHandle( STD_OUTPUT_HANDLE)
push -11
call _GetStdHandle@4
mov ebx, eax
; WriteFile( hstdOut, message, length(message), &bytes, 0);
push 0
lea eax, [ebp-4]
push eax
push (message_end - message)
push message
push ebx
call _WriteFile@20
; ExitProcess(0)
push 0
call _ExitProcess@4
; never here
hlt
message:
db 'Hello, World', 10
message_end:
But I'm not sure how to link the Windows 64-bit APIs to the program. I have also been getting this error: "instruction not supported in 64-bit mode" for the instruction "push eax". I'm just not sure how to re-write this code for 64-bit.
This is the batch program I am using to assemble and link the program:
nasm -f win64 HelloWorld.asm -o HelloWorld.obj &&^
"C:\Program Files\GoLink\GoLink.exe" /console /entry _main HelloWorld.obj /fo HelloWorld.exe &&^
HelloWorld.exe
Can anyone walk me through the steps required to get this program to run with the given constraints?