Chapter 5.1: Interrupts
Introduction
Learning Outcomes
Explain what an interrupt is, how it differs from polling, and the kinds of interrupts.
Use the interrupt vector table to work out where INT n goes.
Trace how INT and IRET push and pop FLAGS, CS and IP.
Follow INTR acknowledgment and masking with the 8259A; install your own handler and restore the old vector.
The Interrupt Concept
What Is an Interrupt?
- An interrupt is an event that makes the processor set aside its current work and run a special routine.
- That routine is the interrupt service routine (ISR).
- When the ISR finishes, the processor resumes at the instruction where it left off; the main program never notices.
- Software interrupts are ready-made services: instead of writing code to print a character, you call an interrupt.
- There are interrupt functions for the screen, keyboard, disk drives, memory management and other hardware.
- Interrupts can also be triggered by hardware (a key press, the timer).
You are reading when the phone rings: you mark the page (save the return address), answer (the ISR), and after the call carry on from the marked page (IRET).
The Interrupt Concept
Polling or Interrupts?
Polling
- Simple; no extra hardware.
- Wastes CPU time.
- An event may be noticed late, depending on the polling interval.
Interrupts
- The CPU is busy only when an event occurs.
- Fast response to events.
- Needs a vector table, a stack and an ISR.
The Interrupt Concept
Kinds of Interrupts
Called deliberately by an INT n instruction in the program. BIOS and DOS services are used this way. INT 3 (breakpoint) and INTO (INT 4 if OF=1) are software interrupts too.
An external device drives a pin. INTR is maskable (ignored while IF=0); NMI is non-maskable (e.g. memory parity error, power failure).
Generated by the processor itself: divide by zero / quotient overflow → INT 0; single step after every instruction while TF=1 → INT 1.
| Type | Source | Description |
|---|---|---|
00h | internal | Divide error (DIV/IDIV by zero, or the quotient does not fit) |
01h | internal | Single step (TF = 1), used by debuggers |
02h | hardware | NMI pin (non-maskable) |
03h | software | Breakpoint; the one-byte INT 3 (CCh) |
04h | software | INTO: overflow interrupt if OF = 1 |
05h–FFh | — | Intel reserves 05h–1Fh; on the PC they are used by the BIOS, DOS and user programs |
The Interrupt Concept
Comparing the Three Sources
| Software (INT n) | Hardware (INTR / NMI) | Internal (exception) | |
|---|---|---|---|
| Triggered by | an instruction in the program | an external device, a pin | the processor itself |
| Timing | synchronous: always at the same point | asynchronous: after any instruction | synchronous: at the faulting instruction |
| Type number | in the instruction | INTR: from the 8259A, via INTA; NMI: always 2 | fixed: 0, 1 |
| While IF = 0 | still runs | INTR waits, NMI runs | still runs |
| Typical use | BIOS/DOS services, INT 3, INTO | keyboard, timer, disk | divide error, single step |
CLI disables only the INTR input; software interrupts and exceptions are not affected by IF.
The Interrupt Vector Table
The Interrupt Vector Table (IVT)
- The first 1 KB of memory:
00000h–003FFh. - For each of the 256 interrupt types it holds a 4-byte vector (the address of the ISR).
- The low word is IP (offset), the high word CS (segment); both little-endian.
- The vector for type n is at address n × 4.
- Changing a vector redirects the interrupt to another routine.
The F000:F065 shown for the INT 10h vector is the address of the video routine in the original IBM PC BIOS (an example).
The Interrupt Vector Table
Vector Address Step by Step
The program executes INT 10h. The type number is n = 10h (16).
Vector address n × 4: 10h × 4 = 40h. (×4 is a shift left by two bits.)
These 4 bytes are read: low word IP = F065h, high word CS = F000h (little-endian: low byte first).
After the return information is pushed, CS:IP = F000:F065 is loaded; the ISR starts at physical address FF065h.
The Interrupt Vector Table
Demo: Vector Calculator
Enter an interrupt type (00h–FFh) to see its place in the table and its known role.
address = n × 4. IP is at that address, CS at n × 4 + 2.INT and IRET
How Does INT n Work?
SP ← SP − 2, [SS:SP] ← FLAGS.IP ← [n×4], CS ← [n×4+2]: the ISR begins.INT and IRET
INT and IRET: The Stack Step by Step
INT 10h at 0700:0104. SP = FFFEh, FLAGS = F202h (IF = 1; on the 8086 bit 1 and bits 12–15 always read 1).
FLAGS is pushed: SP = FFFCh, [FFFCh] = F202h.
IF = 0, TF = 0 → FLAGS = F002h. The old value is safe on the stack.
CS is pushed: SP = FFFAh, [FFFAh] = 0700h.
The return address is pushed: INT 10h is 2 bytes, so 0106h. SP = FFF8h.
CS:IP ← vector (F000:F065). The ISR runs and prints the character.
IRET: IP ← 0106h, CS ← 0700h (SP = FFFCh).
FLAGS ← F202h, SP = FFFEh. The program continues at 0106h. (The old values on the stack are not erased; SP just moves back.)
INT and IRET
INT/IRET vs. CALL/RET
CALL / RET | INT n / IRET | |
|---|---|---|
| Target address | in the instruction (a label) | from the vector table (n × 4) |
| Pushed | IP (near) or CS + IP (far) | FLAGS + CS + IP (6 bytes) |
| Flags | unchanged | IF = 0, TF = 0 |
| Return | RET: pops IP (and CS) | IRET: pops IP, CS, FLAGS |
| Instruction length | 3 bytes (near CALL) | 2 bytes (INT 3: 1 byte) |
| Invoked by | only the program | the program, the processor (exception) or hardware |
RET is a bug: a near RET pops only IP; CS and FLAGS stay on the stack (SP is off by 4 bytes) and IF is not restored. ISRs always end with IRET.INT and IRET · Step through the code
Code Trace: RET Instead of IRET
org 100h mov ax, 0 mov es, ax mov word ptr es:[61h*4], offset isr mov word ptr es:[61h*4+2], cs int 61h hlt ; SP = ? isr: ret ; BUG!
AX = 0: the IVT segment.
ES = 0000h.
IP of the type 61h vector: [184h] = offset isr = 0114h.
CS: [186h] = 0700h. The vector is 0700:0114.
int 61h: FLAGS (F202h), CS (0700h) and IP (0113h) are pushed: 6 bytes; SP = FFF8h, IF = 0.
The near ret in the ISR (it should be IRET) pops only IP (0113h): CS and FLAGS stay on the stack. SP = FFFAh, 4 bytes short of the expected FFFEh; IF stays 0.
We reach hlt, but interrupts are off (IF = 0) and the stack is unbalanced: any later ret would take 0700h as its return address. An ISR always ends with IRET.
Even RETF is not enough: it pops IP and CS, but FLAGS (2 bytes) stays on the stack and IF is not restored.
INT and IRET
Exceptions: Divide Error and INTO
AX = 1000, DIV BL (BL = 2): the quotient 500 does not fit in AL (at most 255) → INT 0. The error occurs even though the divisor is not zero.ADD AL, BL · INTO.Checking before a division that the divisor is nonzero and the quotient will fit (e.g. using DX:AX for a 16-bit divide) prevents INT 0 in the first place.
INT and IRET · Step through the code
Code Trace: Our Own INT 0 Handler
org 100h mov ax, 0 mov es, ax ; ES = 0: IVT mov word ptr es:[0*4], offset div_err mov word ptr es:[0*4+2], cs mov ax, 100 mov bl, 0 div bl ; INT 0! mov dl, al ; returns here ret div_err: mov ax, 0FFFFh ; error flag iret
AX = 0: the IVT segment.
ES = 0000h: ES:[0] is the vector of type 0 (0 × 4 = 0).
Low word (IP) = offset div_err = 011Bh. The previous vector pointed to the emulator’s default routine.
High word (CS) = 0700h. Type 0 now goes to our handler.
AX = 100 = 0064h: the dividend.
BL = 0: the divisor is zero.
div bl → INT 0. FLAGS, CS and the return IP 0118h (the instruction after DIV) are pushed; SP = FFF8h, IF = 0.
The handler runs: AX = FFFFh, telling the program “result invalid”.
iret: IP = 0118h, CS and FLAGS are restored; SP = FFFEh, IF = 1. DIV is not run again.
DL = AL = FFh: the main program sees the error from the flag value.
ret: the program returns to DOS.
For brevity the old vector is not saved; a real program restores it before exiting. Values assume an emu8086-style .COM load (CS = 0700h).
INT and IRET
A Debugger’s Tools: INT 1 and INT 3
Single step (TF): while TF = 1 the processor raises INT 1 after every instruction. No instruction sets TF directly; FLAGS is edited through the stack:
PUSHF POP AX OR AH, 01h ; bit 8 = TF PUSH AX POPF ; TF = 1
The INT 1 handler runs with TF = 0 (INT clears TF), so its own instructions are not single-stepped. IRET restores the TF = 1 saved on the stack.
Breakpoint (INT 3) has a special one-byte encoding: CCh. The debugger:
- Saves the first byte of the instruction to stop at and writes CCh in its place.
- When the program gets there, INT 3 runs and the registers are shown.
- Writes the saved byte back and decrements the return IP on the stack by 1.
- Re-executes the instruction with IRET.
Being one byte, it fits in place of even the shortest instruction; INT 03h would be the two-byte CD 03.
emu8086’s “single step” button does the same inside the emulator; on a real processor tools such as DEBUG use the INT 1 and INT 3 vectors.
Hardware Interrupts
How INTR Is Acknowledged
09h for the keyboard on a PC).INT n: FLAGS, CS, IP onto the stack; IF = TF = 0; CS:IP ← [n×4].IRET.Hardware Interrupts
Priority and the PC Interrupt Map
8086 priority order
| Rank | Source |
|---|---|
| 1 (highest) | Divide error, INT n, INTO |
| 2 | NMI |
| 3 | INTR |
| 4 (lowest) | Single step (TF) |
When several requests arrive at once, the highest priority is served first.
IBM PC hardware interrupts
| IRQ | Device | Type |
|---|---|---|
| 0 | Timer (18.2 Hz) | 08h |
| 1 | Keyboard | 09h |
| 3 / 4 | Serial ports | 0Bh / 0Ch |
| 6 | Floppy disk | 0Eh |
| 7 | Printer | 0Fh |
INT 09h, which puts the key in a buffer; the INT 16h a program calls reads from that buffer.Hardware Interrupts
Three Registers Inside the 8259A
Cascading eight slaves on one master gives 64 IRQs; the PC/AT connects a second chip to IRQ2 (IRQ8–15, types 70h–77h). Do not confuse the in-service register ISR with the interrupt service routine.
Hardware Interrupts · Step through the code
Code Trace: Masking a Single IRQ
org 100h in al, 21h ; read IMR or al, 00000010b ; mask IRQ1 out 21h, al mov cx, 3 ; critical work in al, 21h and al, 11111101b ; unmask IRQ1 out 21h, al ret
in al, 21h: the IMR is read, AL = B8h = 1011 1000b. IRQ3, 4, 5, 7 are already masked (sample value).
or al, 02h: only bit 1 is set → AL = BAh. The other bits are kept; nothing is written to the IMR yet.
out 21h, al: IMR = BAh. IRQ1 (keyboard) can no longer reach INTR; IF and the other IRQs are unaffected.
Work that must not be interrupted (just a placeholder here). A key press waits in the IRR; it is not lost.
The IMR is read again: AL = BAh.
and al, 0FDh (1111 1101b): only bit 1 is cleared → AL = B8h.
out 21h, al: IMR = B8h, IRQ1 is enabled; the pending keyboard request is served now.
ret: the mask is back to its original value. No one else’s bits were touched.
Read–modify–write: OR to mask, AND to unmask, so the other bits are preserved. Unlike CLI, only the chosen device is held off.
Hardware Interrupts
When Is INTR Not Accepted?
MOV SS, AX or POP SS, no interrupt arrives until the next instruction also finishes, so MOV SP, … completes safely. STI also lets one more instruction run; no interrupt can get between sti / hlt or sti / iret.REP MOVSB can be interrupted between iterations; the return address points to the prefix, and after IRET it continues for the remaining CX. The 8086 remembers only one prefix: if REP ES: MOVSB is interrupted the ES: prefix is lost, so wrap such a line in CLI/STI.HLT stops the processor; an INTR or NMI wakes it up and IRET returns to the instruction after HLT. An interrupt arriving during WAIT is also accepted.In short, the 8086 accepts interrupts only at instruction boundaries; in a few cases that boundary is deliberately pushed one instruction later.
Hardware Interrupts
Interrupt Latency
| Operation (8086) | Clocks |
|---|---|
INT n | 51 |
INT 3 | 52 |
INTO (OF = 1 / OF = 0) | 53 / 4 |
| INTR acknowledge (both INTAs) | 61 |
IRET | 24 |
16-bit IDIV (register) | 165–184 |
Interrupt latency: the time from the request to the start of the ISR’s first instruction.
- In the worst case the current instruction must finish first: a long IDIV ≈ 184 clocks.
- Then the acknowledge: 61 clocks. Total ≈ 245 clocks.
- At 5 MHz one clock is 200 ns → ≈ 49 µs.
- Time spent with interrupts off (CLI) adds directly to this latency; keep CLI–STI sections short.
For a timer interrupt arriving 18.2 times a second this is negligible; for fast devices such as a high-speed serial port, a short ISR prevents data loss.
Hardware Interrupts
Our Own Interrupt Handler
org 100h mov ax, 0 mov es, ax ; ES = 0: IVT segment cli ; interrupts off mov word ptr es:[90h*4], offset isr ; IP mov word ptr es:[90h*4+2], cs ; CS sti ; interrupts on int 90h ; our interrupt ret isr: mov ah, 0Eh mov al, '*' int 10h iret ; not RET!
- Pick an unused type: 90h. Its vector is at
90h × 4 = 240h. - The IVT is in segment 0000; set ES = 0.
- Protect the vector write with
CLI/STI: no interrupt may arrive while the vector is half-written. INT 90hprints * and the program continues.- Under DOS,
INT 21hwith AH =25h(set vector) /35h(get vector) is the safer way.
Hardware Interrupts · Step through the code
Code Trace: Our Own ISR on INT 60h
org 100h mov ax, 0 mov es, ax ; ES = 0: IVT cli mov word ptr es:[60h*4], offset isr mov word ptr es:[60h*4+2], cs sti mov al, '#' int 60h ; our interrupt ret isr: mov ah, 0Eh int 10h iret
AX = 0. The interrupt vector table (IVT) is at 0000:0000; the vector for type 60h is at 60h × 4 = 180h.
ES = 0000h: ES:[180h] now refers to byte 0000:0180h of the IVT.
cli: IF = 0. No hardware interrupt may arrive while the vector is half-written. FLAGS = F002h.
Low word = IP = offset isr = 0118h: 18 01 in memory (little-endian).
High word = CS = 0700h: 00 07. The vector is now 0700:0118.
sti: IF = 1, interrupts are enabled again. FLAGS = F202h (on the 8086 bit 1 and bits 12–15 always read 1).
AL = '#' (23h): a parameter passed to the ISR in a register.
int 60h: FLAGS (F202h), CS (0700h) and the return IP (0117h) are pushed; IF = 0; CS:IP ← [180h] = 0700:0118.
The ISR runs: AH = 0Eh (teletype).
int 10h: # is printed. An ISR may call another interrupt.
iret: IP = 0117h, CS = 0700h, FLAGS = F202h are restored; IF is 1 again, SP = FFFEh.
ret: the main program resumes where it left off and returns to DOS.
Type 60h is one of the types set aside for user programs. Values assume an emu8086-style .COM load (CS = 0700h).
Hardware Interrupts · Step through the code
Code Trace: Save, Replace, Restore a Vector
org 100h xor ax, ax mov es, ax ; ES = 0: IVT mov ax, offset isr3 xchg ax, es:[3*4] ; new IP ↔ old mov old_vec, ax mov ax, cs xchg ax, es:[3*4+2] ; new CS ↔ old mov old_vec+2, ax mov ax, 1234h int 3 ; our ISR mov bx, old_vec mov es:[3*4], bx ; restore mov bx, old_vec+2 mov es:[3*4+2], bx ret isr3: push ax mov ax, 0E21h ; AH=0Eh, AL='!' int 10h pop ax iret old_vec dw ?, ?
ES = 0000h: the IVT. The type 3 vector is at 3 × 4 = 000Ch.
AX = offset isr3 = 0130h.
xchg ax, es:[12]: one instruction writes the new IP and brings the old IP (1003h) into AX.
The old IP is saved in old_vec.
AX = CS = 0700h.
xchg: the new CS is written; the old CS = F000h is in AX. The vector is now 0700:0130.
The old CS is saved too: the old vector is F000:1003. Only the program calls type 3, so no CLI is needed; for hardware types these lines go between CLI/STI.
AX = 1234h: a value of the main program. The ISR must not destroy it.
int 3 (one byte, CCh): FLAGS, CS and the return IP 011Dh are pushed; CS:IP ← 0700:0130.
push ax: the ISR first saves the AX it is about to use (1234h on the stack).
AX = 0E21h: AH = 0Eh (teletype), AL = 21h ('!').
int 10h: ! is printed.
pop ax: AX = 1234h, the main program’s value is back.
iret: IP, CS and FLAGS are restored; SP = FFFEh.
The old IP is written back into the vector.
The old CS is written back too: type 3 points to the old routine again.
ret: AX = 1234h was preserved, and the vector table is as it was before the program ran.
The old vector F000:1003h is the emulator’s default routine. Under DOS: INT 21h, AH = 35h / 25h.
Hardware Interrupts
Rules for Writing an ISR, and Common Mistakes
| Rule | If broken |
|---|---|
| Save every register you use with PUSH/POP (the processor saves FLAGS). | The main program continues with a corrupted register; the bug shows up at random moments. |
A hardware ISR sends the 8259A an EOI: MOV AL, 20h · OUT 20h, AL. | The 8259A never clears the in-service bit; that IRQ and all lower ones never come again. |
| Keep the ISR short; leave long work to the main program through a flag variable. | Interrupt latency grows and fast devices lose data. |
| Never call INT 21h from a hardware ISR. | DOS is not reentrant; the interrupted DOS call’s data gets corrupted. |
| Write the vector under CLI or with AH = 25h, and restore the old one on exit. | After the program ends, the interrupt jumps into memory that now holds something else. |
End the ISR with IRET. | The stack is off by 4 bytes and IF stays off. |
A long ISR may use STI to let higher-priority interrupts in; the ISR itself must then be reentrant.
Lab · vector table, INT and IRET step by step
Lab: The Interrupt Mechanism
Summary
Summary
The processor sets its work aside, runs the ISR, and returns where it left off with IRET.
0000:0000–03FF, 256 × 4 bytes. Type n → address n × 4 (IP), n × 4 + 2 (CS).
FLAGS, CS, IP onto the stack (SP −6), IF = TF = 0, CS:IP ← vector.
INTR is accepted only when IF = 1, at an instruction boundary; the type number comes from the 8259A on the second INTA pulse. NMI is always INT 2.
The IMR masks individual IRQs (port 21h); the ISR ends with an EOI: MOV AL, 20h · OUT 20h, AL.
Write the vector between CLI/STI, save the registers you use, end with IRET; restore the old vector on exit.
Summary
Exercises
- At which physical addresses is the
INT 13hvector? - If SP = FFFEh before
INT 21hexecutes, what is SP inside the ISR? What is on the stack? - With IMR = B8h, write the instructions that also mask IRQ0 (the timer). What is the new IMR?
- The type 61h vector is to become 0700:0120h. Which bytes go to which physical addresses?
- On a 5 MHz 8086, INTR arrives just as
MUL BX(at most 133 clocks) starts. What is the worst-case interrupt latency? - What problems does ending an ISR with
RETinstead ofIRETcause?
Hints / answers
- 13h × 4 = 4Ch →
0004Ch–0004Fh(IP: 4Ch–4Dh, CS: 4Eh–4Fh). - SP = FFF8h. [FFF8h] = return IP, [FFFAh] = CS, [FFFCh] = FLAGS.
IN AL, 21h·OR AL, 01h·OUT 21h, AL→ IMR = B9h; IRQ3, 4, 5, 7 stay masked.- 61h × 4 = 184h:
00184h–00187h← 20 01 00 07 (IP first, then CS; little-endian). - 133 + 61 (INTR acknowledge) = 194 clocks × 200 ns ≈ 39 µs.
- CS and FLAGS stay on the stack (a near RET leaves SP 4 bytes off), IF stays 0, and the next
RETmay pick up a wrong return address.
Summary
References
- A. S. Tanenbaum, Modern Operating Systems, 3rd ed., Pearson, 2008.
- Intel, The 8086 Family User's Manual, 1979, Interrupts chapter.
- B. B. Brey, The Intel Microprocessors, 8th ed., Pearson, 2009, Chapters 6 and 12.
- emu8086 help: the “Interrupts” and “Supported interrupts” pages.
- R. Brown, Ralf Brown's Interrupt List (BIOS/DOS interrupt reference).