Interview Prep Zoneby CuriouserLabs

Question 1 of 5 · senior level

How does a CPU actually execute a program? Walk through the fetch–decode–execute cycle.

🎤 Say this first

A program is bytes in memory; the program counter holds the address of the next instruction. Forever: fetch the instruction at PC (through the instruction cache) and advance PC; decode the bits into control signals (which operation, which registers); execute in the ALU / address unit; write back the result to a register (memory access as its own stage for loads/stores). That loop, at ~5GHz, is all a computer is. Everything else in architecture — pipelines, caches, prediction — exists to keep this loop from waiting.

The full picture

🎬 How a CPU runs your code: fetch → decode → execute

1 / 5
Memory (instructions)0x1000: ADD R1,R20x1004: LOAD R3,…0x1008: CMP R1,R30x100C: JNE loopCPUPC: 0x1000IR: (empty)Decodercontrol signalsRegistersR1=5R2=7ALU+ − × ÷ ∧ ∨Everything a computer does reduces to this loop — the rest of architecture is making it not wait.

A program is just bytes in memory. The program counter (PC) holds the address of the next instruction: here 0x1000, an ADD. The CPU loops forever: fetch → decode → execute.

One ADD instruction through the loop — then the pipeline punchline.
  • Registers are the top of the memory hierarchy: a few dozen named slots (~0.3ns) — the compiler's register allocator decides which of your variables live there; 'spilling' to stack is a real performance event.
  • Control flow is just PC arithmetic: a branch writes the PC; a call pushes a return address and jumps; a return pops it — which is why the call stack, stack overflows and ROP attacks all make mechanical sense.
  • Java bridge: bytecode is not this — the JVM interprets it, then the JIT compiles hot paths to exactly these machine instructions. 'What does the machine run?' has a two-layer answer on the JVM, and interviewers enjoy hearing you keep the layers straight.
  • Von Neumann's essence: instructions and data share one memory — a program can be data (JIT output is data becoming code; W^X pages exist to control precisely that).

🔄 Likely follow-up questions

  • What happens to this loop on an interrupt?
  • Where do the stack pointer and frame pointer fit in?
  • How does a JIT turn bytecode into these instructions safely at runtime?