Interrupt handling step by step

Tip: Choose Save as PDF in the print dialog to download. Use A4 paper size.

Interrupt handling step by step

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Which step happens FIRST when an interrupt occurs?
A The interrupt is generated by a device or by software
B The CPU runs the interrupt service routine (ISR)
2. When the CPU detects an interrupt, what has it just done?
A Stopped halfway through an instruction
B Finished its current fetch-decode-execute cycle
3. Which is the correct order for the middle steps?
A Save the CPU's state, then run the ISR
B Run the ISR, then save the CPU's state
4. Why does the CPU save its state before running the ISR?
A So it can return to the original task exactly where it left off
B So the ISR can run faster
5. In the key press example, what happens right after the CPU saves its state?
A It resumes the program it was running before
B It runs the keyboard ISR to read the key that was pressed

Match the Pairs

Draw a line connecting each item on the left to its match on the right.

Left columnRight column
1PUSH registers; PC -> stack Ax = 10 / 0 -> interrupt -> ISR: error message
2POP registers; resume task BThe CPU must save its state BEFORE running the ISR. If it runs the ISR first, the register values and program counter of the interrupted task are overwritten and lost.
3Key press worked example: the keyboard sends a signal, the CPU finishes its cycle, saves its state, runs the keyboard ISR, restores its state and resumes. CSteps 5 and 6: the CPU restores its saved state and carries on with the original task where it left off.
4Division by zero worked example: the software generates an interrupt, the CPU detects it after its cycle, saves its state and runs an ISR that shows an error message. DThe CPU does not stop instantly. It finishes the current fetch-decode-execute cycle first and then detects the interrupt, and the sequence must also end with restore and resume.
5detect -> run ISR -> save state -> restore -> resume EStep 3: the CPU saves its state (register values and the program counter) so the interrupted task can be resumed later.
6generate -> CPU stops instantly -> save -> ISR Fgenerate -> detect -> save -> run ISR -> restore -> resume

Definitions

interrupt handling sequence

Fill in the Blanks

Complete each line of code by filling in the missing parts.

1.
STEP 1: key press generates an interrupt STEP 2: CPU finishes cycle and _______ it
2.
STEP 3: CPU _____ its state (registers, PC)
3.
STEP 3: CPU saves its state STEP 4: CPU runs the ___
4.
STEP 5: CPU ________ the saved state STEP 6: CPU resumes the original task
5.
result = 10 / 0 A ________ interrupt is generated

Spot the Error

Shade in the letter of the line that contains an error.

Question 1
A INTERRUPT generated
B CPU abandons the current cycle
C CPU detects the interrupt
D CPU saves its state
E ISR runs
Question 2
A Key is pressed
B Keyboard sends an interrupt
C CPU restores its state
D CPU saves its state
E ISR reads the key code
Question 3
A Program runs x = 10 / 0
B Software interrupt generated
C CPU ignores the interrupt
D CPU saves its state
E ISR shows an error message
Question 4
A Interrupt generated
B CPU detects it after cycle
C CPU saves registers and PC
D ISR is skipped
E CPU restores saved state
F Original task resumes
Question 5
A ISR starts without any signal
B CPU finishes and detects it
C CPU saves its state
D ISR runs
E CPU restores its state
F Original task resumes

Answer Key

Interrupt handling step by step
Section A: Quick Questions
Q1
A — The interrupt is generated by a device or by software
Q2
B — Finished its current fetch-decode-execute cycle
Q3
A — Save the CPU's state, then run the ISR
Q4
A — So it can return to the original task exactly where it left off
Q5
B — It runs the keyboard ISR to read the key that was pressed
Section B: Match the Pairs
Q1
PUSH registers; PC -> stack → Step 3: the CPU saves its state (register values and the program counter) so the interrupted task can be resumed later.
Q2
POP registers; resume task → Steps 5 and 6: the CPU restores its saved state and carries on with the original task where it left off.
Q3
Key press worked example: the keyboard sends a signal, the CPU finishes its cycle, saves its state, runs the keyboard ISR, restores its state and resumes. → generate -> detect -> save -> run ISR -> restore -> resume
Q4
Division by zero worked example: the software generates an interrupt, the CPU detects it after its cycle, saves its state and runs an ISR that shows an error message. → x = 10 / 0 -> interrupt -> ISR: error message
Q5
detect -> run ISR -> save state -> restore -> resume → The CPU must save its state BEFORE running the ISR. If it runs the ISR first, the register values and program counter of the interrupted task are overwritten and lost.
Q6
generate -> CPU stops instantly -> save -> ISR → The CPU does not stop instantly. It finishes the current fetch-decode-execute cycle first and then detects the interrupt, and the sequence must also end with restore and resume.
Section C: Definitions
Q1
interrupt handling sequence — The ordered steps: an interrupt is generated, the CPU finishes its cycle and detects it, saves its state, runs the ISR, restores its state, then resumes the original task.
Section D: Fill in the Blanks
Q1
STEP 1: key press generates an interrupt STEP 2: CPU finishes cycle and detects it
Q2
STEP 3: CPU saves its state (registers, PC)
Q3
STEP 3: CPU saves its state STEP 4: CPU runs the ISR
Q4
STEP 5: CPU restores the saved state STEP 6: CPU resumes the original task
Q5
result = 10 / 0 A software interrupt is generated
Section E: Spot the Error
Q1
B — The CPU must finish the current fetch-decode-execute cycle before it detects the interrupt. It does not abandon the cycle. The line should be 'CPU finishes the current cycle'.
Q2
C — After the keyboard sends the interrupt, the CPU first finishes its cycle and detects the interrupt. Restoring the state happens after the ISR has run. The line should be 'CPU finishes cycle, detects it'.
Q3
C — The CPU checks for interrupts and detects them, so it does not ignore them. Detecting the interrupt must come before saving the state. The line should be 'CPU detects the interrupt'.
Q4
D — After the CPU saves its state it must run the interrupt service routine (ISR), which deals with the interrupt. Skipping it means the interrupt is never handled. The line should be 'ISR runs'.
Q5
A — The first step is that an interrupt is generated, because the ISR only runs after an interrupt has been generated and detected. The line should be 'Interrupt is generated'.