Putting It Together
Part of the Fundamentals section of Coddy's Assembly journey — lesson 39 of 45.
You have learned all four pieces of the write syscall:
| Register | What it holds | Value for screen output |
|---|---|---|
rax | Syscall number | 1 (write) |
rdi | File descriptor | 1 (stdout) |
rsi | Buffer address | Address of your string |
rdx | Byte count | Length of your string |
Now it is time to put them all together.
The complete write syscall pattern:
mov rax, 1 ; syscall number for write
mov rdi, 1 ; file descriptor (stdout)
mov rsi, msg ; address of the string
mov rdx, msgLen ; number of bytes to print
syscall ; ask the OS to printA complete working program:
section .data
msg db 'Hello, World!', 0x0a
msgLen equ $ - msg
section .text
global _start
_start:
mov rax, 1
mov rdi, 1
mov rsi, msg
mov rdx, msgLen
syscall
mov rax, 60
mov rdi, 0
syscallWhat this program does:
| Step | What happens |
|---|---|
| 1 | Prints "Hello, World!" to the screen |
| 2 | Moves to a new line |
| 3 | Exits cleanly with code 0 |
The order matters:
You must set up the registers before executing syscall. The order of the mov instructions does not matter as long as all four are set before syscall.
Challenge
Write a complete program that prints the message "Assembly is fun" followed by a newline, then exits cleanly.
The string and its length are already defined. You only need to write the code inside _start:.
Try it yourself
section .data
msg db 'Assembly is fun', 0x0a
msgLen equ $ - msg
section .text
global _start
_start:
; TODO: Set up and call the write syscall
; Use rax=1, rdi=1, rsi=msg, rdx=msgLen
; TODO: Exit cleanly with code 0
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Fundamentals
1The Machine
What is CPUMemoryRegisters vs MemoryHow instructions workTwo-File PatternThe x86-64 Architecture3First instruction - MOV
What is MOV?MOV ImmediateMOV Register to RegisterMOV Memory to RegisterMOV Register to MemoryLEA - Load AddressMOV with Different SizesPractice: Swap RegistersRecap: MOV Challenge6Write Syscall
Write Syscall (1)File Descriptor (rdi)Buffer Address (rsi)Byte Count (rdx)Putting It TogetherPrinting Two StringsNewline MattersRecap: Write Setup