Saving the CPU's state before handling an interrupt

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

Saving the CPU's state before handling an interrupt

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. What does the CPU do with its register contents before it runs the ISR?
A It deletes them to make room for the ISR
B It saves them so they can be restored later
2. Which value must be saved so the CPU knows which instruction to carry on with after the interrupt?
A The program counter
B The keyboard buffer
3. Why is the state of the interrupted task saved?
A So the task can carry on exactly where it stopped
B So the task can start again from the beginning
4. When does the CPU save its registers during interrupt handling?
A After the ISR has finished
B Before the ISR starts running
5. What could happen if the program counter was NOT saved before the ISR ran?
A The CPU would lose its place in the interrupted program
B The ISR would run twice

Match the Pairs

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

Left columnRight column
1save register contents Asave state -> run ISR -> restore state
2save program counter Brestore register contents
3The saved state is put back into the registers after the ISR finishes, so the old task carries on exactly where it stopped CNot saving the program counter means the CPU forgets the address of the next instruction, so the interrupted task cannot carry on from where it stopped.
4The correct order is to save the state first, then run the ISR, then restore the state DThe CPU stores the address of the next instruction so it knows where the interrupted task stopped
5run ISR -> save register contents EBefore the ISR runs, the CPU stores the values held in its registers so they are not overwritten
6Only the data registers are saved; the program counter is not saved FThe state is saved after the ISR has already run. The ISR may already have overwritten the registers and the program counter, so the interrupted task's data is lost.

Definitions

save register contents

Fill in the Blanks

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

1.
STORE registers RUN ISR _______ registers
2.
STORE registers RUN ___ RESTORE registers
3.
_____ registers RUN ISR RESTORE registers
4.
savedPC ← __
5.
IF _________ THEN STORE registers RUN ISR

Spot the Error

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

Question 1
A IF interrupt_flag = FALSE
B SAVE registers
C RUN ISR
D RESTORE registers
Question 2
A Interrupt arrives
B CPU saves PC to the stack
C CPU runs the ISR
D CPU discards PC from stack
Question 3
A Keyboard key pressed
B CPU skips saving registers
C ISR reads the key
D CPU restores register contents
Question 4
A Interrupt arrives
B CPU runs ISR, then saves state
C ISR finishes
D CPU restores state
Question 5
A Interrupt: printer error
B Save registers and PC
C Run printer ISR
D Load registers from the ISR
E Resume the interrupted task

Answer Key

Saving the CPU's state before handling an interrupt
Section A: Quick Questions
Q1
B — It saves them so they can be restored later
Q2
A — The program counter
Q3
A — So the task can carry on exactly where it stopped
Q4
B — Before the ISR starts running
Q5
A — The CPU would lose its place in the interrupted program
Section B: Match the Pairs
Q1
save register contents → Before the ISR runs, the CPU stores the values held in its registers so they are not overwritten
Q2
save program counter → The CPU stores the address of the next instruction so it knows where the interrupted task stopped
Q3
The saved state is put back into the registers after the ISR finishes, so the old task carries on exactly where it stopped → restore register contents
Q4
The correct order is to save the state first, then run the ISR, then restore the state → save state -> run ISR -> restore state
Q5
run ISR -> save register contents → The state is saved after the ISR has already run. The ISR may already have overwritten the registers and the program counter, so the interrupted task's data is lost.
Q6
Only the data registers are saved; the program counter is not saved → Not saving the program counter means the CPU forgets the address of the next instruction, so the interrupted task cannot carry on from where it stopped.
Section C: Definitions
Q1
save register contents — Before running the ISR, the CPU stores its register contents, including the program counter, so the interrupted task can later carry on exactly where it stopped.
Section D: Fill in the Blanks
Q1
STORE registers RUN ISR RESTORE registers
Q2
STORE registers RUN ISR RESTORE registers
Q3
STORE registers RUN ISR RESTORE registers
Q4
savedPC ← PC
Q5
IF interrupt THEN STORE registers RUN ISR
Section E: Spot the Error
Q1
A — The save, ISR and restore steps only happen when an interrupt has been received. The test should be interrupt_flag = TRUE.
Q2
D — After the ISR the CPU must restore the saved program counter, not throw it away. Without it, the interrupted task cannot carry on where it stopped.
Q3
B — The CPU must save its register contents before running the ISR. Otherwise the ISR overwrites them and the interrupted task loses its place.
Q4
B — The state must be saved BEFORE the ISR runs. If the ISR runs first, it changes the registers and the original values are lost.
Q5
D — The CPU must restore the values it saved from the interrupted task, not use values left by the ISR. Line 4 should restore the registers and PC.