This is my first ASM project. It is a simple program that prints "Hello, World!" to the console.
- Download or Clone using
Github DesktoporGit - Open in
vscodeor whatever editor you use - Open folder in
terminal/cmd prompt - Run the following commands:
> nasm -f win32 index.asm
> gcc index.obj
> a.exe- You should see
"Hello, World!"in the console
This program uses the Windows API to print to the console. It uses the following instructions:
default rel ; use relative addressing
global main ; make main function visible to linker
extern printf ; import printf function from msvcrt.dll
extern exit ; import exit function from msvcrt.dll
section .data
message: db "Hello, World!", 0 ; the format string for printf
section .text
main:
sub rsp, 28h ; allocate space for printf arguments
lea rcx, [message] ; load the address of the format string into rcx
call printf ; call printf(message)
xor edi, edi ; set exit code to 0
call exit ; call exit(0)
add rsp, 28h ; deallocate space for printf arguments
hlt ; halt the programdefault rel- This tells the assembler to use relative addressing by default. This is important because the Windows API uses relative addressing.global main- This makes themainfunction visible to the linker. This is required for the program to run.extern printf- This imports theprintffunction frommsvcrt.dll. This is the function we will use to print to the console.extern exit- This imports theexitfunction frommsvcrt.dll. This is the function we will use to exit the program.section .data- This is the data section where we define our variables.message: db "Hello, World!", 0- This defines a null-terminated stringmessagewith the value"Hello, World!". The0at the end is the null terminator required byprintf.section .text- This is the code section where we define our instructions.main:- This is the entry point of our program.sub rsp, 28h- This allocates space on the stack for the arguments toprintf. The28his the hexadecimal value for28, which is the number of bytes we need to allocate.lea rcx, [message]- This loads the address of themessagestring into thercxregister. This is the first argument toprintf.call printf- This calls theprintffunction with themessagestring as the argument.xor edi, edi- This sets the exit code to0. This is the return value of the program.call exit- This calls theexitfunction with the exit code0.add rsp, 28h- This deallocates the space on the stack that we allocated earlier.hlt- This halts the program. This isn't needed, but it's good practice.