Making the request
A program cannot run kernel code itself. Instead it makes a system call: a request that names a service by number, carries a few arguments, and enters the kernel through the system call entry point. The kernel does the work and sends back an answer.
write() looks like any function, and it is one: a small function in the C library (libc), running in user mode. Its job is to turn your call into a request the kernel understands.
libc puts the request in CPU registers. rax gets 1, write's number on x86-64 Linux. rdi, rsi and rdx get the three arguments: file descriptor 1 (a small number naming an open file), where the bytes are, and 3. Only numbers cross over, never names.
Inside the kernel
libc runs the syscall instruction. In one move the CPU switches to kernel mode and jumps to the entry point the kernel chose at boot. The entry code first saves the program's registers, so it can carry on later exactly where it was.
The kernel reads rax, 1, and looks it up in its system call table, a list of handlers in number order. Entry 1 is write. A number with no entry is refused with an error, so a program can only reach real services.
The handler trusts nothing. Is file descriptor 1 in this program's table of open files? Yes, it is standard output. Is the buffer in the program's own memory? Yes, so the kernel copies the 3 bytes in safely.
Now the real work: the kernel hands h, i and a newline to the screen's driver, and hi appears. Only the kernel may touch the device, which is the whole reason the program had to ask.
The kernel writes the result, 3 bytes written, into the saved rax, restores the registers and runs sysret. The CPU drops back to user mode, after the syscall instruction. libc sees a number that is not negative and returns 3, so n = 3.
When it fails
Line 2 makes the same call with file descriptor 9. The trip is the same: registers, trap, table, handler. But fd 9 is not in the table of open files, so the handler refuses before it does anything.
The kernel returns a negative number, -9: minus EBADF, bad file descriptor. Back in user mode, libc sees it is negative. It stores 9 in the variable errno and returns -1, the C way of saying it failed. So m = -1.
m is negative, so line 3 calls perror. It reads errno, 9, finds the message Bad file descriptor, and prints write: Bad file descriptor. To print, perror makes system calls of its own, to fd 2, standard error.
Every program talks to the kernel this way; in Python, os.write(1, b"hi\n") makes the same call. A number picks a handler from a table, the handler checks its arguments, and a negative answer becomes -1 plus errno.
What happens
;; step 1/12 · ask
the whole program (C99, POSIX):
#include <stdio.h>
#include <unistd.h>
int main(void) {
ssize_t n, m;
n = write(1, "hi\n", 3);
m = write(9, "hi\n", 3);
if (m < 0) perror("write");
return n == 3 ? 0 : 1;
}
line 1 a write that works
line 2 the same write to a file descriptor that is not open
line 3 report the error
The call number 1 for write, the registers rax, rdi, rsi and rdx, and the instructions syscall and sysret are for Linux on x86-64. Other systems use other numbers: on 64-bit ARM Linux write is 64, and xv6 on RISC-V puts the number in a7 and traps with ecall. The idea is the same everywhere. The table shows only the first three entries. Recent Linux kernels on x86-64 compile the table into a switch statement rather than looking it up in an array, but it does the same job: number in, handler out.