A1.1.5

Describe the fetch, decode and execute cycle

The fetch decode execute (FDE) cycle is the fundamental cycle of instruction execution — also called the instruction cycle.

5 min read899 words
  • Fetch: CPU fetches an instruction from memory (RAM).
  • Decode: CPU interprets the instruction into low-level operations it can execute.
  • Execute: CPU carries out the low-level operations specified by the decoded instruction.

Fetch Phase

CPU retrieves a machine-language instruction from main memory (RAM): CPU requests instruction from RAM; address goes to MAR; fetched instruction transfers to IR.

  1. Address Identification: PC’s address moves to MAR (internal CPU bus).
  2. Address Transmission: MAR puts address on the Address Bus (unidirectional, CPU → RAM).
  3. Command Issuance: CU asserts a READ\text{READ} signal on the Control Bus.
  4. Data Retrieval: RAM receives address + READ command, places instruction onto Data Bus (instructions and data both known as data).
  5. Data Reception: Instruction travels Data Bus (bidirectional) into MDR. → MDR holds instruction/data transferred to/from memory.
  6. Instruction Loading: Instruction transfers from MDR to IR for the rest of the cycle. → IR holds the current instruction being executed.
  7. Preparation: PC is incremented to the next instruction address (usually automatic).

Decode Phase

CPU interprets the machine-language instruction. Mostly done by the CU, which analyzes:

  • Opcode (operation code): dictates (规定) the type of operation
  • Operands: the data/address to be operated on
  • Addressing Modes: determine how to load the operands

Process — the CU interprets the instruction in IR and determines execution steps:

  1. Instruction Analysis: CU examines instruction in IR; parses Opcode (e.g., ADD, STORE\text{ADD, STORE}) and Operand.
  2. Control Signal Preparation: CU generates the micro-operation sequence (control signals) for Execute phase. Transmitted via Control Bus.
  3. Address Staging: If the instruction needs RAM data (e.g., ADD\text{ADD}) or writes back (e.g., STORE\text{STORE}), the operand address moves from IR into MAR. ACC remains static this phase.

Execute Phase

CPU performs the operation specified by the opcode. Common operations:

  • Arithmetic: addition, subtraction, multiplication, division (done by ALU).
  • Logical: AND\text{AND}, OR\text{OR}, NOT\text{NOT} (done by ALU).
  • Memory Access: load (read from memory to register) and store (write from register to memory).
  • Control: conditional/unconditional jumps that alter execution flow.

Process — CU coordinates components based on instruction type:

A. Data Transfer to/from RAM (e.g., LOAD\text{LOAD}, STORE\text{STORE})

  • MAR sends data address → Address Bus.
  • CU asserts READ\text{READ} or WRITE\text{WRITE}Control Bus.
  • LOAD\text{LOAD}: RAM → Data Bus → MDR → ACC.
  • STORE\text{STORE}: ACC → MDR → Data Bus → RAM.

B. Arithmetic/Logic Operations (e.g., ADD\text{ADD}, AND\text{AND})

  • Data sources (often ACC and MDR with data from RAM) supplied to ALU via internal CPU bus.
  • CU sends the operation command (e.g., ADD\text{ADD}) to ALU via Control Bus.
  • ALU performs the calculation.
  • Result written back to ACC, ready for next operation.

Cycle Completion: CU signals end of cycle; CPU loops back to Fetch using updated PC.

Examples of the FDE Cycle

(1) WRITE Value 0x7E to Address 0x4F2

1. FETCH Phase — Getting the Instruction

  1. [PC][\text{PC}] (0x100\text{0x100}) MAR\to \text{MAR} (Internal CPU Bus).
  2. [PC]+1PC[\text{PC}] + 1 \to \text{PC} (0x101\text{0x101}).
  3. [MAR][\text{MAR}] \to Address Bus.
  4. CU sends READ\text{READ} \to Control Bus.
  5. RAM[0x100]\text{RAM[0x100]} \to Data Bus MDR\to \text{MDR}.
  6. [MDR]IR[\text{MDR}] \to \text{IR} (Internal CPU Bus).

2. DECODE Phase — Setting Up the Write

  1. CU decodes STA\text{STA} from IR; identifies as WRITE\text{WRITE}.
  2. [IROperand][\text{IR}_{Operand}] (0x4F2\text{0x4F2}) MAR\to \text{MAR}.
  3. [ACC][\text{ACC}] (0x7E\text{0x7E}) MDR\to \text{MDR}.

3. EXECUTE Phase — Writing to Memory

  1. [MAR][\text{MAR}] (0x4F2\text{0x4F2}) \to Address Bus.
  2. CU sends WRITE\text{WRITE} \to Control Bus.
  3. [MDR][\text{MDR}] (0x7E\text{0x7E}) \to Data Bus.
  4. RAM receives address, command, and data.
  5. RAM[0x4F2]\text{RAM[0x4F2]} is set to 0x7E\text{0x7E}.

Cycle Complete

  • CU signals end of cycle.
  • CPU loops back to FETCH using [PC][\text{PC}] (0x101\text{0x101}).

(2) Comparison Instruction: CMP R1, R2\text{CMP R1, R2}

Compare R1 with R2. Result only sets CPU Status Flags (Zero, Carry, etc.); no register receives the output.

1. FETCH Phase

  1. [PC][\text{PC}] (0x300\text{0x300}) MAR\to \text{MAR}.
  2. [PC]+1PC[\text{PC}] + 1 \to \text{PC} (0x301\text{0x301}).
  3. [MAR][\text{MAR}] \to Address Bus.
  4. CU sends READ\text{READ} \to Control Bus.
  5. RAM[0x300]\text{RAM[0x300]} \to Data Bus MDR\to \text{MDR}.
  6. [MDR]IR[\text{MDR}] \to \text{IR} (CMP R1, R2\text{CMP R1, R2} now in IR).

2. DECODE Phase

  1. CU decodes CMP\text{CMP} from IR.
  2. Identifies it as an ALU op requiring R1 and R2.
  3. Prepares signals to use ALU for subtraction; result is not stored in a register like ACC.

3. EXECUTE Phase

  1. Preparation: [R1][\text{R1}] (1515) and [R2][\text{R2}] (1010) → ALU inputs. Comparison = subtraction: R1R2\text{R1} - \text{R2}.
  2. ALU Operation: CU sends SUBTRACT\text{SUBTRACT} → ALU via Control Bus.
  3. Result and Flags: ALU computes 1510=515 - 10 = 5.
    • Arithmetic result (55) is discarded.
    • The condition is used by ALU to set CPU Status Flags.
  4. Flag Setting (result = 5, positive, non-zero):
    • Zero Flag cleared (0).
    • Carry Flag (Borrow) cleared (0, unsigned).
    • Sign/Negative Flag cleared (0).
  5. Control Flow Enablement: these flags now enable conditional instructions (BGT\text{BGT}, BLE\text{BLE}, etc.) to alter PC based on this comparison.

Cycle Complete

  • CU signals cycle end.
  • CPU begins next FETCH from [PC][\text{PC}] (0x301\text{0x301}).

Start typing to search all published objectives.