Chapter 5.1: Interrupts

Chapter 5.1: Interrupts · Study Questions
Polling and interruptsInterrupt typesVector tableINT and IRETStackExceptionsINTR and NMI8259A and IMRInterrupt latency
Sercan KÜLCÜ · Giresun University · Computer Engineering

Multiple Choice · The Interrupt Concept

Question 1: Polling or Interrupt?

For a device that sends data only rarely, such as a keyboard, what is the advantage of interrupts over polling?

B: With interrupts the CPU keeps doing its own work; when the device signals, the ISR runs and IRET returns to the instruction where the program left off. A and C describe polling (no extra hardware, late detection). The program does not restart; it continues where it stopped.

Short Answer · The Interrupt Concept

Question 2: What Is an Interrupt?

What is an interrupt? What happens when the ISR ends? Give one example of each of the three interrupt sources of the 8086.

Show answer

An interrupt is the processor temporarily leaving the program it is running to execute a special routine that serves an event, the interrupt service routine (ISR). When the ISR ends with IRET, the program continues from the instruction where it left off.

  • Hardware: the INTR pin (maskable; keyboard, timer) and the NMI pin (non-maskable; memory parity error, power loss)
  • Software: INT n instruction (INT 10h, INT 21h), the INT 3 breakpoint, INTO
  • Internal (exception): divide by zero / quotient overflow → INT 0, single step when TF = 1 → INT 1

Multiple Choice · The Interrupt Concept

Question 3: Interrupt Type

With BL = 0, DIV BL is executed. Which interrupt does the processor generate?

A: Division by zero (or a quotient that does not fit the destination) is the type 0 interrupt that the processor generates itself. INT 2 belongs to the NMI pin, INT 4 to INTO when OF = 1; INT 21h is called by the program itself for DOS services.

Multiple Choice · Interrupt Vector Table

Question 4: Vector Address

At which physical address does the interrupt vector of INT 21h start in memory?

D: The interrupt vector table occupies 00000h–003FFh: 256 vectors × 4 bytes = 1 KB. Each vector is 4 bytes (IP first, then CS) → address n × 4 = 21h × 4 = 84h. 00042h would be n × 2; 00021h is n itself.

Classic Question · Interrupt Vector Table

Question 5: From Table to Address

Starting at address 0000:004Ch, memory holds the bytes 59 EC 00 F0. a) Which interrupt's vector is this? b) What are the CS:IP value and the physical address of the ISR? c) At which addresses is the vector of type 09h?

Show answer
  • a) n × 4 = 4Ch → n = 13h: INT 13h (BIOS disk service)
  • b) The low word is IP, the high word is CS; both are little-endian: IP = EC59h, CS = F000h → F0000h + EC59h = FEC59h
  • c) 09h × 4 = 24h → IP: 00024h–00025h, CS: 00026h–00027h

Multiple Choice · INT and IRET

Question 6: The INT n Sequence

INT n is executed. Which list gives what the processor does, in the correct order?

B: First the flags are saved, then IF and TF are cleared (so the ISR is not interrupted by another INTR and does not single-step); CS and IP are pushed as the return address; finally the new CS:IP is loaded from the vector table. If the vector is loaded first, the return address is lost (C). IRET does the reverse: IP, CS and FLAGS are popped.

Step-by-Step Trace · INT and IRET

Question 7: Our Own Interrupt Routine

How do SP, IP, IF and the stack change during INT 90h and IRET? What is printed on the screen?

SPFFFEIP0103stack (SS = 0700h)IF = 1FFF80000FFFA0000FFFC0000FFFE0000▲ SP

AX = 0. An immediate value cannot be loaded into a segment register, so ES is set to 0 through AX.

SPFFFEIP0105stack (SS = 0700h)IF = 1FFF80000FFFA0000FFFC0000FFFE0000▲ SP

ES = 0000h: the vector table lies in segment 0000.

SPFFFEIP0106stack (SS = 0700h)IF = 0FFF80000FFFA0000FFFC0000FFFE0000▲ SP

CLI: IF = 0. A hardware interrupt must not arrive while the vector is half written.

SPFFFEIP010Dstack (SS = 0700h)IF = 0FFF80000FFFA0000FFFC0000FFFE0000▲ SP

The vector of type 90h is at 90h × 4 = 0240h; the offset of isr (0116h) is stored as IP.

SPFFFEIP0112stack (SS = 0700h)IF = 0FFF80000FFFA0000FFFC0000FFFE0000▲ SP

CS (0700h) is stored at 0242h. The vector now points to 0700:0116.

SPFFFEIP0113stack (SS = 0700h)IF = 1FFF80000FFFA0000FFFC0000FFFE0000▲ SP

STI: IF = 1; interrupts are allowed again.

SPFFF8IP0116stack (SS = 0700h)IF = 0FFF80115FFFA0700FFFCF202FFFE0000▲ SP

INT 90h: FLAGS (F202h; on the 8086 the upper 4 bits are always 1), CS and the return IP (0115h) are pushed → SP = FFF8h; IF = 0. CS:IP ← 0700:0116.

SPFFF8IP0118stack (SS = 0700h)IF = 0FFF80115FFFA0700FFFCF202FFFE0000▲ SP

Inside the ISR: AH = 0Eh (teletype).

SPFFF8IP011Astack (SS = 0700h)IF = 0FFF80115FFFA0700FFFCF202FFFE0000▲ SP

AL = '*'.

SPFFF8IP011Cstack (SS = 0700h)IF = 0FFF80115FFFA0700FFFCF202FFFE0000▲ SP

INT 10h writes * to the screen (the BIOS uses the stack itself and restores it).

SPFFFEIP0115stack (SS = 0700h)IF = 1FFF80115FFFA0700FFFCF202FFFE0000▲ SP

IRET: IP ← 0115h, CS ← 0700h, FLAGS ← F202h (IF is 1 again) → SP = FFFEh. The values on the stack are not erased; only SP moves back.

SP0000IP0000stack (SS = 0700h)IF = 1FFF80115FFFA0700FFFCF202FFFE0000

The program continues with the RET after INT 90h and returns to DOS.

org 100h
mov ax, 0
mov es, ax
cli
mov word ptr es:[90h*4], offset isr
mov word ptr es:[90h*4+2], cs
sti
int 90h
ret
isr:
mov ah, 0Eh
mov al, '*'
int 10h
iret

Short Answer · INT and IRET

Question 8: When INT 21h Executes

While SP = FFFEh, INT 21h is executed. a) From which addresses is the vector read? b) What is pushed onto the stack, and what is SP inside the ISR? c) With which instruction does the ISR return; what happens if it returns with RET?

Show answer
  • a) 21h × 4 = 84h → IP ← [0000:0084h], CS ← [0000:0086h]
  • b) FLAGS, CS and IP are pushed in that order (6 bytes) → SP = FFF8h: [FFF8h] = return IP, [FFFAh] = CS, [FFFCh] = FLAGS. Then IF = TF = 0.
  • c) IRET pops IP, CS and FLAGS. A near RET pops only IP: CS and FLAGS stay on the stack, SP is off by 4 bytes, IF stays 0 and later returns fetch the wrong address.

Multiple Choice · INT and IRET

Question 9: Overflow with INTO

With AL = 70h and BL = 30h, ADD AL, BL is executed, followed by INTO. What happens?

A: 70h + 30h = A0h. As signed numbers 112 + 48 = 160 > 127 → OF = 1; as unsigned, 160 ≤ 255, so CF = 0. INTO looks only at OF: if OF = 1 it calls INT 4, if OF = 0 it does nothing. A0h in AL is unchanged; fixing it is the job of the INT 4 routine.

Short Answer · INT and IRET

Question 10: Single Step and Breakpoint

a) Which instruction sequence sets TF to 1? b) Why do the instructions of the INT 1 routine itself not cause single-step interrupts? c) Why does a debugger use the one-byte encoding of INT 3 (CCh) for a breakpoint?

Show answer
  • a) No instruction changes TF directly; FLAGS is edited through the stack: PUSHF · POP AX · OR AH, 01h (bit 8) · PUSH AX · POPF.
  • b) After pushing FLAGS onto the stack, INT sets TF = 0, so the routine runs without single stepping. IRET restores the TF = 1 saved on the stack, and tracing continues.
  • c) One byte fits even in place of the first byte of the shortest instruction. INT 03h would be the two bytes CD 03 and would corrupt the next instruction when placed over a one-byte instruction.

Multiple Choice · Hardware Interrupts

Question 11: NMI

Which statement about the NMI input of the 8086 is false?

C: NMI cannot be masked (IF is ignored), is rising-edge triggered and is fixed at type 2: its vector is at 00008h and no INTA cycle is needed. It is INTR that reads the type number from the data bus with two INTA pulses (the 8259A puts the type number on D0–D7). The counterpart of NMI on the 8085 is TRAP.

Short Answer · Hardware Interrupts

Question 12: The INTR Acknowledge Sequence

A key is pressed: IRQ1 is active and the 8259A sets INTR to 1. a) What happens if IF = 0? b) If IF = 1, how does the processor learn the type number; what are this type and its vector address on the PC? c) What is done at the end of the ISR? d) How is this related to the INT 16h that a program calls?

Show answer
  • a) The request is held; it is accepted when IF = 1 (STI). The 8086 accepts the interrupt when the instruction it is already executing finishes.
  • b) Two INTA pulses are generated; on the second, the 8259A puts the type number on the data bus: keyboard 09h → vector 09h × 4 = 00024h. The rest is the same as INT n: FLAGS, CS, IP are pushed; IF = TF = 0.
  • c) The ISR sends an EOI (end of interrupt) to the 8259A and IRET returns.
  • d) INT 09h puts the key in the keyboard buffer; INT 16h reads from that buffer.

Classic Question · Hardware Interrupts

Question 13: Masking with the IMR

IMR (port 21h) = B8h. a) Which IRQs are masked? b) Which instructions mask IRQ6 (floppy disk) and then unmask it again, and what values does the IMR take? c) How does this method differ from CLI?

Show answer
  • a) B8h = 1011 1000b → IRQ3, 4, 5 and 7 are masked.
  • b) Read–modify–write: IN AL, 21h · OR AL, 40h · OUT 21h, AL → IMR = F8h. To unmask, use IN AL, 21h · AND AL, 0BFh · OUT 21h, AL → back to B8h. The other bits are untouched.
  • c) CLI shuts off the whole INTR input (IF = 0). The IMR holds back only the selected device; the incoming request stays in the IRR and is served once the line is unmasked.

Multiple Choice · Hardware Interrupts

Question 14: The Deferred Interrupt

INTR = 1 and IF = 1. After which instruction does the interrupt not get accepted immediately, but wait until the next instruction has finished as well?

B: After a segment register is loaded (MOV SS, POP SS), the 8086 defers the interrupt by one instruction, so that the following MOV SP, … also completes. Otherwise FLAGS, CS and IP would be pushed to the wrong place, addressed by the new SS and the old SP. After other instructions the interrupt is accepted as soon as the instruction ends.

Classic Question · Hardware Interrupts

Question 15: Interrupt Latency

On a 5 MHz 8086, when INTR arrives a 16-bit IDIV (at most 184 clock cycles) has just started. a) If accepting INTR takes 61 cycles, what is the worst-case latency in cycles and in µs? b) If the program keeps interrupts disabled with CLI for 100 µs at that moment, what does the latency become?

Show answer
  • a) 184 + 61 = 245 cycles; one cycle is 1 / 5 MHz = 200 ns → 245 × 200 ns ≈ 49 µs.
  • b) The time spent with interrupts disabled by CLI is added directly: in the worst case ≈ 100 + 49 ≈ 149 µs. That is why the span between CLI and STI is kept short.

The End

Chapter 5.1: Interrupts: Study Questions
Lecture slides: Chapter 5.1: Interrupts · Lecture notes
1 / 1 Sercan KÜLCÜ. All rights reserved.