A program cannot touch the outside world on its own. Printing text, reading a file, allocating memory, exiting — all of it belongs to the operating system, and the only way to ask for it is a system call. Every "Hello, world!" you have assembled in this course already made two of them: one to write the text, one to exit.
Your code runs unprivileged: it can do arithmetic and shuffle its own memory, but it cannot speak to hardware. The kernel runs privileged and can. A system call is the one controlled doorway between the two. You fill in registers describing the request, execute a single instruction that traps into the kernel, and the kernel validates the request, does the work, and hands control back with a result. It is the boundary between your program and the machine.
On x86-64 Linux the request is laid out in registers, then the syscall instruction fires:
rax holds the syscall number — which call you want.rdi, rsi, rdx, r10, r8, r9, in that order.rax (a small negative value means an error, -errno).That list is almost the function calling convention from Stack & Calls, with one deliberate difference: the fourth argument uses r10, not rcx, because the syscall instruction itself overwrites rcx. Here is the write call inside hello, caught the instant before it fires:
break *0x401019Breakpoint 1 at 0x401019runBreakpoint 1, 0x0000000000401019 in _start ()p/d $rax$1 = 1p/d $rdi$2 = 1p/x $rsi$3 = 0x402000p/d $rdx$4 = 14Read it straight off the convention: rax = 1 is the write syscall, rdi = 1 is the file descriptor (standard output), rsi = 0x402000 is the address of the string, and rdx = 14 is its length. Letting the program run makes the call for real:
./helloHello, world!A handful of syscall numbers come up constantly:
| Number | Name | Does |
|---|---|---|
0 | read | read bytes from a file descriptor |
1 | write | write bytes to a file descriptor |
2 | open | open a file |
3 | close | close a descriptor |
9 | mmap | map memory |
59 | execve | replace the process with a new program |
60 | exit | terminate the process |
int 0x80Before syscall existed, 32-bit x86 entered the kernel with a software interrupt, int 0x80, and you will still meet it in 32-bit shellcode and older binaries. Two things change. The registers are the 32-bit set — number in eax, arguments in ebx, ecx, edx, esi, edi — and, less obviously, the syscall numbers are completely different: in the 32-bit table write is 4 and exit is 1, not 1 and 60. Mixing a 64-bit number into a 32-bit call (or the reverse) calls something entirely unintended. The modern syscall path is faster, since it avoids the interrupt machinery, and is the right choice for 64-bit code; the 32-bit world is covered in 32-bit x86 (i386).
The numbers live in the kernel headers — unistd_64.h for the 64-bit table, unistd_32.h for the 32-bit one — and, as the two values for write show, you must look in the right table for the mode you are in.