1. Overview

We all know that no matter how complex a program is, a computer views it as a set of instructions. Understanding and executing these instructions is the basic operation, or instruction cycle, of a computer. This instruction cycle fetches instructions, decodes them, and then executes them. That’s why we refer to it as the Fetch-Execute cycle or Fetch-Decode-Execute cycle.

In this tutorial, we’ll explore the Fetch-Execute cycle and its stages in detail. This tutorial assumes that we’re already familiar with the basic components and workings of the CPU.

2. Introduction to the Fetch-Execute Cycle

The Fetch-Execute cycle (also known as Fetch-Decode-Execute cycle) was first introduced by John von Neumann. This cycle starts when the computer is turned on and keeps repeating until the computer is shut down:

The image represents that the fetch, decode, and execute stages are repetative in the Fetch-Execute cycle.

The Fetch-Execute cycle is divided into three main stages: fetch, decode, and execute the instructions. Let’s discuss each stage briefly before going into much detail:

  • Fetch: the computer retrieves the instruction from main memory and stores it in the registers.
  • Decode: the CPU interprets the instruction and figures out what action to take.
  • Execute: finally, the CPU performs the action that’s required, such as performing calculations.

We often wonder how this sequentially repetitive cycle can handle the execution of complex programs so efficiently and incredibly fast. Basically, the speed of this cycle depends on various factors, such as clock speed, complexity of the instruction, memory access time, and architecture of the CPU. It’s important to understand that the latest processors can handle billions of instructions per second.

Moreover, many modern CPUs utilize techniques like pipelining to further improve the speed and efficiency of the Fetch-Execute cycle by enabling multiple instructions to be processed simultaneously at different stages of the cycle.

3. Stages of Fetch-Execute Cycle

The figure below represents the flow of the Fetch-Execute cycle:

Fetch-Execute cycle process

Let’s now discuss each stage of the Fetch-Execute cycle in detail.

3.1. Fetch

In the Fetch stage, the computer fetches an instruction from the main memory (RAM) to the CPU. It begins by reading the content of the Program Counter register, which contains the memory address of the next instruction that requires to be fetched. The CPU then copies the exact address of the current instruction that’s required to be fetched into the Memory Address Register (MAR). Once the address is stored in the MAR, the CPU fetches the instruction from the specified memory address and places it in the Memory Data Register (MDR). Finally, the fetched instruction is copied into the Current Instruction Register (CIR) for the next stage.

During this stage, the Program Counter is incremented by one to point to the next instruction in memory, ready for the next fetch cycle.

3.2. Decode

Once the instruction is fetched, it’s ready for the Decode stage. In this stage, the instruction stored in the CIR is decoded by the Control Unit (CU) of the CPU. We must understand that each instruction is divided into two parts:

  • Opcode (Operation code): specifies the action that’s required to be performed.
  • Operand: specifies the data or the memory address where the data is stored, which is required for the operation.

This is an important stage of the cycle as it decodes the instruction and prepares the CPU to handle any operands required to complete the instruction. This stage can also involve fetching additional data from memory or identifying the required registers.

3.3. Execute

Finally, let’s discuss the Execute stage. In this final stage, the CPU performs the actual operation specified by the decoded instruction. The execution process completely depends on the instruction type. For instance, if the instruction is to calculate something, the CPU utilizes the Arithmetic Logic Unit (ALU) to perform the calculation. Apart from calculations, the operation could involve moving data, storing data at a specific memory address, or comparing values.

Once the operation is complete, the result may be stored in the Accumulator register or sent to memory. Lastly, the CPU prepares for the next instruction by repeating the Fetch-Execute cycle.

The figure below illustrates the process of the Fetch-Execute cycle for an instruction, highlighting the role of each register involved in the process:

Steps of Fetch-Execute cycle with the details of every register involved in the process.

4. Understand the Fetch-Execute Cycle via Example

Let’s discuss an example of a Python code that multiplies two variables to understand the fetch, decode, and execute stages for these instructions:

# code for multiplying two variables
a = 5
b = 3
result = a * b

Let’s understand the first instruction a = 5. In the Fetch stage, the instruction is fetched and stored in the CIR. Then, in the Decode stage, the control unit recognizes that the operation is an assignment (opcode) and identifies the value (5) as the operand. The CPU prepares to store the value in the variable (a) by deciding the appropriate memory location. Finally, in the Execute stage, the instruction is executed by storing the value (5) in the designated memory location for the variable (a). Afterward, the PC is incremented to point to the next instruction, which is similar, so we won’t discuss it.

Finally, for the instruction result= a * b. The instruction is fetched and stored in the CIR. Then, the CPU decodes the multiplication operation (opcode) and identifies a and b as operands. Finally, the CPU retrieves the values of a and b, performs the multiplication using the ALU, and stores the result in the memory location assigned to result.

5. Conclusion

In this article, we discussed the basic instruction cycle of a computer, which is to fetch, decode, and execute an instruction. This cycle keeps repeating from the time we boot the system until it’s switched off.


原始标题:Introduction to the Fetch-Execute Cycle