Program Execution: From Code to CPU Instructions
The CPU is the part of the computer built to obsessively ask 'What is next?' and determine the next operation to execute.
The CPU’s Central Question
A program may look like a sequence of instructions written by a person, but execution depends on a component that repeatedly determines what should happen next. The Central Processing Unit, or CPU, is the part of the computer built to ask “What is next?” and determine the next operation to execute. It performs this work at a scale measured in billions of cycles per second.
The CPU’s primary role is not simply to perform calculations. Its fundamental job is to determine the next operation, interpret that operation, and carry it out.
The Repeating Execution Cycle
The CPU follows a cycle called fetch-decode-execute. First, it fetches the next instruction from memory. Next, it decodes the instruction by determining what it means. Then it executes the operation. After execution, the CPU immediately returns to the fetch phase and retrieves the next instruction. This cycle continues throughout program execution.
- Fetch: retrieve the next instruction from memory
- Decode: determine what the instruction means
- Execute: perform the operation
- Repeat: fetch the next instruction and continue the cycle
From Program to Instruction Sequence
When you write a program, you are essentially giving the CPU a list of instructions to execute in order. The CPU works through those instructions using fetch, decode, and execute. The important idea is that execution is not one large, mysterious event: the CPU repeatedly handles the next instruction, understands it, performs it, and then moves on.
Gigahertz and Repeated Timing
Gigahertz, abbreviated GHz, is a measure of CPU speed. One gigahertz means one billion cycles per second.
A 3.0 GHz CPU is described in the source as asking “What is next?” three billion times per second. In that same perspective, three billion decisions, three billion instruction fetches, and three billion execution cycles happen in one second. Modern CPUs often run between 2.0 and 5.0 GHz, although some specialized processors can go faster.
| Rating | Cycles per second | Interpretation |
|---|---|---|
| 1.0 GHz | One billion | One billion cycles per second |
| 3.0 GHz | Three billion | Three billion cycles per second |
| 5.0 GHz | Five billion | Five billion cycles per second |
A Single Instruction Trace
Tracing Three Abstract Operations
Suppose a program provides the CPU with three ordered operations: instruction A, instruction B, and instruction C. How does the CPU handle them?
First fetch: The CPU retrieves instruction A from memory because it is the next instruction in the sequence.
First decode: The CPU determines what instruction A means.
First execute: The CPU performs the operation represented by instruction A.
Second instruction: The CPU returns to fetch, retrieves instruction B, decodes it, and executes it.
Third instruction: The CPU repeats the same cycle for instruction C.
The CPU processes the ordered instruction sequence by repeatedly fetching, decoding, and executing the next operation.
This example uses abstract instruction names rather than a particular programming language. Its purpose is to show the repeated state change: the CPU moves from one next instruction to the following instruction, and each one passes through fetch, decode, and execute.
What do you think happens?
After the CPU executes instruction B, what does it do next?
Reveal answer
Answer: It fetches instruction C.
The fetch-decode-execute cycle returns to fetching the next instruction after an operation is executed.
The CPU’s Internal Team
The CPU does not carry out this work through one undifferentiated part. The Control Unit directs the flow of instructions and coordinates the fetch-decode-execute cycle. The Arithmetic Logic Unit, or ALU, performs calculations and logical operations. Registers are tiny, ultra-fast storage locations holding data the CPU is currently working with. Cache is a small, very fast memory that stores frequently used data so the CPU does not have to wait for slower main memory.
| CPU component | Role described in the source |
|---|---|
| Control Unit | Directs instruction flow and coordinates fetch-decode-execute |
| Arithmetic Logic Unit | Performs calculations and logical operations |
| Registers | Hold data the CPU is currently working with |
| Cache | Stores frequently used data in very fast memory |
Several CPU components work together during instruction execution.
Why Repetition Matters
The CPU’s speed makes repeated work important. If a loop runs a million times and each iteration performs unnecessary work, the CPU repeats that unnecessary work a million times. Efficient code helps the CPU complete the intended work so quickly that a program can feel instant to the user.
Common Misunderstandings
Thinking CPU speed is measured only by the number of calculations a CPU performs.
The source defines GHz as one billion cycles per second and connects the CPU’s speed to repeated decisions, instruction fetches, and execution cycles.
Fix:
Describe 3.0 GHz as three billion cycles per second, while remembering that the CPU’s broader role is to fetch, decode, and execute instructions.Stopping the execution model after the execute phase.
The fetch-decode-execute cycle immediately returns to fetching the next instruction.
Fix:
Treat execution as a repeating cycle: fetch, decode, execute, and fetch again.Treating the CPU as if it works alone.
These components work together to execute instructions at high speed.
Fix:
Explain the CPU as a coordinated collection of components with different roles.Assuming every inefficiency matters equally.
The source emphasizes that repeated work, especially inside a loop, is multiplied across many executions.
Fix:
Pay particular attention to unnecessary operations that occur repeatedly.
Check Your Understanding
In your own words, describe what happens after the CPU fetches an instruction. Then explain what 2.0 GHz means in terms of cycles per second.
Hints
- Use the words decode and execute.
- One gigahertz means one billion cycles per second.
- Multiply one billion by the numerical GHz rating.
A loop performs unnecessary work on every one of its million iterations. Explain why this may matter even though the unnecessary operation in one iteration seems small.
Hints
- Focus on repetition.
- The CPU executes the repeated operation each time the loop iterates.
Execution in One View
- The CPU is the computer component whose central role is determining what operation comes next.
- It executes instructions through the repeating fetch-decode-execute cycle.
- Gigahertz measures cycles per second; one GHz means one billion cycles per second.
- The Control Unit, ALU, registers, and cache work together inside the CPU.
- Repeated unnecessary work can become significant because the CPU performs it again and again.
Key Takeaways
- The CPU repeatedly asks “What is next?” by determining the next operation to execute.
- Program execution follows fetch, decode, execute, and then returns to fetch for the next instruction.
- One GHz equals one billion cycles per second, so a 3.0 GHz CPU is described as operating across three billion cycles per second.
- The Control Unit, ALU, registers, and cache contribute different roles to instruction execution.
- Efficient code matters especially when unnecessary work is repeated many times.