Execution End to End
How It All Connects · Assembly & OS track
4 min read
In this module
Tracing a Program from Source to CPU
Let's pull everything together by chasing a simple C program through every layer of the system. Picture a tiny program that opens a file, reads its contents, and prints them to the screen. We'll follow it from source code all the way down to electrical signals flicking through the CPU.
Step 1: Compilation
You write the program in C and run the compiler. The compiler translates your high-level code into assembly. The assembler turns that into machine code. The linker fuses your object file with bits of the C standard library and writes out a final ELF executable, sitting there on disk.
Step 2: Loading
You type ./program in the terminal. The shell calls fork() to spawn a child process, then exec() to replace the child with your program. The kernel reads the ELF headers, carves out a virtual address space, maps the code and data segments into memory and, for a dynamically linked program, starts the dynamic linker, which loads the shared libraries and jumps to your program's entry point.
Step 3: Fetch-Decode-Execute
The CPU starts its cycle. Fetch the instruction at the program counter from memory (via the cache hierarchy). Decode it to work out what operation to perform and which operands to use. Execute in the ALU or some other functional unit. Advance the program counter, repeat. Billions of times per second.
There's a step hiding inside that fetch. The program counter holds a virtual address, the kind the kernel set up in Step 2, and the memory system only understands physical ones. So before every fetch (and every load or store) the MMU translates the address: it checks the TLB, a small cache of recent translations, and on a miss walks the page tables the kernel built for this process. Step through a walk below. It's a shrunk-down two-level version with 4-bit indexes so it fits on screen; real x86-64 uses four levels of 9 bits each.
Page Table Walker (2-level, 4096B pages)
Translate the same address twice and the second lookup hits in the TLB, skipping the walk entirely. That's why a tight loop running from one page of code pays for translation only once.
The very first touch of each page is different. Step 2 only mapped the file, so there's no physical page behind the address yet. The MMU raises a page fault, the kernel reads that page of the executable from disk (or the page cache), fills in the page table entry, and restarts the instruction. Programs start fast because they only pay for the pages they actually use.
Inside a single instruction, the entire stack is at work: transistors switching to form logic gates, gates building up the ALU and control unit, the control unit orchestrating the fetch-decode-execute cycle, registers holding operands, the cache supplying fast memory access, and the OS having set up the virtual address space that makes any of it possible.
Step 4: System Calls for I/O
When the program calls open(), execution hits a syscall instruction. The CPU traps into kernel mode. The kernel's filesystem code locates the file on disk, talks to the storage driver, and returns a file descriptor. The same dance repeats for read() (the kernel pulls data from disk into the program's buffer) and write() (the kernel pushes the buffer's contents at the terminal device).
Every syscall involves a mode switch from user space to kernel space and back, with the kernel sitting between your code and the hardware.
Step 5: Completion
When the program is done, it calls exit(). The kernel reclaims its memory and closes its file descriptors, but keeps a small process-table entry holding the exit status (a zombie, for a moment). The parent shell's wait() collects that status, the entry is freed, and your prompt blinks back into view.
The Full Journey
Source code became machine code through compilation. The OS loaded it into memory. The CPU executed it instruction by instruction. System calls bridged the gap between user code and hardware. Every module you've studied so far — binary, logic gates, the ALU, memory, the OS, syscalls — quietly did its bit to make a tiny program run.