Skip to main contentSkip to navigation
ThisIsHowItWorks.in

Complex systems, clearly explained.

An independent visual publication explaining the invisible protocols, networks, infrastructure, and mechanisms that run our world.

Explainers

  • How UPI Works
  • Offline UPI Mechanisms
  • All Explainers (Archive)
  • Topics & Roadmap
  • Search Index

Publication

  • About Publication
  • Editorial Principles
  • Changelog
  • RSS / Atom Feed

Legal & Contact

  • Privacy Policy
  • Terms of Use
  • Editorial & Legal Notice
  • Contact Us

Connect

  • Instagram
  • Discord Community
© 2026 ThisIsHowItWorks.in. All rights reserved.
Durable technical understanding built from first principles.
ThisIsHowItWorks.in
ExploreTopicsAbout
  1. Home
  2. /Topics
  3. /computing
  4. /Computing & Digital Architecture
  5. /Computing & Digital Architecture
  6. /How a CPU Executes an Instruction
Computing · computing/ Explainer

How a CPU Executes an Instruction

The microscopic journey of a clock tick: how registers, program counters, instruction decoders, and arithmetic units turn memory bytes into physical computation

Updated for clarity
The Short AnswerFirst-Principles Core

“When software code says 'c = a + b', what physical pathway does an electrical signal actually travel through inside a microprocessor chip?”

Every software application on Earth—from a simple calculator to an operating system kernel, a web browser rendering pixels, or an artificial intelligence generating text—eventually compiles down into a stream of raw binary numbers stored in memory. A central processing unit (CPU) is a physical machine designed to ingest those numbers and execute them at the rate of three billion cycles per second. There is no interpreter inside the silicon. A CPU is a clocked assembly line of physical logic gates, storage registers, and copper bus wires. In the span of a fraction of a nanosecond, an address is placed on a memory bus, binary opcode bytes are latched into an instruction register, a decoder activates control wires, an Arithmetic Logic Unit adds two electrical potentials together, and the result is stamped into a storage register. By understanding the five stages of the classic CPU pipeline—Fetch, Decode, Execute, Memory, and Writeback—the mystery of how hardware runs software dissolves into pure, elegant physical engineering.

Recommended Background

To understand the failure modes and edge cases detailed in this piece, we recommend familiarizing yourself with these foundational mechanisms first:

How Binary and Logic Gates Became Computation
Understanding How Binary and Logic Gates Became Computation is required before reading How a CPU Executes an Instruction
How Clocks Actually Measure Time
Understanding How Clocks Actually Measure Time is required before reading How a CPU Executes an Instruction
In this Explainer8 Sections

When you write a line of computer code such as:

total = price + tax

it feels abstract, mathematical, and weightless. You press a button, and within a microsecond, the calculation is done.

Yet inside the silicon microprocessor beneath your keyboard, nothing is abstract.

To execute that single line of code, thousands of physical electrical signals surged across microscopic copper bus wires. Millions of electrons flowed through semiconductor logic gates. Storage registers latched electrical charges, an Arithmetic Logic Unit resolved binary potentials, and the result was stamped into memory.

A computer processor is not a brain. It is an ultra-fast, mechanical assembly line synchronized by an electronic clock.

In How Binary and Logic Gates Became Computation, we discovered how physical transistors combine to create logic gates (AND, OR, NOT, XOR) and arithmetic adders.

Now, we step up to the next architectural summit: How do those logic gates assemble into a complete, self-directed Central Processing Unit (CPU) capable of executing computer software?

                     THE FIVE STAGES OF THE CPU INSTRUCTION PIPELINE
  
  1. Instruction Fetch (IF)  2. Instruction Decode (ID) 3. Execute / ALU (EX)    4. Memory Access (MEM)   5. Register Writeback (WB)
    Program Counter (PC)      Opcode decoded into        ALU computes arithmetic   Loads from or stores     Result stamped into
    fetches bytes from RAM    physical control signals   result across adders      data to L1/RAM cache     destination register

The diagram below traces the five-stage pipeline through which a modern CPU executes an instruction:

The Five-Stage Microprocessor Instruction Pipeline (Fetch to Writeback)
01
Instruction Fetch(IF)

Program Counter places memory address on address bus; instruction bytes loaded from L1 cache into Instruction Register; PC increments.

→
02
Instruction Decode(ID)

Control unit decodes opcode bits; activates internal control lines; reads operand values from source registers.

→
03
ALU Execution(EX)

Arithmetic Logic Unit routes operand voltages through binary adders and logic gates to compute result or memory address.

→
04
Memory Access(MEM)

If instruction requires RAM access (LOAD/STORE), data bus transfers bytes between CPU and L1 cache; bypassed for register arithmetic.

→
05
Register Writeback(WB)

Final computation result latched into destination register; flags updated; circuit readies for next clock cycle.

Pipeline diagram tracking the execution of a machine instruction through the five stages of a modern CPU pipeline: Instruction Fetch, Instruction Decode, ALU Execution, Memory Access, and Register Writeback.

1. The Machine Code Illusion: What an Instruction Actually Is

Software does not run on hardware directly.

Before your computer can execute total = price + tax, a compiler program (such as GCC, Clang, or Python's bytecode engine) translates that human statement into machine language—a sequence of raw binary numbers.

Consider a simple assembly language instruction for an ARM or RISC-V processor:

ADD R1, R2, R3

This instruction tells the CPU: "Take the number stored in Register 2, add it to the number stored in Register 3, and save the result into Register 1."

When this instruction is compiled into binary machine code, it becomes a 32-bit number:

               THE ANATOMY OF A 32-BIT MACHINE INSTRUCTION (RISC-V)
  
       Opcode (7 bits)   rd (5 bits)   funct3 (3)   rs1 (5 bits)   rs2 (5 bits)   funct7 (7)
      ┌───────────────┬─────────────┬────────────┬──────────────┬──────────────┬──────────────┐
      │    0110011    │    00001    │    000     │    00010     │    00011     │   0000000    │
      └───────────────┴─────────────┴────────────┴──────────────┴──────────────┴──────────────┘
             ▲               ▲                          ▲              ▲
             │               │                          │              │
        "I am an ADD"   Destination:               Source Reg 1:  Source Reg 2:
                        Register R1                Register R2    Register R3

Notice what a machine instruction really is: it is a physical wiring configuration template.

When those thirty-two bits enter the CPU, they are not read as text. They are applied directly to thirty-two physical copper wires as electrical voltages (0 volts for 0, and 1.2 volts for 1).

Those voltages physically configure the internal routing of the chip for the next clock cycle.


2. The Physical Anatomy of a CPU

Inside every microprocessor, regardless of whether it is an Intel x86 chip in a server or an Apple ARM chip in an iPhone, you will find five core organs:

               THE INTERNAL BLOCK DIAGRAM OF A SIMPLE CPU
  
                        ┌──────────────────────────────┐
                        │      PROGRAM COUNTER (PC)    │ ── (Holds Memory Address)
                        └──────────────┬───────────────┘
                                       │
                                       ▼ Address Bus
                        ┌──────────────────────────────┐
                        │   INSTRUCTION MEMORY / L1    │ ── (RAM Cache)
                        └──────────────┬───────────────┘
                                       │
                                       ▼ Data Bus
                        ┌──────────────────────────────┐
                        │     INSTRUCTION REGISTER     │
                        └──────────────┬───────────────┘
                                       │
                                       ▼
                        ┌──────────────────────────────┐
                        │      INSTRUCTION DECODER     │ ──▶ Control Signals (Wire Activations)
                        └──────────────┬───────────────┘
                                       │
               ┌───────────────────────┴───────────────────────┐
               ▼                                               ▼
     ┌───────────────────┐                           ┌───────────────────┐
     │   REGISTER FILE   │ ── Operands A & B ──────▶ │  ARITHMETIC LOGIC │
     │  (R0, R1, R2...)  │                           │    UNIT (ALU)     │
     └─────────▲─────────┘                           └─────────┬─────────┘
               │                                               │
               └─────────────── Result Writeback Bus ──────────┘
  1. The Register File: A collection of ultra-fast internal memory cells (typically 16, 32, or 64 registers) located directly on the processor die. Each register is built from high-speed transistor flip-flops and can deliver a number in less than 200 picoseconds.
  2. The Program Counter (PC): A specialized register that stores the memory address of the instruction currently being executed.
  3. The Instruction Register (IR): A temporary holding latch that stores the binary instruction bytes fetched from memory.
  4. The Instruction Decoder (Control Unit): A combinational logic circuit that examines the opcode bits and switches on the appropriate electrical pathways inside the processor.
  5. The Arithmetic Logic Unit (ALU): The calculating muscle containing adders, subtractors, and bit-shifters (explored in How Binary and Logic Gates Became Computation).
  6. The Bus: Parallel lines of microscopic metal traces that transport addresses, instructions, and data between registers and the ALU.

3. The Heartbeat: Why CPUs Need a Clock

Before examining how an instruction runs, we must understand the processor's heartbeat: the clock.

As explored in How Clocks Actually Measure Time, quartz oscillators generate stable, periodic frequencies. Modern processors contain clock distribution trees that pulse a square wave between 0 volts and high voltage billions of times per second (e.g. $3.5 \text{ GHz} = 3.5 \text{ billion ticks/second}$):

               THE SQUARE-WAVE CPU CLOCK SIGNAL (3.5 GHz)
  
      Voltage
        ▲
        │    Rising Edge                     Rising Edge
   1.2V ┼       ┌─────┐                         ┌─────┐
        │       │     │                         │     │
     0V ┼───────┘     └─────────────────────────┘     └─────────▶ Time
        │       ◀───── 1 Clock Period: ~0.28 nanoseconds ──────▶

Why Can't a CPU Run Without a Clock?

Why not let the electrons flow continuously at maximum speed?

Because of propagation delay and race conditions.

When electrical signals travel through thousands of logic gates in an ALU, they do not arrive instantly. An electrical pulse takes roughly a few picoseconds to traverse a transistor. Because some paths have more gates than others, signals arrive at different times:

  • If a signal takes Path A through three gates, it arrives in 10 picoseconds.
  • If a signal takes Path B through twelve gates, it arrives in 40 picoseconds.

During those intermediate thirty picoseconds, the output wires of the adder are fluctuating wildly through garbage values.

The clock acts as a strict military traffic commander:

  1. It says: "Nobody look at the outputs yet! The signals are still settling."
  2. It waits for the electrical signals to stabilize across all gates.
  3. On the rising edge of the clock wave, it snaps open storage latches (flip-flops) for a fraction of a picosecond, captures the settled voltages, and locks them in place.

4. The Five Stages of the Instruction Cycle

Now let us follow the journey of a single instruction through the classic Five-Stage RISC Pipeline:

   ADD R1, R2, R3   (Machine Code: 0x003100B3)

Stage 1: Instruction Fetch (IF)

At the start of the clock tick:

  1. The Program Counter (PC) holds a memory address (for example, 0x00400000).
  2. This address is placed onto the memory Address Bus.
  3. The processor's Level 1 (L1) Instruction Cache looks up address 0x00400000, retrieves the 32-bit binary number stored there (0x003100B3), and places it onto the Data Bus.
  4. The bits are latched into the processor’s Instruction Register (IR).
  5. In parallel, a simple hardware adder increments the Program Counter by 4 bytes ($PC \leftarrow PC + 4$), pointing it toward the next instruction in memory.

Stage 2: Instruction Decode (ID)

The 32 bits sitting in the Instruction Register are wired directly into the Instruction Decoder:

  1. The decoder looks at the lowest 7 bits (the opcode 0110011). The logic gates inside the decoder identify this pattern: "This is an ALU register-to-register operation."
  2. The decoder examines the next 3 bits (funct3 = 000) and the top 7 bits (funct7 = 0000000): "This is an ADD operation."
  3. The decoder extracts the register address bits:
    • Source 1: 00010 (Register R2)
    • Source 2: 00011 (Register R3)
    • Destination: 00001 (Register R1)
  4. The decoder activates the control lines of the Register File. The voltages stored in Register R2 (say, the number 42) and Register R3 (the number 58) are placed onto the internal ALU input buses.

Stage 3: Execute (EX)

The operands from R2 and R3 enter the Arithmetic Logic Unit:

  1. The Instruction Decoder sends an electrical control code to the ALU: ALU_OP = ADD.
  2. Inside the ALU, multiplexers route the voltages from R2 and R3 into thirty-two cascaded full-adders.
  3. The binary addition ripples across the adder circuits: $$42 ;+; 58 \quad = \quad 100$$
  4. The binary representation of 100 (0000...01100100) stabilizes on the ALU output bus.

Stage 4: Memory Access (MEM)

This stage is used only for LOAD and STORE instructions that need to read from or write to main RAM:

  • If this were a LOAD instruction (e.g. LOAD R1, [R2 + 8]), the ALU's output would be used as a memory address to fetch data from RAM.
  • But because our instruction is a pure register-to-register ADD, the memory stage does nothing. The control unit keeps the memory write-enable line turned OFF, and the ALU result passes straight through to the next stage.

Stage 5: Register Writeback (WB)

On the final clock tick of the instruction:

  1. The control unit asserts the Write-Enable signal on Register R1.
  2. The ALU output bus (carrying the binary number 100) is connected to the input pins of Register R1.
  3. The internal flip-flops of Register R1 latch the voltage.
  4. The value of R1 is now permanently updated to 100.

The instruction is complete. The processor is already executing the next instruction.


5. Pipelining: The Automobile Assembly Line

In early processors from the 1970s (such as the MOS 6502 or Intel 8080), a CPU finished all five stages of one instruction before starting the next. If each stage took one clock cycle, an instruction took five cycles to complete.

In the 1980s, computer architects like John Hennessy (Stanford) and David Patterson (UC Berkeley) pioneered instruction pipelining.

Think of a commercial laundromat:

               THE LAUNDRY PIPELINE ANALOGY
  
   Sequential Laundry (No Pipelining):
   Wash Load 1 (30m) ──▶ Dry Load 1 (30m) ──▶ Fold Load 1 (30m)  [Total: 90 mins for 1 load]
   Wash Load 2 (30m) ──▶ Dry Load 2 (30m) ──▶ Fold Load 2 (30m)  [Total: 180 mins for 2 loads]
  
   Pipelined Laundry (Overlap):
   Load 1:   [ Wash ] ──▶ [ Dry  ] ──▶ [ Fold ]
   Load 2:                [ Wash ] ──▶ [ Dry  ] ──▶ [ Fold ]
   Load 3:                             [ Wash ] ──▶ [ Dry  ] ──▶ [ Fold ]
  
   After the pipeline fills, ONE LOAD COMPLETES EVERY 30 MINUTES!

A pipelined CPU applies this exact principle to instructions:

               THE 5-STAGE CPU PIPELINE EXECUTION GRID
  
   Clock Cycle:     1       2       3       4       5       6       7
  ───────────────────────────────────────────────────────────────────────
   Instruction 1:  [ IF ]  [ ID ]  [ EX ]  [ MEM]  [ WB ]
   Instruction 2:          [ IF ]  [ ID ]  [ EX ]  [ MEM]  [ WB ]
   Instruction 3:                  [ IF ]  [ ID ]  [ EX ]  [ MEM]  [ WB ]
   Instruction 4:                          [ IF ]  [ ID ]  [ EX ]  [ MEM]
   Instruction 5:                                  [ IF ]  [ ID ]  [ EX ]
  • In Cycle 1, Instruction 1 is Fetched.
  • In Cycle 2, Instruction 1 is Decoded, while Instruction 2 is Fetched.
  • In Cycle 3, Instruction 1 is Executed, Instruction 2 is Decoded, and Instruction 3 is Fetched.
  • By Cycle 5, all five stages of the CPU are working simultaneously on five different instructions!

Even though any single instruction takes five clock cycles from start to finish (latency), the CPU completes one full instruction every single clock cycle (throughput: $CPI \approx 1$).

At a clock speed of $3.5 \text{ GHz}$, the CPU completes 3.5 billion instructions every second.


6. Pipeline Hazards: When the Assembly Line Stalls

Pipelining is brilliant, but it creates profound engineering vulnerabilities called pipeline hazards:

                               THE THREE PIPELINE HAZARDS
  
  Hazard Type      Physical Cause                                  Modern Hardware Resolution
  ──────────────────────────────────────────────────────────────────────────────────────────
  Data Hazard      Instruction 2 needs the result of Inst 1        Forwarding (Bypassing ALU result
                   before Inst 1 has finished Writeback!           directly to input; or pipeline stall)
  
  Structural Haz.  Two instructions need the exact same physical   Separate L1 Instruction Cache
                   hardware resource at the exact same cycle       and L1 Data Cache (Harvard split)
  
  Control Hazard   A conditional branch (IF/ELSE) changes PC;     Branch Prediction & Speculative
                   CPU doesn't know which code to fetch next!      Execution (Guessing branch target)

The Control Hazard and Branch Prediction

The most dangerous hazard is the conditional branch (if/else statements in code):

CMP R1, 0          ; Is R1 equal to zero?
BEQ target_address ; If yes, branch to target_address!
ADD R2, R3, R4     ; Else, continue here...

When the CPU fetches BEQ (Branch if Equal), it cannot know whether the branch will be taken until Stage 3 (Execute) compares the numbers.

What should the CPU fetch during Cycles 2 and 3?

  • If it stops and waits, the pipeline empties (a pipeline stall or bubble), destroying performance.
  • Modern processors deploy Branch Predictors—sophisticated neural and pattern-matching circuits that track execution history and guess with over 95% accuracy which path the program will take.
  • The CPU speculatively executes instructions down the predicted path before it knows for sure if the branch was taken!
  • If the guess was correct: zero performance lost.
  • If the guess was wrong (a branch misprediction): the CPU must instantly flush all pipeline stages, discard the speculative results, and restart from the correct address, paying a penalty of 15 to 20 clock cycles.

7. Beyond the Single Core: Superscalar and GPUs

Modern high-performance processors push this architecture to staggering extremes:

  1. Superscalar Execution: Modern CPUs (like Apple M-series or AMD Zen) are not single pipelines. They are wide out-of-order superscalar engines with six to eight parallel execution pipelines. The CPU fetches eight instructions per cycle, analyzes their dependencies in real-time, reorders them, executes them across multiple ALUs simultaneously, and reassembles them in correct order.
  2. Graphics Processing Units (GPUs): While a CPU is optimized to execute a single, complex thread of branching instructions with ultra-low latency, a GPU takes the opposite approach. A GPU packs thousands of small, simple ALUs onto a single chip. It cannot run an operating system efficiently, but it can execute thousands of identical additions and multiplications simultaneously (SIMD: Single Instruction, Multiple Data).

As explored in How Large Language Models Generate Text, modern artificial intelligence models (such as LLMs and neural networks) are nothing more than billions of matrix multiplications. They run on GPUs because thousands of ALUs execute the exact same fetch-decode-execute cycle across billions of weights in parallel.


8. Summary: From Sand to Thought

LayerPhysical RealityIntellectual Breakthrough
MaterialPurified Silicon ($\text{SiO}_2$)Chemical crystallization and photolithographic etching
PhysicsControlled Electron DriftQuantum semiconductor bandgaps and field-effect gates
LogicLogic Gates (NAND, XOR, AND)George Boole's algebra & Claude Shannon's switching circuits
ArithmeticHalf-Adders & Full-AddersBinary addition via XOR sum and AND carry extraction
MicroarchitectureRegisters, Buses, Decoders, ALUClocked edge-triggered latches & instruction pipelines
SoftwareMachine Code BytesStored-program architecture; compiler code generation

From a handful of river sand to a microchip executing billions of instructions per second, computing is the physical embodiment of mathematical logic.

Every pixel on your screen, every web search query processed in How Search Engines Work, and every autonomous decision made in How AI Agents Work rests upon this five-stage mechanical journey executed three billion times every second. Every pixel on your screen, every byte retrieved from volatile silicon capacitors in How Computer Memory (RAM) Actually Works, every hardware privilege ring enforced by How an Operating System Actually Runs Software, and every autonomous decision made in How AI Agents Work rests upon this five-stage mechanical journey executed three billion times every second.

Core Concepts Introduced8 Concepts
The Stored-Program Concept (Von Neumann Architecture)The Program Counter (PC)The Instruction Register & Opcode DecodingArithmetic Logic Unit (ALU) ExecutionThe Five-Stage RISC Pipeline (IF, ID, EX, MEM, WB)Clock Frequency & Edge-Triggered LatchesPipeline Hazards & Branch PredictionRegister Writeback and State Transition
Knowledge Graph Connections

Where to Go From Here

Explore companion architectures or dive deeper into downstream mechanisms.

Next Question

How Binary Arithmetic Logic Units Actually Add Numbers

How does an Arithmetic Logic Unit add two 64-bit binary numbers in a fraction of a nanosecond when carry bits must travel across 64 consecutive stages?

Explore How Binary Arithmetic Logic Units Actually Add Numbers
Next Question

How CMOS Transistors Form Logic Gates

How do microscopic silicon transistors physically connect together to calculate NOT, NAND, NOR, and XOR without wasting continuous electrical power?

Explore How CMOS Transistors Form Logic Gates
Research Grounding & Primary Sources

Verified Specifications & Architectural References

4 Authoritative References

This explainer is grounded in primary-source engineering specifications, regulatory circulars, and standard documentation.

Primary SourceMorgan Kaufmann (John L. Hennessy, David A. Patterson)• 2017

Computer Architecture: A Quantitative Approach

The seminal masterwork on computer microarchitecture, instruction set design, pipelining, memory hierarchies, and superscalar execution.

Primary SourceMorgan Kaufmann (David Money Harris, Sarah L. Harris)• 2012

Digital Design and Computer Architecture

Clear, exhaustive circuit-level engineering text tracing the construction of a complete RISC processor from logic gates up to assembly execution.

Primary SourcePearson (Randal E. Bryant, David R. O'Hallaron)• 2015

Computer Systems: A Programmer's Perspective

Foundational university reference explaining how high-level code translates into machine instructions and executes on pipelined hardware.

Primary SourceMoore School of Electrical Engineering (John von Neumann)• 1945

First Draft of a Report on the EDVAC

The founding architectural document establishing the stored-program computer: memory holding both data and instructions executed by a central processor.

Previous ExplainerHow Binary and Logic Gates Became ComputationNext Explainer How Computer Memory (RAM) Actually Works
More from Computing & Digital Architecture•Topic Hub: computingTopic Hub: Computing & Digital Architecture
Ground Truth Engineering Publication