Assembly language examples using NASM on Windows
Console Message - 64 bit
Description
Output a message to the console window, 64 bit.
Assemble
- nasm -f win64 ConsoleMessage64.asm -o ConsoleMessage64.obj
Link
- golink /entry:Start /console kernel32.dll user32.dll ConsoleMessage64.obj
- polink /ENTRY:Start /SUBSYSTEM:CONSOLE /LIBPATH:c:\lib64 kernel32.lib user32.lib ConsoleMessage64.obj
Notes
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.
Code
NULL EQU 0
STD_OUTPUT_HANDLE EQU -11
extern GetStdHandle
extern WriteFile
extern ExitProcess
global Start
section .data
Message db "Console Message 64", 0Dh, 0Ah
MessageLength EQU $-Message
section .bss
alignb 8
StandardHandle resq 1
Written resq 1
section .text
Start:
sub RSP, 8
sub RSP, 32
mov ECX, STD_OUTPUT_HANDLE
call GetStdHandle
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
call WriteFile
add RSP, 48
xor ECX, ECX
call ExitProcess