Your First Program
In this lesson you will understand every line of a complete assembly program: one that prints text and then exits cleanly. Along the way you will meet system calls, the only way a program can talk to the outside world.
section .data
message: db "Assembly is fun!", 10
msg_len equ $ - message
section .text
global _start
_start:
mov eax, 1 ; syscall number: write
mov edi, 1 ; argument 1: file descriptor (1 = stdout)
mov rsi, message ; argument 2: address of the bytes
mov edx, msg_len ; argument 3: number of bytes
syscall
mov eax, 60 ; syscall number: exit
xor edi, edi ; argument 1: exit status (0 = success)
syscallAssembly is fun!
Line by line
section .data starts the data section, where initialized variables live.
message: db "Assembly is fun!", 10 defines a label message (an address) followed by bytes: the characters of the string, then the byte 10, which is the newline character (\n). db means define bytes. Note that there is no terminating zero: system calls take explicit lengths.
msg_len equ $ - message defines a constant. $ means "the current address" — right after the last byte of the string — so $ - message is the string's length, computed by NASM. If you change the text, the length updates automatically.
section .text starts the code section.
global _start exports the label _start so the linker can see it. The linker uses _start as the program's entry point.
_start: is where execution begins.
System calls
A program runs in user mode and may not touch hardware such as the terminal. To get something done it asks the kernel through the syscall instruction. On x86-64 Linux the rules are:
| Register | Meaning |
|---|---|
rax |
the system call number |
rdi, rsi, rdx, r10, r8, r9 |
arguments 1 to 6 |
rax (after) |
the result, or a negative error code |
rcx, r11 |
destroyed by syscall |
The two calls you need first:
| Number | Name | C equivalent | Arguments |
|---|---|---|---|
| 1 | write |
write(fd, buf, count) |
rdi = file descriptor, rsi = buffer address, rdx = byte count |
| 60 | exit |
_exit(status) |
rdi = exit status (0–255) |
File descriptor 1 is standard output and 2 is standard error.
Always exit
There is no "end of program" in machine code. After the last instruction you wrote, the CPU just keeps going and executes whatever bytes come next. Without the exit call the program runs off the end of its code and crashes:
section .data
message: db "About to fall off the end...", 10
msg_len equ $ - message
section .text
global _start
_start:
mov eax, 1
mov edi, 1
mov rsi, message
mov edx, msg_len
syscall
; forgot the exit system call!About to fall off the end...
Program terminated with signal SIGSEGV (11)
That is why every program in this course ends with mov eax, 60 / syscall.
Several lines of output
Each write call prints exactly the bytes you point it at. You can print several strings one after another, or put several lines into one string:
section .data
title: db "Shopping list", 10
title_len equ $ - title
items: db "- bread", 10, "- milk", 10, "- apples", 10
items_len equ $ - items
section .text
global _start
_start:
mov eax, 1
mov edi, 1
mov rsi, title
mov edx, title_len
syscall
mov eax, 1 ; rax was overwritten with write's result,
mov edi, 1 ; so set every argument again
mov rsi, items
mov edx, items_len
syscall
mov eax, 60
xor edi, edi
syscallShopping list
- bread
- milk
- apples
Note: After
syscall,raxholds the return value — forwritethe number of bytes written — andrcxandr11are overwritten. Always reloadraxbefore the next system call.
Looking at the return value
Since write returns the number of bytes written in rax, we can print it with the print_uint helper (explained in Printing Numbers):
section .data
word_hi: db "Hi!", 10
section .text
global _start
_start:
mov eax, 1
mov edi, 1
mov rsi, word_hi
mov edx, 4
syscall ; prints "Hi!\n", returns 4 in rax
mov rdi, rax
call print_uint
mov eax, 60
xor edi, edi
syscall
; ---- print_uint: print rdi as an unsigned decimal number and a newline ----
print_uint:
mov rax, rdi ; rax = the number to convert
sub rsp, 32 ; room for up to 20 digits and a newline
lea rsi, [rsp + 31] ; fill the buffer from its end
mov byte [rsi], 10 ; the newline is the last character
mov ecx, 10
.next_digit:
xor edx, edx ; the dividend is rdx:rax, so clear rdx
div rcx ; rax = quotient, rdx = remainder (0..9)
add dl, '0' ; remainder -> ASCII digit
dec rsi
mov [rsi], dl ; store it in front of the previous one
test rax, rax
jnz .next_digit ; repeat until the quotient is 0
lea rdx, [rsp + 32]
sub rdx, rsi ; rdx = number of bytes to print
mov eax, 1 ; write(1, rsi, rdx)
mov edi, 1
syscall
add rsp, 32
retHi!
4
The exit status
The value in rdi for exit becomes the program's exit status. By convention 0 means success and anything else (1–255) signals an error. In a shell you can read it with echo $?:
$ ./first
Assembly is fun!
$ echo $?
0Scripts and build tools look at this number to decide whether a step succeeded, so return a non-zero status when your program fails.
Test yourself
Which register holds the system call number?
- rdi
- rax
- rsi
- rcx
What does $ - message compute right after the string definition?
- The address of message
- The length of the string in bytes
- The current line number
- The value of the first character
What happens if _start never calls exit?
- The program ends normally
- The assembler adds an exit automatically
- The CPU executes whatever bytes follow and the program usually crashes
- The kernel waits forever