Every program is executed by repeating one small routine. The processor fetches an instruction, works out what it means, and then carries it out. This routine is the instruction cycle, and it runs for every instruction of every program.
Phases of the Cycle
The instruction cycle has four phases: fetch the instruction from memory, decode it, read the effective address if the address is indirect, and execute the instruction. After the execution the cycle starts again for the next instruction, unless the computer has halted.
Definition: Instruction cycle. The sequence of steps of fetching, decoding and executing an instruction that a processor repeats for every instruction of a program.
Fetch and Decode
The first steps are common to all instructions and are described by register transfers.
T0: AR ← PC. The address of the next instruction is moved to AR.
T1: IR ← M[AR], PC ← PC + 1. The instruction is read and the PC points to the following one.
T2: D0 to D7 ← decode IR (12 to 14), AR ← IR (0 to 11), I ← IR (15). The opcode is decoded and the address and mode are saved.
Indirect Address and Execution
If the instruction is memory-reference with I = 1, the step T3: AR ← M[AR] reads the effective address. After this, the instruction is executed during T4 and the following steps, and the sequence counter is then cleared.
mem = {0: ("LDA", 4), 1: ("ADD", 5), 2: ("STA", 6), 3: ("HLT", 0),
4: 10, 5: 7, 6: 0}
pc, ac = 0, 0
while True:
op, ad = mem[pc]
pc += 1
if op == "LDA": ac = mem[ad]
elif op == "ADD": ac += mem[ad]
elif op == "STA": mem[ad] = ac
else: break
print(op, ad, "AC =", ac, "PC =", pc)
Output
LDA 4 AC = 10 PC = 1
ADD 5 AC = 17 PC = 2
STA 6 AC = 17 PC = 3
Example 3.3: Following three instructions
Problem: A program at addresses 0 to 3 holds LDA 4, ADD 5, STA 6 and HLT. Memory 4 holds 10 and memory 5 holds 7. Find the final value stored at 6.
Solution:
- LDA 4 loads 10 into AC, and PC becomes 1.
- ADD 5 adds 7 to AC, which becomes 17, and PC becomes 2.
- STA 6 stores 17 at address 6, and PC becomes 3.
- HLT stops the computer.
Result: Memory location 6 holds 17.
Common mistakes
- Forgetting that the PC is incremented during the fetch, not during the execution.
- Executing the instruction before it has been decoded.
- Assuming that every instruction needs the indirect step. It applies only to memory-reference instructions with I = 1.
Key takeaways
- The cycle is fetch, decode, indirect address if needed, execute.
- T0: AR ← PC; T1: IR ← M[AR], PC ← PC + 1; T2: decode.
- The PC is incremented after the fetch.
- The cycle repeats until a halt.
Practice questions
- What happens during T1 of the instruction cycle?
- When is the indirect step T3 needed?
- What is the purpose of clearing SC at the end of an instruction?
Answers to the practice questions
- The instruction is read from memory into IR and the PC is incremented.
- For a memory-reference instruction with I = 1.
- To start the next instruction at T0.
Sources
- Official BCA syllabus Computer Architecture, course code 25BCA401DS02, Unit III
