14.3 Instruction Cycle
COS2621 - Computer Organisation · 14. Processor Structure and Function
14. Processor Structure and Function
14.3 Instruction Cycle
The instruction cycle is the process by which a computer retrieves, decodes, and executes instructions from memory. This cycle is fundamental for the operation of the CPU (Central Processing Unit) and consists of three main phases: fetch, decode, and execute.
Fetch Phase
During the fetch phase, the CPU retrieves an instruction from memory. The address of the instruction is stored in the program counter (PC). After fetching the instruction, the PC is incremented to point to the next instruction. The fetched instruction is then stored in the instruction register (IR).
Example of Fetch Phase: Assume the program counter (PC) has the value 3000. The CPU retrieves the instruction located at memory address 3000. If the instruction is "SUB AX, 5h", it is loaded into the instruction register (IR), and the PC is updated to 3001.
Watch out: Students often confuse the roles of the program counter and instruction register. The program counter holds the address of the next instruction, while the instruction register holds the current instruction being executed.
Decode Phase
In the decode phase, the CPU interprets the instruction stored in the IR. This involves determining the operation to be performed and identifying the operands involved. The control unit generates the necessary control signals to execute the instruction.
Example of Decode Phase: Continuing from the previous example, the instruction "SUB AX, 5h" indicates that the CPU should subtract the value 5 from register AX. The control unit decodes this instruction and prepares the necessary signals to perform the subtraction operation.
Execute Phase
During the execute phase, the actual operation specified by the instruction is performed. This may involve arithmetic operations, data movement, or control operations.
Example of Execute Phase: For the instruction "SUB AX, 5h", if the instruction is executed, the value in AX will now be decreased by 5.
Instruction Cycle Timing
The time taken to complete one instruction cycle depends on factors such as CPU clock speed and the complexity of the instruction. Simpler instructions typically require one cycle, while more complex instructions may take multiple cycles. The processing required for a single instruction is called an instruction cycle.
Example of Timing Calculation: Consider the following code:
mov bx, 10h
add ax, bx
sub bx, 3hAssume the following:
- The machine has a 2 GHz clock.
- Four cycles are needed to fetch an instruction from memory.
- Addition takes 2 clock cycles.
- Subtraction takes 2 clock cycles.
- The mov instruction takes 3 cycles.
To calculate the total time taken:
- Fetch "mov bx, 10h": 4 cycles (fetch) + 3 cycles (execute) = 7 cycles
- Fetch "add ax, bx": 4 cycles + 2 cycles = 6 cycles
- Fetch "sub bx, 3h": 4 cycles + 2 cycles = 6 cycles
Total Cycles: 7 + 6 + 6 = 19 cycles.
Time per cycle: 1 / (2 × 10^9) seconds = 0.5 ns.
Total Time: 19 cycles × 0.5 ns = 9.5 ns.
Remember: The three main phases of the instruction cycle are fetch, decode, and execute.
Check your understanding
- What are the three phases of the instruction cycle?
- What role does the program counter play in the instruction cycle?
- During which phase is the instruction actually executed?
- Explain the difference between the instruction register and the program counter.
Test yourself
5 quick questions on this lesson. Pick an answer to see if you're right.
1. The is an area in memory that is used to save data and addresses that need to be temporarily stored while the program is executing.
2. The processing required for a single instruction is called a(n) __________ cycle.
3. What is the first phase of the instruction cycle?
4. Which register holds the address of the next instruction to be executed?
5. Which one of the following memory organisations is impossible? Explain why the others are possible.