← All posts
7 min read

What the CPU Actually Does: Fetch, Decode, and Execute

#technology#computer-science#cpu#hardware
📑 On this page

The CPU is often called the brain of a computer. That phrase suggests intelligence, but a processor does something more precise and mechanical:

A CPU repeatedly fetches encoded instructions, decodes their meaning, executes their operations, and updates the machine's state.

Billions of simple operations, carefully organized by software, produce the applications we recognize.

Programs become machine instructions

Developers usually write source code in languages such as JavaScript, Python, Rust, or C#. A processor does not directly execute those words.

Compilers, interpreters, and runtime systems eventually produce or select machine instructions supported by the processor's instruction set architecture.

Instructions can request operations such as:

  • Load a value from memory
  • Store a value
  • Add or subtract
  • Compare two values
  • Perform bit operations
  • Jump to another instruction
  • Call a reusable routine

The instruction set is a contract between software and processor design. Different CPU families may encode and organize instructions differently.

The fetch-decode-execute cycle

A simplified instruction cycle has three stages.

Fetch

The CPU obtains the next instruction from memory, usually through caches. A special value called the program counter tracks where the next instruction is located.

Decode

Control circuitry interprets the instruction's encoded fields. It determines which operation is requested, which registers are involved, and whether additional data is needed.

Execute

The appropriate processor unit performs the operation. The CPU may calculate a result, compare values, access memory, or change the program counter.

Modern processors overlap and reorder portions of many instructions, but this cycle remains the conceptual foundation.

Registers: the CPU's immediate workspace

Registers are tiny, extremely fast storage locations inside the processor.

An instruction might:

  1. Load two numbers into registers.
  2. Ask an arithmetic unit to add them.
  3. Place the result in another register.
  4. Store the result into memory.

Registers are much smaller than RAM. They hold values needed immediately: numbers, addresses, intermediate results, status information, and control state.

Because register access is fast, compilers try to keep frequently used values there. If too many active values compete for limited registers, some must temporarily move to memory.

Arithmetic, logic, and comparisons

An arithmetic logic unit, or ALU, performs integer arithmetic and logical operations.

Examples include:

7 + 5
A AND B
value < limit
bits shifted left

Processors also contain units for floating-point calculations, vector operations, address generation, cryptography, and other specialized work.

A spreadsheet formula such as:

subtotal = price × quantity

eventually becomes a series of lower-level actions: retrieve values, multiply them using an appropriate numerical format, and store the result.

Branches create decisions

Programs need conditions:

if password is correct:
    allow access
else:
    reject access

At machine level, the CPU compares values and conditionally changes which instruction comes next. This is a branch.

Loops also rely on branches. After one iteration, the processor checks whether to jump back and repeat.

Modern CPUs predict branch outcomes so they can prepare likely future instructions early. A correct prediction saves time. A wrong prediction discards speculative work and restarts along the correct path.

Branch prediction does not decide the application's logic. It predicts execution flow for performance while preserving the program's defined result.

Memory access is expensive

The CPU can operate far faster than main memory can provide arbitrary data. A processor that waited for RAM after every instruction would waste much of its capability.

Small, fast caches keep recently or nearby data close to the cores. Caches are commonly organized into levels:

  • L1: smallest and fastest
  • L2: larger and slightly slower
  • L3: larger again and often shared among cores

Programs benefit when they reuse nearby data, a property called locality.

For example, processing an array in order is often cache-friendly because adjacent values arrive together. Jumping unpredictably among distant memory locations can cause more cache misses.

A concrete example: recalculating a total

Imagine a spreadsheet containing prices and quantities:

  1. The spreadsheet program receives an edit.
  2. It identifies formulas affected by the change.
  3. CPU instructions load values from memory.
  4. Arithmetic instructions multiply price by quantity.
  5. Additional instructions add the row totals.
  6. The results are written back to memory.
  7. Graphics instructions help update visible cells.

The CPU does not know the business meaning of "price." It manipulates encoded values according to the spreadsheet software.

Meaning exists at higher layers. The processor provides dependable mechanisms.

Pipelines and instruction-level parallelism

Different parts of an instruction take time. Instead of completing one instruction fully before beginning another, CPUs use a pipeline.

This resembles a production line:

  • One instruction is being fetched.
  • An earlier one is being decoded.
  • Another is executing.
  • Another is writing its result.

Modern processors can begin several instructions per cycle, execute them out of order when dependencies permit, and then retire results in a way that preserves correct program behavior.

The visible program appears sequential even though the hardware performs substantial internal scheduling.

Interrupts and operating-system control

The CPU must react to events such as:

  • A key press
  • Network data arrival
  • A storage operation completing
  • A timer expiring

Hardware can signal an interrupt. The CPU pauses its current sequence at a controlled point, saves necessary state, and runs an operating-system handler.

The operating system also uses timers to regain control and switch among processes. This prevents one ordinary program from occupying a core forever.

The CPU does not work alone

Other components handle specialized responsibilities:

  • The GPU performs wide parallel computation.
  • Storage controllers manage device operations.
  • Network adapters send and receive frames.
  • Direct memory access can move blocks of data without making the CPU copy every byte.

The CPU configures and coordinates much of this work, then responds when devices complete operations.

Good systems avoid using expensive CPU attention for tasks that dedicated hardware can handle efficiently.

Common misunderstandings

"The CPU understands application concepts"

It executes machine instructions. Concepts such as invoice, photograph, or message are created by software structures and human interpretation.

"One clock cycle equals one instruction"

Modern CPUs overlap instructions, and different instructions require different resources and timing. Several may begin in one cycle, while others take many cycles to complete.

"The CPU directly reads every byte from the SSD"

Storage controllers and operating-system software manage transfers, usually moving data through memory before the CPU processes it.

"A faster CPU fixes every slow application"

Performance may be limited by memory, storage, network, GPU, synchronization, or inefficient software.

Knowledge check

1. What are the three conceptual stages of the instruction cycle?

Fetch the instruction, decode its meaning, and execute the requested operation.

2. Why are registers useful?

They give processor instructions extremely fast access to a small set of immediate values and control state.

3. What does a conditional branch do?

It changes which instruction executes next based on a condition, enabling decisions and loops.

4. Why do CPUs use caches?

Main memory is much slower than processor execution. Caches keep likely-needed instructions and data closer to the cores.

The one idea to remember

The CPU follows tiny machine instructions, using registers, execution units, caches, and control flow to transform state at enormous speed.

The intelligence of an application comes from how software organizes those operations, not from a processor understanding their human meaning.

Next, we will examine why clock speed and core count each reveal only part of CPU performance.