How CPUs Execute Instructions: Fetch Decode Execute Cycle

Introduction

Every program you write eventually becomes a stream of machine instructions that the CPU must execute one after another. Understanding the fetch-decode-execute cycle is fundamental for any developer who wants to know how computers actually run code, why performance characteristics appear, and how compilers and operating systems interact with hardware.

This knowledge appears whenever you debug low-level issues, optimize hot paths, read assembly, or reason about concurrency and memory.

Simple Explanation

Think of the CPU as a chef following a recipe book stored in memory. The chef repeatedly does three things: looks up the next step (fetch), reads what the step says (decode), and performs the action (execute). Then moves to the next step. This loop continues as long as the computer is powered on.

How It Works Internally

The classic instruction cycle has three primary stages:

  • Fetch: The Program Counter (PC) holds the address of the next instruction. That address is placed in the Memory Address Register (MAR). Memory returns the instruction into the Memory Data Register (MDR) or Instruction Register (IR). The PC is then incremented.
  • Decode: The Control Unit examines the opcode (the part that says what to do) and the operands (registers or memory locations involved).
  • Execute: The Arithmetic Logic Unit (ALU) or other functional units perform the operation. Results may be written back to registers or memory. Branch instructions can update the PC to a different address.

ASCII flow:

PC → MAR → Memory → MDR/IR → Decode → Execute → (update registers / PC) → next cycle

Modern CPUs extend this with pipelining (multiple instructions in different stages at once), caches, branch prediction, and out-of-order execution, but the logical model remains the same.

Real-World Examples

When you compile a simple C function that adds two numbers, the compiler emits a sequence of load, add, and store instructions. Each of those travels through the cycle. Cache misses force the CPU to wait for main memory. Branch mispredictions flush the pipeline. These micro-architectural details explain many performance surprises developers encounter.

Code Examples

Conceptual instructions:

LOAD R1, 100     ; fetch value at address 100 into R1
LOAD R2, 104     ; fetch value at address 104 into R2
ADD  R3, R1, R2  ; R3 = R1 + R2
STORE R3, 108    ; write result to address 108

Each line above is fetched, decoded, and executed in turn (or overlapped in a pipeline).

Common Misconceptions

  • High-level languages are not executed directly; they are translated into these machine instructions.
  • One source line often expands into many machine instructions.
  • Modern CPUs are not purely sequential, yet the cycle is still the conceptual foundation.
  • Clock speed alone does not determine performance; instruction mix, cache behavior, and pipeline efficiency matter greatly.

Best Practices / Key Takeaways

  • Write locality-friendly code so data stays in caches.
  • Prefer predictable control flow to help branch predictors.
  • Study basic assembly of your language to see what the CPU actually receives.
  • Use profilers that report instruction-level and cache metrics.

FAQ

What is the fetch-decode-execute cycle?
The fundamental loop the CPU uses to process every machine instruction: retrieve it from memory, interpret it, perform the operation.

What is the Program Counter?
A special register that always holds the memory address of the next instruction to fetch.

What is an opcode?
The portion of an instruction that tells the CPU which operation to perform (add, load, jump, etc.).

Why do CPUs use pipelining?
To keep multiple stages busy at once and increase the number of instructions completed per unit of time.

Does every instruction take the same number of cycles?
No. Simple register operations are fast; memory accesses and complex operations take longer, especially on cache misses.

How does this relate to machine code?
Machine code is exactly the binary encoding of the instructions the CPU fetches and executes.

Related Articles

  • Compiler vs Interpreter Explained
  • Source Code vs Machine Code Explained
  • Memory Management Explained
  • How Operating Systems Work
  • How Programming Languages Work Internally
Next Post Previous Post