Sub topic 5
What happens when an interrupt occurs
This chapter assumes you know the instruction cycle (fetch, decode, execute), how the stack and subroutine call/return work, and the idea of interrupt-driven I/O from the previous chapter. Here you follow one interrupt from the device's request signal to the first instruction after the return. GATE questions often ask for the exact order of these steps, and for what the hardware does versus what software does.
The interrupt cycle
An interrupt is a signal that asks the CPU to stop its current program and run a special routine. The routine is called an interrupt service routine (ISR), or interrupt handler. The CPU checks for pending interrupts at one fixed point: the end of each instruction. It does not stop in the middle of an instruction.
The instruction cycle therefore has an extra step:
- Fetch the instruction.
- Decode it.
- Execute it.
- If interrupts are enabled and a request is pending, run the interrupt cycle.
The interrupt cycle is a hardware sequence. It is not part of any program. The CPU performs it automatically when it accepts a request.
Steps of the interrupt cycle
The sequence below is the standard textbook model. Details differ between processors, but the order of ideas is the same.
- The device raises a request. The device sets its interrupt request line (IRQ) when it has data ready or has finished an operation.
- The CPU finishes the current instruction. It does not abandon it. This keeps the machine state consistent.
- The CPU acknowledges the interrupt. It sends an interrupt acknowledge signal (INTA). In vectored systems, the device answers with an identifier.
- The CPU saves the return address. The program counter (PC) holds the address of the next instruction of the interrupted program. The hardware pushes it onto the stack.
- The CPU saves the processor status. The flags or program status word (PSW) are also saved, usually on the stack. Many processors do this in hardware, because the ISR would change the flags and the interrupted program would then misbehave.
- The CPU disables further interrupts, if the design requires it. This prevents a second interrupt from corrupting the half-saved state.
- The CPU loads the PC with the ISR address. The address comes from a fixed location, from a vector supplied by the device, or from a vector table indexed by the device number.
- The ISR runs. It saves any other registers it will use, serves the device, and restores those registers.
- The ISR ends with a return-from-interrupt instruction. This instruction pops the saved status and the return address, so the interrupted program resumes at the instruction after the one that was completed.
Steps 1 to 7 are done by hardware. Step 8 is software. Step 9 is one instruction, but it undoes hardware work.
What must be saved, and who saves it
The key idea is that the interrupted program must not notice the interruption. Anything the ISR may change must be restored.
| Item | Saved by | Why |
|---|---|---|
| Program counter | Hardware | Without it the CPU cannot find the place to resume |
| Status flags (PSW) | Usually hardware | The ISR's arithmetic would overwrite them |
| General-purpose registers | The ISR, in software | Hardware cannot know which ones the ISR will use |
| Memory contents | Nobody | The ISR must not overwrite the interrupted program's data |
On GATE questions, assume the PC is always saved by hardware unless the question says otherwise. Whether general registers are saved by hardware or software depends on the wording of the question. If the question says "context switch" or "all registers saved," count those cycles too.
Why the PC goes on the stack
An interrupt can arrive at any point, including inside another ISR. A single fixed save location would be overwritten by the second interrupt. A stack stores return addresses in last-in, first-out order, so nested interrupts unwind correctly. This is the same reason subroutine calls use a stack.
A few older or simple designs save the return address in a fixed memory location or a dedicated register. Such a design cannot nest interrupts safely unless the ISR copies the value elsewhere first.
Interrupt versus subroutine call
An interrupt resembles a call instruction, but there are differences you should know.
| Feature | Subroutine call | Interrupt |
|---|---|---|
| Trigger | An instruction in the program | An external event, asynchronous to the program |
| When it happens | At a known point in the code | After any instruction |
| Saved by hardware | PC only | PC, and usually the PSW |
| Target address | Given in the instruction | Fixed, or found through a vector |
| Return instruction | `RET` | `IRET` or `RETI` |
A software interrupt, such as a system call instruction, is an exception to the "asynchronous" row. It is triggered by an instruction and so happens at a known point, but it uses the same save and vector mechanism.
Finding the ISR: three ways
The CPU must learn which routine serves the device. Designs use one of three approaches.
- Single fixed address. Every interrupt goes to one routine. That routine polls the devices to find the source. This is simple but slow.
- Vectored interrupt. The device supplies a vector, a number or address that identifies it. The CPU uses it to jump to the right ISR with no polling.
- Vector table. The vector is an index into a table of ISR addresses in memory, called the interrupt vector table. The CPU computes the table entry address, reads the ISR address, and loads it into the PC.
A worked example makes the table case concrete. Suppose the table starts at address 0x0000, each entry is 4 bytes, and a device supplies vector number 5.
table_base = 0x0000
entry_size = 4
vector = 5
entry_address = table_base + vector * entry_size
print(hex(entry_address))The output is 0x14. The CPU reads the 4-byte ISR address stored at 0x14 and loads it into the PC. GATE questions on this pattern ask for the entry address, or the table size for a given number of vectors. Table size is the number of vectors times the entry size.
Interrupt latency
Interrupt latency is the time from the device raising the request to the first ISR instruction starting. It has these parts:
- The wait for the current instruction to finish. A long instruction, such as a block move or a division, increases this.
- The acknowledge handshake, and the vector fetch if used.
- The hardware save of the PC and PSW, which includes memory writes to the stack.
- Any time spent with interrupts disabled, such as when a higher-priority ISR is running.
Worst-case latency is what matters for real-time systems. If a question gives "the longest instruction takes N cycles," the worst case is when the request arrives just after that instruction starts, so the wait is nearly N cycles.
The total cost of one interrupt includes the latency, the ISR body, and the return. When you compute CPU overhead in a later chapter, you will often be given one lumped figure, such as "1000 cycles per interrupt." That figure covers all of these parts.
Nested interrupts and the enable flag
By default the hardware disables interrupts when it enters the ISR. A short ISR can run to its end with no risk. A long ISR makes other devices wait, and a fast device may lose data if its request is not served in time.
To allow nesting, the ISR can re-enable interrupts after it has saved what it needs. Then a higher-priority request can interrupt the ISR. The second PC and PSW are pushed above the first, and the returns unwind in reverse order.
Re-enabling too early is a common bug. If the ISR enables interrupts before it clears the device's request, the same device can interrupt again at once, and the stack grows with each repeat until it overflows.
Edge cases that GATE questions use
- The interrupt arrives during an instruction. It is held until that instruction completes. The saved PC points to the next instruction, not the one in progress.
- Interrupts are masked. The request stays pending, and the CPU serves it when the mask is cleared, as long as the device still holds the line. A level-sensitive line holds the request. An edge-triggered line may lose a pulse that came and went while masked.
- Two requests arrive together. The priority scheme decides the order. The next chapter covers it.
- Multi-cycle instructions that can be interrupted. Some designs allow interruption inside long string or block instructions and save partial progress. Most GATE questions assume interruption only between instructions.
- Exceptions versus interrupts. An exception, such as divide by zero or a page fault, comes from the running instruction. The saved PC may point to the faulting instruction, so that it can be retried after the fault is fixed. For an ordinary device interrupt, the saved PC points to the next instruction.
A tiny C model of the cycle
The program below simulates the stack behavior in software. It is a teaching model. The real steps happen in hardware.
#include <stdio.h>
#define STACK_SIZE 16
static unsigned int stack[STACK_SIZE];
static int sp = 0;
static void push(unsigned int v) { stack[sp++] = v; }
static unsigned int pop(void) { return stack[--sp]; }
int main(void) {
unsigned int pc = 0x1004; /* next instruction of the main program */
unsigned int psw = 0x02; /* example flag bits */
unsigned int isr_addr = 0x2000;
printf("Before interrupt: PC=0x%X PSW=0x%X\n", pc, psw);
/* hardware part of the interrupt cycle */
push(pc);
push(psw);
pc = isr_addr;
printf("In ISR: PC=0x%X, stack depth=%d\n", pc, sp);
/* return from interrupt: pop in reverse order */
psw = pop();
pc = pop();
printf("After return: PC=0x%X PSW=0x%X\n", pc, psw);
return 0;
}The output shows the PC and PSW identical before and after, with the stack depth of 2 inside the ISR. The pop order is the reverse of the push order. If an ISR pushed a different number of items than it popped, the return would load a wrong address, and the program would crash.
Common mistakes
- Saying the CPU interrupts an instruction midway. It checks only at instruction boundaries (except for the special designs noted above).
- Saying the ISR address is saved. The return address is saved, and the ISR address is loaded.
- Forgetting that the stack pointer must be valid. If the stack lives in memory that the interrupt cannot safely use, the push fails.
- Mixing up interrupt latency with ISR execution time. Latency ends when the ISR starts.
- Treating
RETIas the same asRET.RETIalso restores the status and may re-enable interrupts.
Exercise
A CPU saves only the PC and PSW on interrupt, each 4 bytes, onto a stack that starts at address 0x8000 and grows downward. The stack pointer holds the address of the last pushed item and is decremented before each push. A device interrupts when SP = 0x8000. What is SP after both items are saved, and what is it after a nested interrupt from a second device saves its own PC and PSW before the first ISR returns?
Show answer
Each interrupt pushes 2 items of 4 bytes, so 8 bytes. After the first interrupt, SP = 0x8000 - 8 = 0x7FF8. After the nested interrupt, SP = 0x7FF8 - 8 = 0x7FF0. The returns pop in reverse order, so SP goes back to 0x7FF8 and then to 0x8000.
Exercise
A vector table starts at address 0x0200 and has 4-byte entries. A device supplies vector number 12. At what address does the CPU read the ISR address? How large is the table if the system supports 64 vectors?
Show answer
The entry address is 0x0200 + 12 × 4 = 0x0200 + 0x30 = 0x0230. The table size is 64 × 4 = 256 bytes.
Exercise
A device raises an interrupt while the CPU is halfway through a 20-cycle instruction. Is the interrupt accepted immediately? What does the saved PC point to?
Show answer
No. The CPU finishes the 20-cycle instruction first and checks for interrupts at its end. The saved PC points to the instruction that follows the completed one, so the program resumes there after the ISR returns.