CPU - Fetch Decode Execute
How does a CPU work?
A CPU (Central Processing Unit) is the “brain” of a computer. Its job is to execute instructions quickly and very precisely. At its core, a CPU repeatedly performs a simple loop called the Fetch → Decode → Execute cycle. Everything a computer does ultimately comes down to this process.
x86 Machine Code
CPU Clock Speed
Clock speed determines how many cycles a CPU can perform per second.
- Measured in GHz (gigahertz)
1 GHz = 1 billion cycles per second
For example:
- A 3.5 GHz CPU can perform 3.5 billion cycles per second
A higher clock speed does not automatically mean a faster CPU.
Modern performance also depends on:
- Number of cores
- Instructions per cycle (IPC)
- Cache efficiency
- Branch prediction and pipelining
Programs and Memory
Before diving into the CPU cycle, it helps to understand where programs live.
- Programs are stored in main memory (RAM)
- A program is a sequence of machine code instructions
- Data used by the program is also stored in memory
The CPU does not execute programs directly from disk—it only works with data that has been loaded into RAM.
The Fetch–Decode–Execute Cycle
Every instruction goes through the same three stages:
- Fetch – Get the instruction from memory
- Decode – Figure out what the instruction means
- Execute – Perform the instruction
This loop repeats billions of times per second.
Fetch
The Fetch stage retrieves the next instruction from memory.
Key Components
-
Program Counter (PC)
- A special CPU register
- Holds the memory address of the next instruction to execute
-
Address Bus
- Carries the memory address from the CPU to RAM
-
Data Bus
- Carries the instruction data from RAM back to the CPU
What Happens
- The CPU places the value of the Program Counter onto the Address Bus
- Memory reads the instruction at that address
- The instruction is sent back to the CPU over the Data Bus
- The instruction is stored in the Instruction Register (IR)
Think of the PC as a bookmark telling the CPU where it is in the program.
Decode
Once the instruction is fetched, the CPU must understand what to do.
Instruction Structure
An instruction typically contains:
- An opcode (what operation to perform)
- Zero or more operands (data or memory locations)
Decoding Process
- The Control Unit examines the opcode
- It determines:
- Which operation to perform
- Which hardware components are needed
- Where the operands come from
Examples:
- Add two numbers
- Load data from memory
- Jump to another instruction
- Compare values and branch
RISC processors tend to have simpler instructions
CISC processors may decode one instruction into multiple internal steps
Execute
In the Execute stage, the instruction actually does something.
Possible Actions
Depending on the instruction, the CPU may:
- Perform arithmetic or logic using the ALU (Arithmetic Logic Unit)
- Read from or write to memory
- Modify registers
- Change control flow (branches or jumps)
The ALU
- Performs operations like:
- Addition
- Subtraction
- AND / OR / XOR
- Comparisons
- Uses its own smaller internal control signals (not full machine opcodes)
Operands are usually taken from CPU registers, which are much faster than memory.
Updating the Program Counter
At the end of execution, the CPU updates the Program Counter:
-
Sequential execution
- PC increments to the next instruction
-
Branch or jump instruction
- PC is set to a new address based on a condition or target
This updated PC value determines what will be fetched next.
CPU Architecture (32-bit vs 64-bit)
When people refer to a 32-bit or 64-bit CPU, they are talking about the processor’s word size.
Word Size Affects
- Register size
- Maximum addressable memory
- Data processed per instruction
Memory Addressing
-
32-bit CPU
- Can address up to
2³²memory locations - ~4 billion bytes → 4 GB RAM
- Can address up to
-
64-bit CPU
- Can address vastly more memory
- Practical limits depend on the operating system and hardware