How a CPU Executes an Instruction
Every program you run, from a web browser to a game, is eventually reduced to a long list of very simple instructions for the processor: add these two numbers, load a value from memory, compare, jump somewhere else. A CPU is a machine that follows that list one step at a time, billions of times per second.
Understanding what happens to a single instruction makes the rest of computer architecture much easier to follow. The process is a repeating cycle, often called the fetch-decode-execute cycle.
The Pieces Involved
A few components take part in nearly every instruction.
- Program counter (PC): A register that holds the memory address of the next instruction.
- Instruction register (IR): Holds the instruction currently being worked on.
- Control unit: Decodes the instruction and sends signals that tell the other parts what to do.
- Registers: A small set of very fast storage locations inside the CPU for values in use.
- Arithmetic logic unit (ALU): The circuit that performs math and logic operations.
- Memory and cache: Where instructions and data live outside the registers.
- Clock: A timing signal that steps the work along.
Step 1: Fetch
The CPU reads the address in the program counter and requests the instruction stored there. The instruction travels from memory, usually via the cache, into the instruction register. The program counter is then advanced to point at the next instruction, unless the current one changes the flow, such as a jump.
Step 2: Decode
An instruction is a binary pattern. Part of it, the opcode, says what to do. Other parts identify which registers, memory addresses or constant values to use, called operands. The control unit examines the bit pattern and works out the operation and where the operands are.
Instruction sets differ in how regular this is. Some designs use fixed-length instructions that are easy to decode, while others use variable lengths that take more effort. Many modern processors break complex instructions into smaller internal operations to keep the hardware simple.
Step 3: Execute
The control unit activates the right circuit. For an addition, the ALU takes two operands and produces a sum, and it also sets flags, small status bits that record things like a zero result or an overflow. For a memory access, an address is calculated and sent out. For a branch, the condition is tested and the program counter may be replaced with a new target.
Step 4: Memory Access
If the instruction reads or writes memory, that happens here. A load instruction fetches data from an address into a register, while a store writes a register value out. Because memory is far slower than the CPU, the processor first checks its caches, which hold recently used data. A hit returns the value quickly, while a miss forces a longer trip to main memory.
Step 5: Write-Back
The result is written to its destination, usually a register. Now the instruction is complete, and the next one is waiting.
A Tiny Example
Consider the instruction to add register B to register A and store the result in register A.
- The CPU fetches the instruction bytes from the address in the program counter.
- It decodes them and learns the opcode is an addition and the operands are registers A and B.
- The values in A and B are sent to the ALU, which produces the sum.
- There is no memory access.
- The sum is written back to A, and flags are updated.
The next fetch then begins from the advanced program counter.
Pipelining: Doing Several Things at Once
Running each stage one after another wastes time, because most hardware sits idle while a single instruction works through the stages. A pipeline fixes that, like a factory assembly line. While one instruction is executing, the next is decoding and the one after that is being fetched. Once the pipeline fills, one instruction can finish in nearly every clock cycle, even though each still takes several cycles from start to end.
Pipelines run into hazards.
- Data hazards: An instruction needs a result that an earlier instruction has not produced yet. Hardware may stall or forward the result directly.
- Control hazards: A branch changes the next instruction, so already fetched ones may be wrong. Processors use branch prediction to guess the outcome and discard work if the guess fails.
- Structural hazards: Two instructions need the same hardware at the same time.
Going Further
Modern CPUs add more tricks on top. Superscalar designs issue several instructions per cycle. Out-of-order execution lets independent instructions run as soon as their inputs are ready, then puts results back in order. Multiple cores run separate instruction streams at the same time. All of these accelerate the basic cycle without changing its meaning.
Frequently Asked Questions
What is the difference between clock speed and instructions per cycle?
Clock speed counts how many cycles occur each second. Instructions per cycle describes how much work finishes in each one. Performance depends on both, so a lower clock with higher efficiency can match a faster clock with lower efficiency.
What is machine code?
Machine code is the binary form of instructions that the CPU decodes directly. Assembly language is a readable representation of it, and higher level languages are translated into it by compilers or interpreters.
Why do caches matter so much?
Main memory is hundreds of times slower than the CPU's internal speed. Caches keep nearby copies of recently used data and instructions close to the processor, so most accesses avoid the slow trip and the pipeline stays busy.