OCR H446, section 1.2.1 (c)
An interrupt is a signal to the processor that something has happened which needs attention now. The processor pauses the program it is running, deals with the event, and then carries on where it left off.
Without interrupts, the processor would have to keep checking (polling) every device to see whether it needs anything, which wastes time. With interrupts, devices tell the processor when they need it.
Causes of interrupts
| Source | Examples |
|---|---|
| Hardware | A key is pressed; the mouse moves; a disk finishes reading; a network packet arrives; a printer runs out of paper; a timer goes off; a power failure |
| Software | A program requests a service from the OS; an error such as dividing by zero or using an invalid memory address; a page fault |
| User | Pressing a reset or break key |
The timer interrupt is vital: it lets the operating system take control at regular intervals, so it can switch between programs (scheduling, 1.2.1 (d)).
Interrupt service routines (ISRs)
An interrupt service routine (ISR), or interrupt handler, is a short piece of code that deals with one particular type of interrupt. For example, the keyboard ISR reads the key that was pressed and puts it in a buffer.
The processor uses a table (the interrupt vector table) that holds the address of the ISR for each type of interrupt, so it can find the right one quickly.
The effect on the fetch-decode-execute cycle
Normally the processor repeats fetch, decode and execute. Interrupts add a step:
- At the end of each fetch-decode-execute cycle, the processor checks whether any interrupt is waiting.
- If there isn't, it carries straight on with the next instruction.
- If there is, and its priority is higher than the current job, the processor saves the state of the running program.
- The PC is loaded with the address of the ISR, which then runs.
- When the ISR has finished, the saved state is restored, and the interrupted program resumes from the instruction after the one that finished.
The current instruction is completed first, so it is not left half done.
Saving and restoring the state (context)
The ISR uses the same registers as the interrupted program, so it would overwrite their values. The processor must therefore save the context:
- the program counter (where to resume);
- the contents of registers such as the accumulator and others;
- the status flags.
These are pushed onto a stack in memory (the stack works well because interrupts can be nested, with the most recent one finished first). After the ISR, the values are popped and restored, so the original program carries on as if nothing had happened.
Priorities
Interrupts have priorities. A more urgent interrupt (such as a power failure) can interrupt a lower priority ISR, while a lower priority interrupt must wait until the more important work is finished. Interrupts that are waiting are held in a queue.
Why are interrupts useful?
- The processor doesn't waste time polling devices.
- It can respond quickly to urgent events.
- It allows multitasking, through the timer interrupt.
- Slow devices (printers, disks) can work while the processor does other things, and interrupt when done.
Worked example
Task: a program is adding up a list of numbers when the user presses a key. Describe what happens.
- The keyboard controller sends an interrupt signal.
- The processor finishes the current instruction, and at the end of the cycle detects the interrupt.
- It pushes the PC, the accumulator and the other registers onto the stack.
- The PC is set to the address of the keyboard ISR, which reads the key and stores it in a buffer.
- The saved values are popped from the stack, and the adding program resumes exactly where it stopped.
Exam tip: state that the processor checks for interrupts at the end of each cycle, that the context (registers and PC) is saved, that the ISR runs, and that the context is then restored. Give the reason for saving: so the interrupted program continues correctly.
Check yourself
What is an interrupt?
A signal to the processor that something needs attention, so that it temporarily stops its current task to deal with the event.
What is an ISR?
An interrupt service routine: the code that deals with a particular type of interrupt.
When does the processor check for interrupts?
At the end of each fetch-decode-execute cycle.
Why must the registers be saved before an ISR runs?
The ISR will use the registers and overwrite their values, so they must be saved so that the interrupted program can be restored and carry on correctly.