The Instruction Cycle
In order for a single instruction to be executed by the CPU, it must go through the instruction cycle (also sometimes referred to as the fetch-execute cycle). While this cycle can vary from CPU to CPU, they typically consist of the following stages:
- Memory Access
- Registry Write-Back
In this article, we’ll go through the different stages of the instruction cycle to gain a better understanding of how the CPU handles instructions.
From the moment we turn our computers on, the CPU is ready to process instructions. As instructions come in, a register in the CPU referred to as the Program Counter (PC) stores the memory address of the instruction that should be processed next. When it’s time to start processing the instruction, the CPU copies the instruction’s memory address and stores the copied data to another register on the CPU called the Instruction Register (IR). Once the memory of the instruction is available, the instruction gets decoded.
Think of being at a deli. As you come in and give your order, a ticket containing your data (name, number in line, and food order) is created and placed somewhere that the deli staff can easily access and refer to. Once your number comes up, then someone will start working on your order!
The next stage in the cycle involves decoding the instruction. During this stage, the Control Unit deciphers what the instruction stored in the IR means. For example, the instruction could have been sent to do an arithmetic operation or to send information to another piece of hardware. As the instruction is decoded, they are turned into a series of control signals that are used to execute the instruction.
Back at the deli, a staff member picks up your order ticket. Before they start making your order, they first need to figure out what you’re asking them to make!
In this stage, the instruction is performed! We noted that during the decoding stage, the instruction is decoded into control signals and sent to the correct part of the ALU to be processed and completed.
In our deli example, this is the part where the order gets made!
So to recap, in order to process an instruction, we need to fetch it from memory, decode the instruction, and execute it. That’s all, right? Not quite! Sometimes a few extra stages need to occur before or after execution.
The memory access stage is used to retrieve any required data necessary to execute an instruction. This stage only occurs if the instruction requires data from memory. For example, imagine the following Python code:
x = 5y = x + 3
Once the first instruction is complete, a piece of memory is created to store the data
x = 5. The second instruction,
y = x + 3, is a little trickier to execute because the value of
y relies on whatever value was assigned to
y = x + 3 can be executed, we need to access the memory address of the first instruction
x = 5 in order to retrieve the data that tells us what the value of
Imagine in your deli order, you ask for honey mustard to be added to one of the two sandwiches you order. Before your order can be created, the staff member needs to make and retrieve honey mustard for the sandwich.
The registry write-back stage is used if the execution of the instruction impacts data. This is another stage that isn’t always a part of the cycle.
Let’s think back to our previous example:
x = 5y = x + 3
As each instruction is executed, we find ourselves needing to save this data. During the registry write-back stage, this new data is stored to one of the register’s in the CPU. The registry write-back stage is also necessary if existing data is changed or updated.
As the deli’s 10,000th customer, they decide to name your order after you and put it on their “Deli Specials” board. They need to create space and allocate a part of the board’s “memory” to store your order.
Nice job reaching the end of the article. Let’s recap what we learned:
- Instructions must go through the instruction cycle in order to be processed by the CPU
- Although the cycle varies amongst CPUs, the stages of the instruction cycle are:
- Fetch the instruction.
- Decode the instruction.
- Execute the instruction.
- Memory access (if needed).
- Registry write-back (if needed).