Skip to main content

1.6 Instruction Cycle & Interrupts

An instruction cycle defines the sequential steps a processor performs to execute a single machine instruction. This chapter explores the timing subcycles (Fetch, Decode, Execute, and Interrupt), details the T-state clock-level register transfers, classifies hardware and software interrupts, and traces the CPU stack execution steps during the Interrupt Service Routine (ISR) sequence.

Learning Objectives

  • Define the primary phases of the instruction cycle (Fetch, Decode, Execute, Interrupt) and identify categories of interrupts.
  • Explain the step-by-step CPU hardware actions required to transition from a running program to an Interrupt Service Routine (ISR) and return.
  • Trace the contents of the Program Counter (PC), Stack Pointer (SP), and Stack Memory during nested interrupt execution.
  • Contrast vectored interrupts with polled interrupts in terms of latency, response time, and hardware lines.
  • Evaluate the impact of interrupt cycles and ISR execution overhead on overall CPU performance.
Prerequisites

Before studying this chapter, you should review Chapter 1.3: Register Organization to understand the CPU registers involved in instruction execution (PC, IR, MAR, MBR, SP, PSW).

1. Introduction to Instruction Processing

At its most fundamental level, CPU execution consists of a continuous cycle of fetching and executing instructions. This continuous loop is called the instruction cycle. The time required to process a single instruction is divided into major subcycles, which are further divided into clock periods or **T-states** (Timing States).

2. The Instruction Cycle Subcycles

An instruction cycle is divided into four main sequential subcycles:

  1. Fetch Subcycle: The CPU reads an instruction from memory.
  2. Decode Subcycle: The Control Unit decodes the opcode in the Instruction Register (IR) to identify the operation and locate any operands.
  3. Execute Subcycle: The CPU performs the designated operation (e.g., ALU operations, memory read/write, branching).
  4. Interrupt Subcycle: If interrupts are enabled and pending, the CPU saves its current state and branches to the service routine.

2.1 Timing States (T-States) in the Fetch Cycle

To trace the exact timing transitions of the Fetch subcycle, let's examine the Register Transfer Language (RTL) actions during the first three clock periods (T-states):

  • State $T_1$ (Address Placement): The contents of the Program Counter (PC) are transferred to the Memory Address Register (MAR) via the internal bus: $$T_1:\text{MAR} \leftarrow \text{PC}$$
  • State $T_2$ (Memory Read & PC Increment): The Control Unit asserts the memory read command line. The memory subsystem decodes the address in MAR and places the fetched instruction word onto the data bus. Simultaneously, the CPU increments the PC to point to the next instruction: $$T_2:\text{MBR} \leftarrow \text{Memory}, \quad \text{PC} \leftarrow \text{PC} + 1$$
  • State $T_3$ (Instruction Loading): The fetched instruction byte/word is moved from the Memory Buffer Register (MBR) into the Instruction Register (IR) for decoding: $$T_3:\text{IR} \leftarrow \text{MBR}$$
[Figure 1.6.1: Complete Instruction Cycle Timeline]
  ┌───────────────────────────────── Fetch Subcycle ─────────────────────────────────┐
  │  State T1: PC ──► MAR                                                            │
  │  State T2: Memory ──► MBR, PC ──► PC + 1                                         │
  │  State T3: MBR ──► IR                                                            │
  └───────────────────────────────────────┬──────────────────────────────────────────┘
                                          ▼
                               ┌──────────────────────┐
                               │   Decode Opcode      │
                               └──────────┬───────────┘
                                          ▼
                               ┌──────────────────────┐
                               │   Execute Cycle      │
                               └──────────┬───────────┘
                                          ▼
                               ┌──────────────────────┐
                               │   Interrupt Check    │
                               └──────────┬───────────┘
                                          │  Interrupt Pending?
                                          ├─────────────────────────┐ Yes
                                          │ No                      ▼
                                          ▼                 ┌───────────────┐
                                  [Next Instruction]        │  Interrupt    │
                                  (Repeat T1-T3 Fetch)      │  Subcycle     │
                                                            └───────────────┘
                                
Figure 1.6.1: Clock-state and phase sequence of the instruction execution pipeline.

3. CPU Interrupt Classifications

An interrupt is an electrical signal or software instruction that temporarily suspends normal program execution, forcing the CPU to branch to a specialized routine. Interrupts are classified as:

  • Hardware vs. Software:
    • Hardware Interrupt: Issued by external physical I/O modules (e.g. keyboard stroke, disk write completion) using dedicated processor pins.
    • Software Interrupt: Programmed trap instructions (e.g., `INT 21h` in x86) used to request Operating System services.
  • Maskable vs. Non-Maskable (NMI):
    • Maskable: Can be disabled (masked) by clearing the Interrupt Enable (IE) flag bit inside the status register (PSW). The CPU ignores these requests while executing time-critical code blocks.
    • Non-Maskable (NMI): Cannot be disabled by software. Reserved for high-priority catastrophic hardware faults (e.g., power failure, parity memory error).
  • Vectored vs. Non-Vectored (Polled):
    • Vectored: The requesting device provides a vector address index. The CPU reads this vector to locate the service routine directly in the Interrupt Vector Table (IVT).
    • Non-Vectored: All devices share a single interrupt pin. The CPU branches to a generic routine that poles (queries) every device sequentially to find the source.

4. The Interrupt Service Routine (ISR) Sequence

When the CPU detects an active, unmasked interrupt request at the end of an instruction cycle, it enters the **Interrupt Subcycle**. The CPU executes the following hardware actions:

  1. State Saving (Hardware Phase): The CPU must save its current context so it can resume the program later. It decrements the Stack Pointer (SP) and pushes the Program Counter (PC) and Program Status Word (PSW) onto the stack: $$T_{int1}: \text{SP} \leftarrow \text{SP} - 1, \quad \text{Memory}[\text{SP}] \leftarrow \text{PC}_{high}$$ $$T_{int2}: \text{SP} \leftarrow \text{SP} - 1, \quad \text{Memory}[\text{SP}] \leftarrow \text{PC}_{low}$$ $$T_{int3}: \text{SP} \leftarrow \text{SP} - 1, \quad \text{Memory}[\text{SP}] \leftarrow \text{PSW}$$
  2. Interrupt Acknowledge & Vector Lookup: The CPU asserts the Interrupt Acknowledge (INTA) control line. It reads the interrupt vector index from the data bus, references the Interrupt Vector Table (IVT) in memory, and loads the target ISR start address into the PC: $$\text{PC} \leftarrow \text{IVT}[\text{Vector}]$$
  3. ISR Execution (Software Phase): The CPU begins executing the ISR instructions at the address loaded in PC. The first instructions in the ISR typically save any general-purpose registers (GPRs) onto the stack (using `PUSH`).
  4. Return from Interrupt (RETI): The final instruction of the ISR is always a Return from Interrupt (`RETI`) instruction. When executed: - The CPU restores any saved GPRs (using `POP`). - The CPU pops the PSW and PC off the stack: $$\text{PSW} \leftarrow \text{Memory}[\text{SP}], \quad \text{SP} \leftarrow \text{SP} + 1$$ $$\text{PC} \leftarrow \text{Memory}[\text{SP}], \quad \text{SP} \leftarrow \text{SP} + 2$$ - The CPU resumes the interrupted program at the restored PC address.

5. Comprehensive Solved Examples

Cycle Example 1.6.1

Stack Tracing during Nested Interrupts

Problem: An 8-bit CPU has a Stack Pointer initialized to `0x00FF`. It is executing a main program. The CPU is interrupted at PC = `0x15A4` (with PSW = `0x80`) by a Level 1 interrupt (ISR1 start address = `0x3000`). While executing ISR1 at PC = `0x3012` (with PSW = `0x81`), a higher-priority Level 2 interrupt occurs (ISR2 start address = `0x4000`). Trace the Stack Pointer and Stack Memory contents at each phase of this nested interrupt sequence.

Solution:

  1. Initial State: - $\text{PC} = \text{Main Program}$ - $\text{SP} = 0\text{x}00\text{FF}$
  2. Level 1 Interrupt Occurs (PC = `0x15A4`, PSW = `0x80`): - The CPU saves PC (high byte `0x15`, low byte `0xA4`) and PSW (`0x80`) on the stack: - $\text{SP} \leftarrow 0\text{x}00\text{FE}$; $\text{Memory}[0\text{x}00\text{FE}] \leftarrow 0\text{x}15$ (PC High) - $\text{SP} \leftarrow 0\text{x}00\text{FD}$; $\text{Memory}[0\text{x}00\text{FD}] \leftarrow 0\text{xA}4$ (PC Low) - $\text{SP} \leftarrow 0\text{x}00\text{FC}$; $\text{Memory}[0\text{x}00\text{FC}] \leftarrow 0\text{x}80$ (PSW) - $\text{PC} \leftarrow 0\text{x}3000$ (branches to ISR1).
  3. Level 2 Interrupt Occurs (during ISR1 at PC = `0x3012`, PSW = `0x81`): - The CPU suspends ISR1 and saves PC (high byte `0x30`, low byte `0x12`) and PSW (`0x81`) onto the stack: - $\text{SP} \leftarrow 0\text{x}00\text{FB}$; $\text{Memory}[0\text{x}00\text{FB}] \leftarrow 0\text{x}30$ (PC High) - $\text{SP} \leftarrow 0\text{x}00\text{FA}$; $\text{Memory}[0\text{x}00\text{FA}] \leftarrow 0\text{x}12$ (PC Low) - $\text{SP} \leftarrow 0\text{x}00\text{F}9$; $\text{Memory}[0\text{x}00\text{F}9] \leftarrow 0\text{x}81$ (PSW) - $\text{PC} \leftarrow 0\text{x}4000$ (branches to ISR2).
  4. Stack Memory Layout after ISR2 Entry:
      Address   | Data Value | Description
      ──────────┼────────────┼───────────────────────────
      0x00FF    |   (empty)  | Initial Stack Base
      0x00FE    |    0x15    | PC High (Main Program)
      0x00FD    |    0xA4    | PC Low (Main Program)
      0x00FC    |    0x80    | PSW (Main Program)
      0x00FB    |    0x30    | PC High (ISR1)
      0x00FA    |    0x12    | PC Low (ISR1)
      0x00F9    |    0x81    | PSW (ISR1)   <─── SP points here (0x00F9)
                                            

Answer: The stack pointer decrements from `0x00FF` to `0x00F9`. The stack preserves the return parameters to cleanly return to ISR1 first, and then to the main program.

Solved Exam Question 1.6.1

T-State Timing Trace

Problem: Trace the register contents of a CPU during a fetch cycle if PC contains `0x2005` and memory location `0x2005` contains the instruction opcode `0x3E` (load instruction). State register contents at $T_1, T_2, T_3$.

Solution:

  • Initial State: $\text{PC} = 0\text{x}2005$, $\text{Memory}[0\text{x}2005] = 0\text{x}3\text{E}$.
  • State $T_1$: - Action: $\text{MAR} \leftarrow \text{PC}$. - Registers: $\text{MAR} = 0\text{x}2005$, $\text{PC} = 0\text{x}2005$.
  • State $T_2$: - Action: $\text{MBR} \leftarrow \text{Memory}[\text{MAR}]$, $\text{PC} \leftarrow \text{PC} + 1$. - Registers: $\text{MBR} = 0\text{x}3\text{E}$, $\text{PC} = 0\text{x}2006$.
  • State $T_3$: - Action: $\text{IR} \leftarrow \text{MBR}$. - Registers: $\text{IR} = 0\text{x}3\text{E}$, $\text{MBR} = 0\text{x}3\text{E}$, $\text{PC} = 0\text{x}2006$.

Answer: At the end of $T_3$, the Instruction Register contains the fetched opcode `0x3E`, and the PC is successfully incremented to `0x2006`.

6. Core Comparison Tables

Table 1.6.3: Vectored vs. Polled (Non-Vectored) Interrupts

Feature Vectored Interrupts Polled (Non-Vectored) Interrupts
ISR Address Source Supplied directly by the interrupting hardware device. Determined by software querying every device address.
Interrupt Latency Very Low (direct branch via IVT) High (polling loop scan takes time)
Wiring Complexity High (Needs vector address bus support) Low (All devices share one interrupt line)
Arbitration Method Priority Resolver hardware. Polled software check order.

7. Common Mistakes Students Make

🎯 Key Takeaways

  • The **Instruction Cycle** is composed of Fetch, Decode, Execute, and Interrupt subcycles.
  • The **Fetch cycle T-states** are: $T_1:\text{MAR} \leftarrow \text{PC}$; $T_2:\text{MBR} \leftarrow \text{Memory}, \text{PC} \leftarrow \text{PC}+1$; $T_3:\text{IR} \leftarrow \text{MBR}$.
  • **Interrupts** are classified by hardware/software origin, maskable/NMI settings, and vectored/polled addressing.
  • To service an interrupt, the CPU executes a hardware state save (pushing PC and PSW to stack) before fetching the vector address of the ISR.

🔗 Connection to Future Chapters

The T-state sequencing and interrupt states studied here provide the foundation for developing the physical Control Unit state machines in **Chapter 2.3** (Hardwired Control) and the PIC chip details in **Chapter 3.9**.

8. Assessment Bank

8.1 Multiple Choice Questions

Q1. Which register transfer describes the actions that occur during timing state $T_1$ of a standard instruction fetch subcycle?
Q2. At what point in the instruction cycle does the CPU check for active hardware interrupt requests?
Q3. Which type of interrupt cannot be masked or disabled by software instructions?
Q4. When the CPU executes the Return from Interrupt (RETI) instruction, what actions occur?

8.2 Short Answer Questions

  1. Trace the Register Transfer Language (RTL) operations that occur during the Fetch subcycle timing states ($T_1, T_2, T_3$).
  2. Contrast Vectored Interrupts and Polled Interrupts. Which has lower latency?
  3. Why must the CPU save the Program Status Word (PSW) on the stack during an interrupt?

8.3 Long Answer / Exam Questions

  1. Detail the complete hardware and software sequence that occurs when an external hardware interrupt is asserted. Trace the register modifications, stack pointer adjustments, and memory accesses step-by-step from request to RETI.
  2. A CPU has a 16-bit address space, stack pointer initialized to `0x3FF0`, and is executing code at address `0x2050`. An interrupt occurs, causing the CPU to branch to an ISR. The ISR itself is then interrupted by a higher-priority interrupt at ISR address `0x5010`. Draw a diagram of the stack memory showing all pushed values and register contents (PC, SP) at the peak of the nested interrupts.

8.4 Viva Voce Questions

  1. "What is the difference between a software interrupt instruction (e.g. INT) and a standard subroutine call (e.g. CALL)?" Model Answer: A `CALL` instruction only saves the return address (PC) on the stack and branches to a user-defined subroutine address within the program space. An `INT` instruction (software interrupt) saves both the PC and the PSW flags, disables interrupts (clearing the IE flag to prevent conflicts), and branches via the Interrupt Vector Table (IVT) to an operating system service routine.
  2. "Why do processors decrement the Stack Pointer before writing to stack memory?" Model Answer: This is the convention of a downward-growing stack. The Stack Pointer (SP) points to the current active data byte at the top of the stack. To write new data, the CPU must first decrement SP to point to the next free memory location, then write the data byte.

References

  1. IUST COA Syllabus, CS-301 course structure, Topic 6: "Instruction Cycle & Interrupts." [Local Verified]
  2. COA Class Notes, Timing diagrams and interrupt sequences, Pages 8–15. [Local Verified]
  3. Handwritten COA Notes, T-states and stack pushes, Pages 13–20. [Local Verified]
  4. William Stallings, Computer Organization and Architecture: Designing for Performance, 11th Edition, Pearson, 2019. Chapter 3: System Buses & Chapter 14: Instruction Cycles. [Pending Verification]
  5. Prof. Kamalika Datta, Instruction Cycle and Interrupts, NPTEL Lectures 12: Computer Architecture. [Pending Verification]