Day 1 of 5
⏱ ~60 minutes
Computer Architecture in 5 Days — Day 1

CPU Fundamentals

Before you optimize software, you need to know what hardware is doing. Today: ALU, registers, control unit, and the fetch-decode-execute cycle.

The Four Parts of a CPU

The CPU has four core components: the ALU (does arithmetic and logic), registers (tiny ultra-fast storage, ~1 cycle access), the control unit (decodes instructions, coordinates execution), and the clock (synchronizes everything). Modern x86-64 CPUs have 16 general-purpose 64-bit registers. Accessing a register takes 1 clock cycle — the fastest possible memory.

Instruction Set Architecture

The ISA is the contract between software and hardware — a list of every instruction the CPU understands, how to encode them in binary, and what they do. Two dominant ISAs: x86-64 (Intel/AMD, used in desktops and servers) and ARM (Apple Silicon, mobile). Every piece of code you write eventually becomes a sequence of ISA instructions.

The Fetch-Decode-Execute Cycle

Every CPU runs one loop forever: Fetch the next instruction from memory (using the Program Counter register), Decode it to determine the operation and operands, Execute it in the ALU, Writeback the result to a register or memory, then advance the PC. Modern CPUs pipeline and parallelize these stages — but the logical model is always this loop.

asm
; x86-64 Assembly: what your code compiles to
; gcc -O0 -S hello.c generates this

section .text
    global _start
_start:
    mov rax, 10       ; load 10 into register RAX   (1 cycle)
    mov rbx, 32       ; load 32 into register RBX   (1 cycle)
    add rax, rbx      ; RAX = RAX + RBX = 42         (1 cycle)
    cmp rax, 42       ; set flags: ZF=1 if equal
    je  done          ; jump if ZF=1
done:
    mov rdi, 0        ; exit code 0
    mov rax, 60       ; syscall number: exit
    syscall
💡
Compile any C file with gcc -O0 -S file.c to see the assembly. Every C statement becomes a handful of mov/add/cmp instructions. This is what the CPU actually runs.
📝 Day 1 Exercise
Trace a C Program Through the CPU
  1. Write a simple C program: int a=5,b=7,c=a+b; printf("%d\n",c);
  2. Compile with gcc -O0 -S program.c and open the .s output file
  3. Find the add instruction. What registers hold a and b?
  4. Recompile with -O2 and compare — the optimizer often reduces 10 instructions to 3
  5. Count total instructions in both versions. This is how compilers think about your code

Day 1 Summary

  • CPU = ALU + registers + control unit + clock — these 4 parts run every program ever written
  • Registers are the fastest storage: 1 cycle, 16 general-purpose on x86-64
  • The ISA is the hardware/software contract — x86-64 for servers, ARM for mobile/Apple
  • Every line of high-level code eventually becomes a sequence of simple ISA instructions
Challenge

Install godbolt.org (Compiler Explorer) — paste a C function and watch the assembly update in real time. Try optimizing the C code by hand: remove a branch, simplify math. See how each change affects the assembly output.

Finished this lesson?