AQA 7517, section 4.7.3.6
An interrupt is a signal to the processor that something needs urgent attention. The processor pauses what it is doing, deals with the event, and then carries on from where it left off.
Interrupts mean the processor doesn't waste time polling (checking over and over whether a device needs it), and can respond quickly to events.
Sources of interrupts
- Hardware: a key is pressed or the mouse moves; a disk or printer finishes a job; a network packet arrives; a timer goes off (used for multitasking); a power failure.
- Software: a program does something that needs the operating system, or causes an error such as dividing by zero or accessing invalid memory.
- User: pressing a reset key or a break key.
Interrupt service routine (ISR)
An interrupt service routine (ISR), also called an interrupt handler, is the special piece of code that deals with a particular interrupt. Each kind of interrupt has its own ISR, found through a table that lists their addresses. For example, the keyboard ISR reads the key that was pressed and stores it in a buffer.
Effect on the fetch-execute cycle
Normally the processor repeats fetch, decode and execute. With interrupts:
- At the end of each fetch-execute cycle, the processor checks whether any interrupt has been signalled.
- If there is one, and its priority is higher than the program that is running, the processor stops the current program and runs the ISR.
- When the ISR finishes, the processor returns to the program and continues with the next instruction.
Steps when an interrupt occurs
- The current instruction finishes.
- The processor sees the interrupt at the end of the cycle.
- It saves the volatile environment of the running program.
- The PC is set to the address of the ISR, so the ISR runs.
- When it ends, the saved environment is restored.
- The interrupted program carries on as if nothing had happened.
Saving the volatile environment
The volatile environment is the state of the processor that the interrupted program needs, which would be overwritten by the ISR:
- the program counter (where to resume);
- the contents of the registers (general-purpose and others);
- the status register (the flags).
This must be saved (usually pushed onto the stack in memory) before the ISR runs, because the ISR uses the same registers. If it weren't saved, the interrupted program would resume with wrong values, and give wrong results or crash. After the ISR, the values are restored (popped from the stack).
Priorities
Interrupts have priorities. A higher priority interrupt (such as a power failure) can interrupt a lower priority ISR, and a lower priority interrupt must wait. Stacks make this possible, because several saved environments can be held one on top of another.
Why use interrupts?
- The processor doesn't waste time checking devices.
- It can respond immediately to urgent events.
- It allows multitasking, since a timer interrupt lets the operating system switch between programs.
- It lets the processor run while slow devices (printers, disks) work.
Worked example
Task: a program is adding numbers when the user presses a key. Describe what happens.
- The keyboard sends an interrupt signal.
- The processor finishes the current instruction, then checks for interrupts at the end of the fetch-execute cycle.
- It saves the PC, registers and status register (the volatile environment) on the stack.
- The PC is loaded with the address of the keyboard ISR, which reads the key and stores it in a buffer.
- The saved values are restored, and the adding program resumes exactly where it stopped.
Exam tip: mention that the interrupt is checked at the end of each fetch-execute cycle, that the volatile environment is saved (and say why: so the interrupted program can resume correctly), that the ISR runs, and that the environment is restored afterwards.
Check yourself
What is an interrupt?
A signal sent to the processor to say that something needs urgent attention, so the processor temporarily stops what it is doing to deal with it.
What is an ISR?
An interrupt service routine, the piece of code that deals with a particular interrupt.
Why must the volatile environment be saved before an ISR runs?
The ISR will use the registers, overwriting their values. Saving the program counter, registers and status flags allows the interrupted program to resume correctly afterwards.
When does the processor check for interrupts?
At the end of each fetch-execute cycle.