Time to write a complete program from nothing. You met every piece in Assembling & Linking and System Calls; this challenge is about putting them together yourself, start to finish.
Write an assembly program that prints a short message to the screen and then exits cleanly — using only the write and exit system calls, no C library. Assemble it with nasm, link it with ld, and run it.
New to this? Open one hint at a time before reading the walk-through.
section .data and give it a label so you can refer to its address.write is syscall number 1. Its three arguments — file descriptor, buffer address, byte count — go in rdi, rsi, rdx. Standard output is file descriptor 1.len equ $ - msg sets len to the current address minus the string's start, i.e. its length in bytes.60 (exit), status in rdi. If you skip it, execution runs off the end of your code into whatever bytes follow and crashes.The program splits into two sections. section .data holds the message, and section .text holds the instructions. The label _start must be marked global because it is the entry point the linker hands to the kernel.
The body is just two system calls. The first is write(1, msg, len): load 1 into rax (the syscall number), 1 into rdi (stdout), the address of the message into rsi, its length into rdx, then syscall. The second is exit(0): 60 into rax, 0 into rdi, and one more syscall. The only trick is the length — rather than hard-coding 14, the equ $ - msg idiom lets the assembler measure the string so the count can never drift out of sync with the text.
1section .data
2 msg db "Hello, world!", 10 ; 10 is the newline byte
3 len equ $ - msg ; assembler computes the length
4
Assemble, link, and run it — then check the exit status:
./hello; echo $?Hello, world!0The message prints, and the 0 from echo $? is the exit status _start passed to exit — a clean finish, not a crash.