Introduction
Welcome back! In our last lesson, we established the high-level blueprint of a computer: the von Neumann architecture. We identified the CPU as the "brain" of the operation, containing a Control Unit (CU), an Arithmetic Logic Unit (ALU), and registers.
Today, we will zoom in on the CPU to understand how these internal components work together. Your learning outcome is to explain the role of registers, ALU, and control unit in instruction execution. We'll uncover the fundamental "heartbeat" of the CPU—the process that takes a single instruction from your C++ program and brings it to life. This process is the foundation for all computation.
The CPU's Core Components
As we discussed, the CPU's work is divided among three main types of components. Let's start by clarifying their distinct jobs.
To begin, let's watch a quick overview that introduces the main functional units within a processor.
Watch the segment from 01:21 to 01:48. This will re-introduce the ALU, registers, and the control unit.
Now, let's look at each one in more detail.
-
The Arithmetic Logic Unit (ALU): This is the computational core of the CPU. As the name suggests, it handles all arithmetic (addition, subtraction, etc.) and logic (AND, OR, NOT) operations. Think of it as the calculator of the CPU, performing the actual data manipulation. After an operation, the ALU also sets status bits called flags (e.g., a Zero Flag if the result was zero, a Sign Flag if the result was negative), which are crucial for decision-making in programs (like
ifstatements). -
The Control Unit (CU): If the ALU is the calculator, the CU is the director. It doesn't perform calculations itself. Instead, it fetches instructions from memory, decodes them to understand what needs to be done, and then issues a sequence of control signals to the ALU, registers, and memory to execute the instruction. It orchestrates the entire process, ensuring data flows to the right place at the right time.
-
Registers: These are small, extremely fast storage locations built directly into the CPU. They act as the CPU's immediate scratchpad, temporarily holding the instruction currently being executed, data that the ALU is about to process, and memory addresses. Their speed is critical; accessing data from a register is orders of magnitude faster than accessing it from main memory (RAM).
The Instruction Cycle's Key Players: Special-Purpose Registers
To orchestrate the execution of an instruction, the Control Unit relies on several special-purpose registers. Each has a very specific job. These are fundamental to understanding the flow of information within the CPU.
Mastering Computer Organization, Architecture, and Assembly Language
This article provides excellent, detailed descriptions of the most important special-purpose registers. Understanding their roles is key to understanding the instruction cycle.
Please read section '6. Registers and Flags in CPU'. Focus on the subsection 'Special-Purpose Registers' to learn about the Program Counter (PC/IP), Instruction Register (IR), Memory Address Register (MAR), and Memory Data Register (MDR). Also, read the 'Condition Flags' section to see what the ALU produces besides a numerical result.
Let's summarize the roles of the four most important registers for instruction execution:
- Program Counter (PC): This register is the CPU's bookmark. It always holds the memory address of the next instruction to be fetched.
- Memory Address Register (MAR): When the CPU needs to read from or write to memory, it places the target memory address in the MAR.
- Memory Data Register (MDR): This register acts as a two-way buffer. It holds data just read from memory or data waiting to be written to memory.
- Instruction Register (IR): After an instruction is fetched from memory, it's placed in the IR. The Control Unit then decodes the instruction held in this register.
With these components defined, we can now see how they work together.
The Heartbeat: The Fetch-Decode-Execute Cycle
The CPU executes a program by relentlessly performing a sequence of steps known as the Fetch-Decode-Execute cycle. This cycle is the fundamental operation of the CPU, repeated for every single instruction.
The following animated video provides an excellent walkthrough of this cycle. We'll watch it in parts to analyze each stage.
Stage 1: Fetch
The first step is to retrieve an instruction from memory.
Fetch Decode Execute Cycle in more detail
Let's see how the CPU fetches an instruction from memory. This animation clearly shows the registers involved.
Watch from 01:26 to 03:49. Observe the interplay between the Program Counter (PC), MAR, MDR, and the Current Instruction Register (CIR, which is another name for IR).
As you just saw, the fetch process works as follows:
- The address in the Program Counter (PC) is copied to the Memory Address Register (MAR).
- The Control Unit (CU) signals a memory read, and the instruction at that address is retrieved from RAM and placed in the Memory Data Register (MDR).
- The instruction is copied from the MDR to the Instruction Register (IR).
- The PC is incremented to point to the address of the next instruction.
The instruction is now inside the CPU, ready to be understood.
Stage 2 & 3: Decode and Execute
Once the instruction is in the IR, the Control Unit takes over to decode it and orchestrate its execution. The exact actions depend on the instruction itself.
Fetch Decode Execute Cycle in more detail
Now, watch how the CU decodes the instruction and directs the execution for two different types: loading data from memory and performing an arithmetic operation with the ALU.
Continue watching from 03:49 to 06:53. Notice that for a 'load' instruction, the MAR and MDR are used again to fetch data. For the 'add' instruction, see how the ALU is activated to perform the calculation.
Let's break this down:
- Decode: The CU analyzes the bit pattern in the Instruction Register (IR). It identifies the operation to be performed (the opcode, e.g.,
LOAD,ADD,STORE) and the data or memory locations involved (operands). - Execute: The CU sends out control signals to the relevant components.
- For an arithmetic/logic instruction (like
ADD): It routes data from registers to the ALU, instructs the ALU to perform the operation, and routes the result back to a register (often called the Accumulator).
- For a data transfer instruction (like
LOAD): It uses the MAR and MDR to fetch a piece of data from memory and place it into a register. For aSTOREinstruction, it would move data from a register into memory.
- For an arithmetic/logic instruction (like
This entire sequence is elegantly captured in the following diagram.

From C++ to Machine Instructions
How does this relate to the C++ code you write? A single line like int result = a + b; is translated by the compiler into several simple machine instructions. Conceptually, they would look something like this:
LOAD a-> Fetch the instruction. Decode it. Execute by copying the value of variableafrom memory into a register.LOAD b-> Fetch the instruction. Decode it. Execute by copying the value of variablebinto another register.ADD-> Fetch the instruction. Decode it. Execute by sending the two registers to the ALU, performing addition, and storing the sum in a register.STORE result-> Fetch the instruction. Decode it. Execute by copying the result from the register into the memory location for the variableresult.
Each of these machine instructions goes through its own Fetch-Decode-Execute cycle, tirelessly executed by the CPU.
Conclusion
In this lesson, we dissected the inner workings of the CPU to see how it executes instructions. You now understand the specific contributions of its core components.
Here are the key takeaways:
- The Control Unit (CU) is the director, fetching and decoding instructions and orchestrating the entire process.
- The Arithmetic Logic Unit (ALU) is the performer, executing all mathematical and logical calculations.
- Registers are the CPU's high-speed internal workspace. Special-purpose registers like the Program Counter (PC), Instruction Register (IR), MAR, and MDR are essential for the instruction cycle.
- The Fetch-Decode-Execute cycle is the fundamental, repetitive process the CPU uses to execute instructions, bringing together the CU, ALU, and registers.
Preview of the Next Lesson:
You now understand the roles of the components in the cycle. In the next lesson, we will solidify this knowledge by doing a "pen-and-paper" trace. Your learning outcome will be to trace the fetch-decode-execute cycle for simple instructions. Given a small program and the initial state of memory, you will predict the values in the key registers after each step of the cycle.