AS Level · Topic 4.1
A-Level Computer Science 9618 / Topic 4 / AS

Excuse me, processor.

A printer runs out of paper, a key is pressed, a program tries to divide by zero. The processor cannot keep checking every device, so devices and programs send it a signal instead. Learn what causes an interrupt, when the processor notices it and exactly how it is handled.

The key idea

An interrupt is a signal from a device or a program to the processor, showing that something needs attention. Interrupts are checked at the end of each fetch-execute cycle. If there is one with a higher priority than the current task, the processor saves the contents of its registers (including the PC) on the stack, loads the start address of the interrupt service routine (ISR) into the PC and runs it. When the ISR finishes, the saved values are restored and the interrupted task carries on from where it stopped.

handling an interrupt main programF-E cycles main programcarries on ISR runsservices the device 1. check for interruptat end of the cycle 2. save PC and registers(on the stack) 4. restore registersand the PC 3. load the ISR start address into the PC a lower priority interrupt waits; a higher one may interrupt the ISR
Fig. 1: the processor leaves the main program, runs the ISR, then returns
Section 01

Why interrupt?

Without interrupts, the processor would have to keep checking every device to see whether it needed anything, wasting time. With interrupts, devices tell the processor when they need it, so it can get on with other work in the meantime.

SourceExamples of causes
HardwareA key is pressed or the mouse moves; a printer is out of paper or has finished printing; data has arrived at the network card; power is failing.
SoftwareA run-time error such as division by zero or an attempt to access memory the program does not own; a program requests an operating system service.
TimerThe clock signals that a program's time slice is over, so the operating system can switch to another process.
UserThe user presses a key combination to stop or break into a program.
Your turn
Give one application of interrupts in a multitasking operating system.
Examiner trap

An interrupt is a signal to the processor. Do not describe it as "an error": many interrupts, such as a key press or a finished print job, are normal events.

Section 02

When and how it is handled.

In the lab, start an interrupt, then fire a higher priority one while the first ISR is running, and watch what is saved on the stack. The handling steps below are the ones the mark scheme looks for.

  1. The processor finishes the current fetch-execute cycle, then checks for an interrupt (by checking the interrupt flags in the status register).
  2. If there is an interrupt, its priority is compared with the current task. A lower priority interrupt waits; a higher priority one is serviced.
  3. The contents of the registers, including the PC, are saved on the stack.
  4. The start address of the ISR is loaded into the PC, and the ISR is executed to deal with the cause of the interrupt.
  5. While the ISR runs, an interrupt of even higher priority can interrupt it in the same way.
  6. When the ISR has finished, the saved values are restored from the stack, and the interrupted program continues from where it left off. Further pending interrupts are then checked.
Examiner trap

"The computer stops what it is doing" earns nothing. Say that the registers are saved (on the stack) so the task can be resumed later, and that the ISR is executed.

Stage 1 · Learn

Check what the sim just showed you

Four quick checks. Each correct answer earns XP and records this skill on your profile.

Quick check+10 XP

What is an interrupt?

Quick check+10 XP

When does the processor check for interrupts?

Quick check+10 XP

In the lab, what happens to the PC when an interrupt is accepted?

Quick check+10 XP

Which is a software interrupt?

Section 03

Priorities in action.

Questions often describe two or three interrupts arriving close together and ask for the order in which they are serviced. Use the priorities, and remember that an interrupt is only noticed at the end of a cycle.

Worked example Order of servicing
A program is running. The printer sends a "finished" interrupt (priority 1, lowest). While its ISR runs, a key press interrupt (priority 2) arrives, and then a power failure interrupt (priority 5, highest) arrives during the key press ISR. Describe what happens.
  1. At the end of a cycle the printer interrupt is detected. It has higher priority than the program, so the program's registers are saved on the stack and the printer ISR starts.
  2. The key press (priority 2) is higher than the printer ISR (1), so at the end of a cycle the printer ISR's registers are saved and the key press ISR runs.
  3. The power failure (priority 5) interrupts the key press ISR in the same way and is serviced at once, for example by saving data before the power goes.
  4. If the power holds, each finished ISR restores the values from the stack in reverse order: key press ISR, then printer ISR, then the program.
Answer: Power failure first, then key press, then printer, then the program resumes
Your turn
Why is a stack a suitable structure for saving register contents when interrupts can be nested?
Stage 2 · Exam

Exam-style questions

Unlocks once the four checks above are done. Worth more XP, written to AS Paper 1 and Paper 2 standard.

Finish the four checks above to unlock the exam questions
Exam style+20 XP

An interrupt arrives with a lower priority than the ISR currently running. What happens?

Exam style+20 XP

Why are the register contents saved before the ISR runs?

Exam style+20 XP

What is loaded into the PC so that the ISR runs?

Exam style+20 XP

Which is an application of a timer interrupt?

Skill unlocked

Interrupts, mastered.

This skill is now recorded on your profile. Keep the chain going.

-Rank -Level -Score -Topics
Go deeper · practice
Six original Cambridge-style questions
The causes and applications of interrupts, the interrupt service routine, and how interrupts are detected and handled.