Created
May 9, 2022 03:03
-
-
Save Colouratura/43d1655004c1fe4dc21e27670bc131bb to your computer and use it in GitHub Desktop.
Simple MASM program to output hello world
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
; NOTE: to successfully build you must have a copy of kernel32.lib in the same directory! | |
; build: | |
; ml64 helloworld.asm /link /subsystem:console /entry:main | |
includelib kernel32.lib ; library - kernel functions (Windows Kit) (directory relative) | |
; external functions and handles | |
GetStdHandle proto ; function - gets an output handle | |
WriteConsoleA proto ; funciton - writes to a console handle | |
ExitProcess proto ; function - exits the current process | |
SetConsoleTextAttribute proto ; function - set text attributes for file | |
Console equ -11 ; handle - defualt handle for the current console | |
.data | |
msg byte "Hello, World!", 0Ah, 0Dh ; null-terminated string of bytes | |
stdout qword ? ; reserves space for handel to stdout | |
nBytesWritten qword ? ; reserved space for the number of bytes written | |
.code | |
main proc | |
sub rbp, 40 ; create a 40-byte frame on the stack | |
; get file handle for Console | |
; filehandle GetStdHandle(RCX) | |
mov rcx, Console ; move Console value to RCX (console handle) | |
call GetStdHandle ; gets handle for Console and puts it into RAX | |
mov stdout, rax ; store the handle in 'stdout' | |
; set bgcolor and fgcolor text attributes | |
; void SetConsoleTextAttribute(RCX, RDX) | |
mov rcx, stdout ; RCX (arg 1) - file handle | |
mov rdx, 1fh ; RDX (arg 2) - bgcolor = 0x1 (Blue), fgcolor = 0xf (Bright white) | |
call SetConsoleTextAttribute ; call the function (no return) | |
; displays the message in the console | |
; void WriteConsoleA() | |
mov rcx, stdout ; RCX (arg 1) - file handle | |
lea rdx, msg ; RDX (arg 2) - address of begining of msg | |
mov r8, lengthof msg ; R8 (arg 3) - msg length in bytes to terminating 0 | |
lea r9, nBytesWritten ; R9 (arg 4) - address of variable to write result to | |
call WriteConsoleA ; call the function (no return) | |
; restore the console text attributes | |
; void SetConsoleTextAttribute(RCX, RDX) | |
mov rcx, stdout ; RCX (arg 1) - file handle | |
mov rdx, 07h ; RDX (arg 2) - bgcolor = 0x0 (Black), fgcolor = 0x7 (Grey) | |
call SetConsoleTextAttribute ; | |
; clean up and exit | |
add rbp, 40 ; release the 40-bytes from the stack | |
mov rax, nBytesWritten ; set the return code to number of bytes written | |
mov rcx, 0 ; set the exit code 0 | |
call ExitProcess ; Jump to ExitProcess function | |
main endp | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment