Assembly language examples using NASM on Windows
Console Message - 64 bit
Output a message to the console window, 64 bit.
- nasm -f win64 ConsoleMessage64.asm -o ConsoleMessage64.obj
- golink /entry:Start /console kernel32.dll user32.dll ConsoleMessage64.obj
- polink /ENTRY:Start /SUBSYSTEM:CONSOLE /LIBPATH:c:\lib64 kernel32.lib user32.lib ConsoleMessage64.obj
Open a console window, and run the executable.
The message length is calculated by subtracting the starting address of Message from the starting address of the line containing the $
WriteFile was chosen instead of WriteConsoleA, as the output can be redirected to a file using > filename
WriteFile takes 5 parameters. The 5th is placed on to the stack.
All RSP subtraction and addition is written out in full before and after each Windows API function call, this is to make it clear
as to what needs to be done.
NULL EQU 0
STD_OUTPUT_HANDLE EQU -11
Message db "Console Message 64", 0Dh, 0Ah
MessageLength EQU $-Message
StandardHandle resq 1
Written resq 1
sub RSP, 8
sub RSP, 32
mov ECX, STD_OUTPUT_HANDLE
mov qword [REL StandardHandle], RAX
add RSP, 32
sub RSP, 32 + 8 + 8
mov RCX, qword [REL StandardHandle]
lea RDX, [REL Message]
mov R8, MessageLength
lea R9, [REL Written]
mov qword [RSP + 4 * 8], NULL
add RSP, 48
xor ECX, ECX