The worksheetDownload the PDF
Answers

A9.7 Interrupts

Computer architecture · A level · OCR H446 1.2.1, AQA 7517 4.7.3.6 · about 20 min

BugBotLab

What this lesson is about

Sources of interrupts, polling, the check at the end of each cycle, saving the volatile environment on a stack, and priorities.

Questions 5 marks in all

  1. [1 mark]When does the processor check for an interrupt?

    1. AAt the end of each fetch-decode-execute cycle
    2. BHalfway through executing an instruction
    3. COnly when the program has finished
    4. DOnce a second
    Answer: A. It never abandons an instruction partway; it checks before the next fetch.
  2. [1 mark]Why is the volatile environment saved before an ISR runs?

    1. AThe ISR uses the same registers, so the interrupted program's values would otherwise be lost
    2. BTo free up main memory
    3. CSo the ISR can run faster
    4. DTo stop further interrupts
    Answer: A. Restoring the PC, status register and other registers lets the program resume as if nothing happened.
  3. [1 mark]Put the handling of an interrupt in order.

    Number the lines 1 to 6 to put them in the right order.

    1. The address of the ISR is loaded into the PC
    2. The registers are pushed onto the stack
    3. The registers are popped off the stack and the program resumes
    4. The current instruction finishes executing
    5. The processor checks for interrupts and finds one of higher priority
    6. The ISR runs
    Answer:
    The current instruction finishes executing
    The processor checks for interrupts and finds one of higher priority
    The registers are pushed onto the stack
    The address of the ISR is loaded into the PC
    The ISR runs
    The registers are popped off the stack and the program resumes

    Finish, check, save, jump to the ISR, run it, restore.

  4. [1 mark]Why is a stack the right structure for saving registers when interrupts are nested?

    1. AThe last registers saved are the first restored, so nested interrupts unwind in the right order
    2. BA stack is faster than a queue
    3. CA stack can hold any amount of data
    4. DA stack is kept inside the ALU
    Answer: A. Last in, first out matches returning from the most recent interrupt first.
  5. [1 mark]Which is a disadvantage of polling compared with interrupts?

    1. AProcessor time is wasted checking devices that have nothing to report
    2. BIt needs extra hardware
    3. CDevices cannot send data
    4. DIt makes the stack overflow
    Answer: A. With interrupts, no time is spent until a device actually needs attention.

The task: a timer interrupt

Simulate a processor that is interrupted by a timer. The main program runs 12 cycles. In each cycle it drives forward 3 cm, adds 3 to acc, and adds 1 to pc, in that order. At the end of any cycle after which pc is a multiple of 4 (so after cycles 4, 8 and 12), the timer interrupts. Handle it like this: 1. Save the volatile environment by pushing pc, then acc, onto the list stack, and print saved PC=4 ACC=12 (with the real values). 2. Run the ISR. It uses the accumulator as its working register: set acc to 200 plus 100 times the number of interrupts so far including this one (300, then 400, then 500), and play acc as a tone for 0.2 seconds. 3. Restore the registers by popping them off stack in the reverse order, and print restored PC=4 ACC=12. After the 12 cycles, print done PC=12 ACC=36, using the values in the registers.

# the two lines every program starts with: the commands, then the robot
from bugbot import *
connect()

pc = 0
acc = 0
stack = []

while pc < 12:
    forward(50, distance=3)
    acc = acc + 3
    pc = pc + 1

print(f"done PC={pc} ACC={acc}")

The hint students can ask for: The check belongs after the main program's work in each cycle, not before it. Count the interrupts in a separate variable. A stack gives back the last thing pushed first, so think about which register must come off first.

A solution

from bugbot import *
connect()
pc = 0
acc = 0
stack = []
interrupts = 0

while pc < 12:
    forward(50, distance=3)
    acc = acc + 3
    pc = pc + 1
    if pc % 4 == 0:
        stack.append(pc)
        stack.append(acc)
        print(f"saved PC={pc} ACC={acc}")
        interrupts = interrupts + 1
        acc = 200 + 100 * interrupts
        tone(acc, 0.2)
        acc = stack.pop()
        pc = stack.pop()
        print(f"restored PC={pc} ACC={acc}")

print(f"done PC={pc} ACC={acc}")

Any program that meets the task's checks is marked correct in the simulator; this is one way, not the only way.