Sub topic 1
Recap: the instruction cycle
Prerequisites
This chapter assumes that you know what a CPU, register, memory, clock cycle, and simple assembly instruction are. You should also recognize instructions such as add, lw, sw, and conditional branch instructions.
An instruction cycle is the sequence of actions the processor performs for one instruction. In a simple sequential processor, the CPU completes one instruction before starting the next. Pipelining later allows parts of several instruction cycles to overlap.
The main steps
The exact implementation differs between processors, but the basic instruction cycle has these steps:
- Fetch: The CPU reads the instruction from memory.
- Decode: The control unit determines what the instruction means.
- Read operands: The CPU obtains input values from registers or memory addresses.
- Execute: The arithmetic logic unit, or ALU, performs the required operation.
- Access memory: A load reads memory, while a store writes memory. Instructions that do not use memory may skip this action.
- Write back: The result is written to a destination register.
- Update the program counter: The program counter, or PC, is changed to identify the next instruction.
The PC is a register that holds the address of the next instruction to fetch. For a normal sequential instruction, the processor increases the PC by the instruction length. A branch or jump replaces the normal next address with a target address.
The instruction register, or IR, temporarily holds the fetched instruction. During decoding, the control unit separates the operation code, called the opcode, from fields such as source-register numbers, destination-register numbers, and an immediate value.
A worked example
Consider this instruction:
add r1, r2, r3Assume that the instruction means r1 = r2 + r3.
- The PC supplies the instruction address.
- Memory returns the encoded
addinstruction. - The decoder identifies the operation as addition.
- The register file supplies the values in
r2andr3. - The ALU adds those values.
- The result is written into
r1. - The PC moves to the next instruction.
A register file is the processor's collection of fast registers. A register read supplies an operand, and a register write stores a result.
Sequential execution versus pipelining
In sequential execution, the processor waits until all steps for one instruction finish. In a pipeline, these steps are divided into stages. While one instruction executes, another can be decoded and a third can be fetched.
This overlap improves throughput, meaning the number of completed instructions per unit of time. It does not usually reduce the amount of work required by one instruction. The overlap also creates hazards when instructions compete for hardware, depend on one another, or change the control flow.
Exercise
For lw r1, 0(r2), which step obtains the data value that will eventually be written into r1?
Show answer
The memory-access step reads the data from the address formed using r2 and the offset 0. The write-back step then stores that value in r1.