12.3 ISRs, Priority, Masking, and Nesting
Section 12.2 showed the automatic FLAGS, CS, and IP frame. That frame is necessary but not sufficient. An interrupt service routine may also change general registers, segment registers, device state, and controller state that the foreground program expects to remain consistent.
Preserve the interrupted program’s contract
An ISR should have a clear preservation contract: every register it changes, unless deliberately used as a return value by an agreed interface, must be saved and restored. A typical structure is:
isr:
PUSH AX
PUSH BX
PUSH DS
; establish any required segment state
; acknowledge or service the device
POP DS
POP BX
POP AX
IRETRegisters are restored in reverse order because the stack is last-in, first-out. IRET, not RET, removes the automatic interrupt frame and restores FLAGS. If the ISR executes STI, changes DS, or calls another procedure, those choices expand the state and stack-depth analysis.
Keeping an ISR short reduces the interval before other events can be serviced. Long computation can often be deferred: the ISR captures data or sets a flag, and foreground code performs the expensive work later.
Control which pending request runs next
When several devices request service, three related mechanisms answer different questions:
- Priority chooses the most important eligible request.
- Masking temporarily makes a source ineligible.
- Nesting allows one ISR to be interrupted by another accepted request.
On interrupt entry, the 8086 clears IF, so maskable interrupts do not normally nest immediately. An ISR may execute STI after saving vulnerable state and configuring its controller, allowing a higher-priority request to preempt it. Every nested entry adds another interrupt frame and more saved registers to the stack.
A global mask such as clearing IF blocks all INTR requests at the CPU. A controller’s per-source mask can block one device while leaving others eligible. Neither mechanism blocks NMI. A pending masked request may remain recorded by the controller and become eligible when unmasked; masking is not necessarily the same as deleting the event.
Uncontrolled nesting can exhaust the stack or expose partially updated shared data. A safe design states which priorities may preempt, when IF is re-enabled, how much stack each level uses, and which data needs a critical section.
Section 12.3 described the policies an interrupt system needs. Section 12.4 gives those policies concrete registers and commands in the 8259 programmable interrupt controller.