The Fetch-Execute Cycle đź§ đź’»
students, every time you open an app, type a message, or search the web, your computer is doing millions or even billions of tiny steps behind the scenes. The main pattern that makes this possible is called the fetch-execute cycle. It is the repeated process a processor uses to read instructions from memory, understand them, and carry them out.
Lesson objectives
By the end of this lesson, students, you should be able to:
- explain the main ideas and terminology behind the fetch-execute cycle
- describe the role of the CPU, memory, and registers in the process
- apply IB Computer Science HL reasoning to simple examples of instruction execution
- connect the fetch-execute cycle to computer organization as a whole
- summarize why this cycle is essential for all modern computers
A helpful real-world comparison is a recipe in a kitchen 🍳. The recipe gives instructions, the chef reads one step at a time, and the tools in the kitchen help carry out each action. In a computer, the CPU is like the chef, memory stores the recipe, and registers are the small workspace where the current step is handled.
What the fetch-execute cycle is
The fetch-execute cycle is the repeating process that the CPU follows to run program instructions. A program is stored as a sequence of machine code instructions in memory. Each instruction tells the processor to do one small task, such as adding two numbers, moving data, or comparing values.
The cycle is usually described in four main stages:
- Fetch the next instruction from memory
- Decode the instruction to understand what it means
- Execute the instruction
- Store the result if needed
These steps happen extremely quickly. Modern processors perform this cycle billions of times per second. Even though real processors are more complex internally, this model is the standard way to understand how they work at IB Computer Science HL level.
The CPU uses special storage locations called registers to keep track of what it is doing. Two very important registers are:
- Program Counter $\text{PC}$: holds the address of the next instruction to fetch
- Current Instruction Register $\text{CIR}$: holds the instruction currently being decoded and executed
Another key part is the memory address register $\text{MAR}$, which stores the address of the memory location being accessed, and the memory data register $\text{MDR}$, which stores the data being transferred to or from memory.
Step-by-step: how the cycle works
1. Fetch
The CPU begins by using the $\text{PC}$ to find the address of the next instruction. That address is copied into the $\text{MAR}$. Then the control unit sends a signal to main memory to read the instruction at that address.
The instruction is copied from memory into the $\text{MDR}$, then transferred into the $\text{CIR}$. After that, the $\text{PC}$ is usually increased so that it points to the following instruction. If one instruction is stored at each memory address, then the next instruction is the next address in sequence.
This is like reading one line of a worksheet at a time đź“„. Once you have copied the current line into your working area, you move your finger to the next line so you know where to go next.
2. Decode
In the decode stage, the control unit examines the contents of the $\text{CIR}$. It identifies:
- the operation code, often called the opcode
- the operand or operands, which are the values or addresses involved
The opcode tells the CPU what action to take, such as ADD, LOAD, STORE, or JUMP. The operands tell it what data to use or where to find it.
For example, an instruction might mean “add the number stored in address $200$ to the value in a register.” The CPU does not treat the instruction as plain text. Instead, it interprets the binary pattern according to the instruction set architecture of that processor.
3. Execute
In the execute stage, the CPU carries out the instruction. The action depends on the instruction type.
Some examples:
- Arithmetic: add, subtract, multiply, divide
- Data movement: copy a value from memory into a register, or from a register back into memory
- Logic: compare values, AND, OR, NOT
- Control flow: jump to another instruction address if a condition is met
If the instruction is ADD, the arithmetic logic unit $\text{ALU}$ may perform the calculation. If the instruction is a branch or jump, the $\text{PC}$ may be changed to a different address instead of moving to the next sequential instruction.
4. Store
If the instruction produces a result that must be saved, the CPU stores it in a register or in memory. For example, after calculating $8+5$, the result $13$ may be left in a register or written to memory at a specific address.
Not every instruction needs to store a visible result. Some instructions only change internal flags or update the $\text{PC}$.
A simple example
Suppose a program contains these simplified instructions:
- LOAD $A$
- ADD $B$
- STORE $C$
Imagine that $A=7$ and $B=4$. The CPU fetches the first instruction, decodes it, and loads the value at $A$ into a register. Then it fetches the second instruction and adds $4$ to the current register value, giving $11$. Finally, it fetches the third instruction and stores $11$ at address $C$.
This shows an important idea: the fetch-execute cycle happens one instruction at a time, but programs can still produce complex behavior because many small instructions work together.
A calculator app on your phone works in the same basic way 📱. When you enter $12+8$, the processor does not “understand” the whole calculation as a human would. Instead, it processes many machine instructions that eventually produce the result $20$.
Why the cycle matters in computer organization
Computer organization is about how the parts of a computer work together to process data. The fetch-execute cycle connects several key components:
- main memory, which stores instructions and data
- CPU, which controls and performs processing
- registers, which hold temporary data and addresses
- control unit, which directs the sequence of operations
- ALU, which performs calculations and logical operations
This means the fetch-execute cycle is not just a theory. It is the link between hardware organization and actual program execution.
The cycle also helps explain several important IB ideas:
- Von Neumann architecture: instructions and data are both stored in the same main memory
- bottleneck: the speed of memory access can limit overall performance because the CPU may have to wait for data or instructions
- clock speed: the processor’s clock controls the pace of operations, although modern CPUs may use internal techniques to handle multiple instructions efficiently
It is important to know that real processors often use advanced features such as caching, pipelining, and multiple cores. Even so, the fetch-execute cycle remains the core model for understanding how instruction processing works.
Common misunderstandings
One common mistake is thinking that a program is executed all at once. In reality, the CPU works through instructions in order, unless a jump or branch changes the flow.
Another misunderstanding is assuming the $\text{PC}$ always increases by exactly $1$. That is often true in simple models, but the actual next address depends on the instruction size and on whether the program uses a jump, branch, or subroutine call.
A third mistake is mixing up the roles of memory and registers. Memory is large but slower. Registers are tiny but very fast. The CPU uses registers for immediate work because fetching from registers is much quicker than repeatedly accessing main memory.
Conclusion
students, the fetch-execute cycle is the basic repeating process that lets a CPU run a program. The CPU fetches an instruction from memory, decodes it, executes it, and stores the result if needed. Registers such as the $\text{PC}$, $\text{CIR}$, $\text{MAR}$, and $\text{MDR}$ support each stage.
This cycle is central to computer organization because it shows how hardware components work together to process instructions. It also explains why computers are fast, why memory matters, and why even complex applications are built from many simple machine instructions.
Study Notes
- The fetch-execute cycle is the repeated process used by the CPU to run instructions.
- Main stages: fetch, decode, execute, store.
- The $\text{PC}$ holds the address of the next instruction.
- The $\text{CIR}$ holds the current instruction being worked on.
- The $\text{MAR}$ holds the memory address being accessed.
- The $\text{MDR}$ holds data moving between memory and the CPU.
- The opcode tells the CPU what to do; operands give the data or addresses involved.
- The $\text{ALU}$ performs arithmetic and logic operations.
- The cycle repeats many times per second and is the foundation of program execution.
- The fetch-execute cycle connects memory, the CPU, registers, and control signals.
- In Von Neumann systems, instructions and data share the same main memory.
- Real CPUs may use caching and pipelining, but the fetch-execute cycle is still the core model.
- Understanding this cycle helps explain how computers process code, data, and user actions.
