Chapter 5.1: Interrupts

Microprocessors
What an interrupt isKinds of interruptsVector tableINT and IRETExceptions: INT 0, 1, 3, 4Hardware interrupts8259A and maskingYour own handler
Sercan KÜLCÜ · Giresun University · Computer Engineering

Introduction

Learning Outcomes

Concept

Explain what an interrupt is, how it differs from polling, and the kinds of interrupts.

Mechanism

Use the interrupt vector table to work out where INT n goes.

Stack

Trace how INT and IRET push and pop FLAGS, CS and IP.

Hardware interrupts

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).
An everyday analogy

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).

main programmain program (resumes)ISRINT / requestIRETreturn address on stack

The Interrupt Concept

Polling or Interrupts?

Pollingready?ready?ready?ready?ready?ready?ready?ready?ready?ready?The CPU keeps asking the device; most checks are wasted and data may be noticed late.Interruptmain programISRmain program (where it left off)device: “data ready”return with IRET

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

Software interrupt

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.

Hardware interrupt

An external device drives a pin. INTR is maskable (ignored while IF=0); NMI is non-maskable (e.g. memory parity error, power failure).

Internal interrupt (exception)

Generated by the processor itself: divide by zero / quotient overflow → INT 0; single step after every instruction while TF=1 → INT 1.

TypeSourceDescription
00hinternalDivide error (DIV/IDIV by zero, or the quotient does not fit)
01hinternalSingle step (TF = 1), used by debuggers
02hhardwareNMI pin (non-maskable)
03hsoftwareBreakpoint; the one-byte INT 3 (CCh)
04hsoftwareINTO: 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 byan instruction in the programan external device, a pinthe processor itself
Timingsynchronous: always at the same pointasynchronous: after any instructionsynchronous: at the faulting instruction
Type numberin the instructionINTR: from the 8259A, via INTA; NMI: always 2fixed: 0, 1
While IF = 0still runsINTR waits, NMI runsstill runs
Typical useBIOS/DOS services, INT 3, INTOkeyboard, timer, diskdivide error, single step
A common pathOnce the type number is known, all three follow the same steps: FLAGS, CS, IP onto the stack; IF = TF = 0; CS:IP ← vector.
So what differs?Only in where the type number comes from and when the interrupt occurs.

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.
address (0000:…)+0, +1+2, +30000hIPCSINT 00h0004hIPCSINT 01h0008hIPCSINT 02h000ChIPCSINT 03h⋮0040hIP = F065hCS = F000hINT 10h⋮0084hIPCSINT 21h⋮03FChIPCSINT FFh256 entries × 4 bytes = 1024 bytes (0000:0000 – 0000:03FF)

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

INT 10hinterrupt type n = 10h = 16

The program executes INT 10h. The type number is n = 10h (16).

10h × 4 = 40h16 × 4 = 64 → address 0000:0040h

Vector address n × 4: 10h × 4 = 40h. (×4 is a shift left by two bits.)

0040h65h0041hF0h0042h00h0043hF0h} IP = F065h} CS = F000h

These 4 bytes are read: low word IP = F065h, high word CS = F000h (little-endian: low byte first).

physical address = CS × 16 + IPF0000h + F065h = FF065h

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.

The calculation never changes: address = n × 4. IP is at that address, CS at n × 4 + 2.

INT and IRET

How Does INT n Work?

1. FLAGS is pushed: SP ← SP − 2, [SS:SP] ← FLAGS.
2. IF = 0 and TF = 0: the ISR is not interrupted by other maskable interrupts or by single-stepping.
3. CS is pushed.
4. IP (the address of the instruction after INT) is pushed.
5. IP ← [n×4], CS ← [n×4+2]: the ISR begins.
6. The ISR ends with IRET: IP, CS and FLAGS are popped in that order and the program resumes where it left off.
A total of 6 bytes (3 words) goes onto the stack. The old IF is saved with FLAGS, so IRET restores it too.

INT and IRET

INT and IRET: The Stack Step by Step

stack (SS = 0700h)FFF6hFFF8hFFFAhFFFChFFFEh← SPCS0700hIP0104hSPFFFEhFLAGSF202h

INT 10h at 0700:0104. SP = FFFEh, FLAGS = F202h (IF = 1; on the 8086 bit 1 and bits 12–15 always read 1).

stack (SS = 0700h)FFF6hFFF8hFFFAhFFFChF202h← SPFFFEhCS0700hIP0104hSPFFFChFLAGSF202h

FLAGS is pushed: SP = FFFCh, [FFFCh] = F202h.

stack (SS = 0700h)FFF6hFFF8hFFFAhFFFChF202h← SPFFFEhCS0700hIP0104hSPFFFChFLAGSF002h

IF = 0, TF = 0 → FLAGS = F002h. The old value is safe on the stack.

stack (SS = 0700h)FFF6hFFF8hFFFAh0700h← SPFFFChF202hFFFEhCS0700hIP0104hSPFFFAhFLAGSF002h

CS is pushed: SP = FFFAh, [FFFAh] = 0700h.

stack (SS = 0700h)FFF6hFFF8h0106h← SPFFFAh0700hFFFChF202hFFFEhCS0700hIP0104hSPFFF8hFLAGSF002h

The return address is pushed: INT 10h is 2 bytes, so 0106h. SP = FFF8h.

stack (SS = 0700h)FFF6hFFF8h0106h← SPFFFAh0700hFFFChF202hFFFEhCSF000hIPF065hSPFFF8hFLAGSF002h

CS:IP ← vector (F000:F065). The ISR runs and prints the character.

stack (SS = 0700h)FFF6hFFF8h0106hFFFAh0700hFFFChF202h← SPFFFEhCS0700hIP0106hSPFFFChFLAGSF002h

IRET: IP ← 0106h, CS ← 0700h (SP = FFFCh).

stack (SS = 0700h)FFF6hFFF8h0106hFFFAh0700hFFFChF202hFFFEh← SPCS0700hIP0106hSPFFFEhFLAGSF202h

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 / RETINT n / IRET
Target addressin the instruction (a label)from the vector table (n × 4)
PushedIP (near) or CS + IP (far)FLAGS + CS + IP (6 bytes)
FlagsunchangedIF = 0, TF = 0
ReturnRET: pops IP (and CS)IRET: pops IP, CS, FLAGS
Instruction length3 bytes (near CALL)2 bytes (INT 3: 1 byte)
Invoked byonly the programthe program, the processor (exception) or hardware
Ending an ISR with 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!
stack (SS = 0700h)FFF8FFFAFFFCFFFE0000h← SPCS:IP (next)0700:0103SPFFFEhIF = 1FLAGS = F202h

AX = 0: the IVT segment.

stack (SS = 0700h)FFF8FFFAFFFCFFFE0000h← SPCS:IP (next)0700:0105SPFFFEhIF = 1FLAGS = F202h

ES = 0000h.

stack (SS = 0700h)FFF8FFFAFFFCFFFE0000h← SPCS:IP (next)0700:010CSPFFFEhIF = 1FLAGS = F202h

IP of the type 61h vector: [184h] = offset isr = 0114h.

stack (SS = 0700h)FFF8FFFAFFFCFFFE0000h← SPCS:IP (next)0700:0111SPFFFEhIF = 1FLAGS = F202h

CS: [186h] = 0700h. The vector is 0700:0114.

stack (SS = 0700h)FFF80113h← SPFFFA0700hFFFCF202hFFFE0000hCS:IP (next)0700:0114SPFFF8hIF = 0FLAGS = F002h

int 61h: FLAGS (F202h), CS (0700h) and IP (0113h) are pushed: 6 bytes; SP = FFF8h, IF = 0.

stack (SS = 0700h)FFF8FFFA0700h← SPFFFCF202hFFFE0000hleftover: CS, FLAGSCS:IP (next)0700:0113SPFFFAhIF = 0FLAGS = F002h

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.

stack (SS = 0700h)FFF8FFFA0700h← SPFFFCF202hFFFE0000hleftover: CS, FLAGSCS:IP (next)0700:0114SPFFFAhIF = 0FLAGS = F002h

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

INT 0: divide errorDIV or IDIV raises INT 0 in two cases: the divisor is 0, or the quotient does not fit in the destination. 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.
Return addressThe 8086 pushes the address of the instruction after DIV; IRET resumes the program past the division. From the 80286 on, the faulting instruction’s own address is pushed (so it can be retried).
INTOA one-byte instruction (CEh). Calls INT 4 if OF = 1 and does nothing if OF = 0. Place it right after a signed add or subtract: ADD AL, BL · INTO.
What does DOS do?DOS installs its own routine in the INT 0 vector: it prints "Divide overflow" and terminates the program. A program can change this by installing its own handler.

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
IVT 0000:0000h (type 00h)001000F0IPCSvector = F000:1000hstack (SS = 0700)FFF6····FFF8····FFFA····FFFC····FFFE0000← SPAX0000BL00DL00CS:IP0700:0103IF1

AX = 0: the IVT segment.

IVT 0000:0000h (type 00h)001000F0IPCSvector = F000:1000hstack (SS = 0700)FFF6····FFF8····FFFA····FFFC····FFFE0000← SPAX0000BL00DL00CS:IP0700:0105IF1

ES = 0000h: ES:[0] is the vector of type 0 (0 × 4 = 0).

IVT 0000:0000h (type 00h)1B0100F0IPCSvector = F000:011Bhstack (SS = 0700)FFF6····FFF8····FFFA····FFFC····FFFE0000← SPAX0000BL00DL00CS:IP0700:010CIF1

Low word (IP) = offset div_err = 011Bh. The previous vector pointed to the emulator’s default routine.

IVT 0000:0000h (type 00h)1B010007IPCSvector = 0700:011Bhstack (SS = 0700)FFF6····FFF8····FFFA····FFFC····FFFE0000← SPAX0000BL00DL00CS:IP0700:0111IF1

High word (CS) = 0700h. Type 0 now goes to our handler.

IVT 0000:0000h (type 00h)1B010007IPCSvector = 0700:011Bhstack (SS = 0700)FFF6····FFF8····FFFA····FFFC····FFFE0000← SPAX0064BL00DL00CS:IP0700:0114IF1

AX = 100 = 0064h: the dividend.

IVT 0000:0000h (type 00h)1B010007IPCSvector = 0700:011Bhstack (SS = 0700)FFF6····FFF8····FFFA····FFFC····FFFE0000← SPAX0064BL00DL00CS:IP0700:0116IF1

BL = 0: the divisor is zero.

IVT 0000:0000h (type 00h)1B010007IPCSvector = 0700:011Bhstack (SS = 0700)FFF6····FFF80118← SPFFFA0700FFFCF202FFFE0000AX0064BL00DL00CS:IP0700:011BIF0

div bl → INT 0. FLAGS, CS and the return IP 0118h (the instruction after DIV) are pushed; SP = FFF8h, IF = 0.

IVT 0000:0000h (type 00h)1B010007IPCSvector = 0700:011Bhstack (SS = 0700)FFF6····FFF80118← SPFFFA0700FFFCF202FFFE0000AXFFFFBL00DL00CS:IP0700:011EIF0

The handler runs: AX = FFFFh, telling the program “result invalid”.

IVT 0000:0000h (type 00h)1B010007IPCSvector = 0700:011Bhstack (SS = 0700)FFF6····FFF8····FFFA····FFFC····FFFE0000← SPAXFFFFBL00DL00CS:IP0700:0118IF1

iret: IP = 0118h, CS and FLAGS are restored; SP = FFFEh, IF = 1. DIV is not run again.

IVT 0000:0000h (type 00h)1B010007IPCSvector = 0700:011Bhstack (SS = 0700)FFF6····FFF8····FFFA····FFFC····FFFE0000← SPAXFFFFBL00DLFFCS:IP0700:011AIF1

DL = AL = FFh: the main program sees the error from the flag value.

IVT 0000:0000h (type 00h)1B010007IPCSvector = 0700:011Bhstack (SS = 0700)FFF6····FFF8····FFFA····FFFC····FFFE····AXFFFFBL00DLFFCS:IP0700:0000IF1

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:

  1. Saves the first byte of the instruction to stop at and writes CCh in its place.
  2. When the program gets there, INT 3 runs and the registers are shown.
  3. Writes the saved byte back and decrements the return IP on the stack by 1.
  4. 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

1. A device (e.g. the keyboard) activates IRQ1; the 8259A drives the INTR pin high.
2. The 8086 finishes the current instruction; interrupts are never accepted mid-instruction.
3. If IF = 0 the request waits. If IF = 1 it is accepted.
4. The CPU issues two INTA̅ pulses; on the second the 8259A places the type number on the data bus (09h for the keyboard on a PC).
5. From here on it is the same as INT n: FLAGS, CS, IP onto the stack; IF = TF = 0; CS:IP ← [n×4].
6. When the ISR is done it sends the 8259A an EOI (end of interrupt) and returns with IRET.
INTR = 11instr. ends2IF = 1?3INTA̅ ×2 → 09h4pushed5ISR … IRET6time

Hardware Interrupts

Priority and the PC Interrupt Map

8086 priority order

RankSource
1 (highest)Divide error, INT n, INTO
2NMI
3INTR
4 (lowest)Single step (TF)

When several requests arrive at once, the highest priority is served first.

IBM PC hardware interrupts

IRQDeviceType
0Timer (18.2 Hz)08h
1Keyboard09h
3 / 4Serial ports0Bh / 0Ch
6Floppy disk0Eh
7Printer0Fh
A key press first runs the hardware interrupt 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

IRQ0–7IRR (request)IMR (mask)priority +ISR (in service)INTRport 21h: bit = 1 → that IRQ blockedEOI: port 20h ← 20hpending requestsINTA: the 8259A puts the type number (base + IRQ no.) on the data bus
IRRHolds requests that have arrived but not yet been served.
IMRInterrupt mask register: one bit per IRQ. IF disables all of INTR; the IMR disables individual lines.
ISR (register)Marks the IRQ being served; lower priorities wait until an EOI arrives.

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
AL1IRQ70IRQ61IRQ51IRQ41IRQ30IRQ20IRQ10IRQ0B8hIMR10111000B8htimerkeybdCOM2COM1floppyprinterred = masked (1) · port 21h = IMR

in al, 21h: the IMR is read, AL = B8h = 1011 1000b. IRQ3, 4, 5, 7 are already masked (sample value).

AL1IRQ70IRQ61IRQ51IRQ41IRQ30IRQ21IRQ10IRQ0BAhIMR10111000B8htimerkeybdCOM2COM1floppyprinterred = masked (1) · port 21h = IMR

or al, 02h: only bit 1 is set → AL = BAh. The other bits are kept; nothing is written to the IMR yet.

AL1IRQ70IRQ61IRQ51IRQ41IRQ30IRQ21IRQ10IRQ0BAhIMR10111010BAhtimerkeybdCOM2COM1floppyprinterred = masked (1) · port 21h = IMR

out 21h, al: IMR = BAh. IRQ1 (keyboard) can no longer reach INTR; IF and the other IRQs are unaffected.

AL1IRQ70IRQ61IRQ51IRQ41IRQ30IRQ21IRQ10IRQ0BAhIMR10111010BAhtimerkeybdCOM2COM1floppyprinterred = masked (1) · port 21h = IMR

Work that must not be interrupted (just a placeholder here). A key press waits in the IRR; it is not lost.

AL1IRQ70IRQ61IRQ51IRQ41IRQ30IRQ21IRQ10IRQ0BAhIMR10111010BAhtimerkeybdCOM2COM1floppyprinterred = masked (1) · port 21h = IMR

The IMR is read again: AL = BAh.

AL1IRQ70IRQ61IRQ51IRQ41IRQ30IRQ20IRQ10IRQ0B8hIMR10111010BAhtimerkeybdCOM2COM1floppyprinterred = masked (1) · port 21h = IMR

and al, 0FDh (1111 1101b): only bit 1 is cleared → AL = B8h.

AL1IRQ70IRQ61IRQ51IRQ41IRQ30IRQ20IRQ10IRQ0B8hIMR10111000B8htimerkeybdCOM2COM1floppyprinterred = masked (1) · port 21h = IMR

out 21h, al: IMR = B8h, IRQ1 is enabled; the pending keyboard request is served now.

AL1IRQ70IRQ61IRQ51IRQ41IRQ30IRQ20IRQ10IRQ0B8hIMR10111000B8htimerkeybdCOM2COM1floppyprinterred = masked (1) · port 21h = IMR

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?

Mid-instructionThe request waits until the current instruction finishes. Prefixes (LOCK, REP, segment override) count as part of the instruction; no interrupt is accepted between a prefix and its instruction.
After loading SS, and after STIAfter 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.
String instructions with REPA long 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 and WAITHLT 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 n51
INT 352
INTO (OF = 1 / OF = 0)53 / 4
INTR acknowledge (both INTAs)61
IRET24
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 90h prints * and the program continues.
  • Under DOS, INT 21h with 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
IVT 0000:0180h (type 60h)????????IPCSstack (SS = 0700h)FFF8FFFAFFFCFFFE0000h← SPCS:IP (next)0700:0103FLAGSF202hIF = 1screen:

AX = 0. The interrupt vector table (IVT) is at 0000:0000; the vector for type 60h is at 60h × 4 = 180h.

IVT 0000:0180h (type 60h)????????IPCSstack (SS = 0700h)FFF8FFFAFFFCFFFE0000h← SPCS:IP (next)0700:0105FLAGSF202hIF = 1screen:

ES = 0000h: ES:[180h] now refers to byte 0000:0180h of the IVT.

IVT 0000:0180h (type 60h)????????IPCSstack (SS = 0700h)FFF8FFFAFFFCFFFE0000h← SPCS:IP (next)0700:0106FLAGSF002hIF = 0screen:

cli: IF = 0. No hardware interrupt may arrive while the vector is half-written. FLAGS = F002h.

IVT 0000:0180h (type 60h)1801????IPCSstack (SS = 0700h)FFF8FFFAFFFCFFFE0000h← SPCS:IP (next)0700:010DFLAGSF002hIF = 0screen:

Low word = IP = offset isr = 0118h: 18 01 in memory (little-endian).

IVT 0000:0180h (type 60h)18010007IPCSvector = 0700:0118hstack (SS = 0700h)FFF8FFFAFFFCFFFE0000h← SPCS:IP (next)0700:0112FLAGSF002hIF = 0screen:

High word = CS = 0700h: 00 07. The vector is now 0700:0118.

IVT 0000:0180h (type 60h)18010007IPCSvector = 0700:0118hstack (SS = 0700h)FFF8FFFAFFFCFFFE0000h← SPCS:IP (next)0700:0113FLAGSF202hIF = 1screen:

sti: IF = 1, interrupts are enabled again. FLAGS = F202h (on the 8086 bit 1 and bits 12–15 always read 1).

IVT 0000:0180h (type 60h)18010007IPCSvector = 0700:0118hstack (SS = 0700h)FFF8FFFAFFFCFFFE0000h← SPCS:IP (next)0700:0115FLAGSF202hIF = 1screen:

AL = '#' (23h): a parameter passed to the ISR in a register.

IVT 0000:0180h (type 60h)18010007IPCSvector = 0700:0118hstack (SS = 0700h)FFF80117h← SPFFFA0700hFFFCF202hFFFE0000hCS:IP (next)0700:0118FLAGSF002hIF = 0screen:

int 60h: FLAGS (F202h), CS (0700h) and the return IP (0117h) are pushed; IF = 0; CS:IP ← [180h] = 0700:0118.

IVT 0000:0180h (type 60h)18010007IPCSvector = 0700:0118hstack (SS = 0700h)FFF80117h← SPFFFA0700hFFFCF202hFFFE0000hCS:IP (next)0700:011AFLAGSF002hIF = 0screen: inside the ISR

The ISR runs: AH = 0Eh (teletype).

IVT 0000:0180h (type 60h)18010007IPCSvector = 0700:0118hstack (SS = 0700h)FFF80117h← SPFFFA0700hFFFCF202hFFFE0000hCS:IP (next)0700:011CFLAGSF002hIF = 0screen: #inside the ISR

int 10h: # is printed. An ISR may call another interrupt.

IVT 0000:0180h (type 60h)18010007IPCSvector = 0700:0118hstack (SS = 0700h)FFF8FFFAFFFCFFFE0000h← SPCS:IP (next)0700:0117FLAGSF202hIF = 1screen: #inside the ISR

iret: IP = 0117h, CS = 0700h, FLAGS = F202h are restored; IF is 1 again, SP = FFFEh.

IVT 0000:0180h (type 60h)18010007IPCSvector = 0700:0118hstack (SS = 0700h)FFF8FFFAFFFCFFFECS:IP (next)0700:0000FLAGSF202hIF = 1screen: #

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 ?, ?
IVT 0000:000Ch (type 03h)031000F0IPCSvector = F000:1003hstack (SS = 0700)FFF6····FFF8····FFFA····FFFC····FFFE0000← SPAX0000BX0000CS:IP0700:0104IF1output:

ES = 0000h: the IVT. The type 3 vector is at 3 × 4 = 000Ch.

IVT 0000:000Ch (type 03h)031000F0IPCSvector = F000:1003hstack (SS = 0700)FFF6····FFF8····FFFA····FFFC····FFFE0000← SPAX0130BX0000CS:IP0700:0107IF1output:

AX = offset isr3 = 0130h.

IVT 0000:000Ch (type 03h)300100F0IPCSvector = F000:0130hstack (SS = 0700)FFF6····FFF8····FFFA····FFFC····FFFE0000← SPAX1003BX0000CS:IP0700:010CIF1output:

xchg ax, es:[12]: one instruction writes the new IP and brings the old IP (1003h) into AX.

IVT 0000:000Ch (type 03h)300100F0IPCSvector = F000:0130hstack (SS = 0700)FFF6····FFF8····FFFA····FFFC····FFFE0000← SPAX1003BX0000CS:IP0700:010FIF1output:

The old IP is saved in old_vec.

IVT 0000:000Ch (type 03h)300100F0IPCSvector = F000:0130hstack (SS = 0700)FFF6····FFF8····FFFA····FFFC····FFFE0000← SPAX0700BX0000CS:IP0700:0111IF1output:

AX = CS = 0700h.

IVT 0000:000Ch (type 03h)30010007IPCSvector = 0700:0130hstack (SS = 0700)FFF6····FFF8····FFFA····FFFC····FFFE0000← SPAXF000BX0000CS:IP0700:0116IF1output:

xchg: the new CS is written; the old CS = F000h is in AX. The vector is now 0700:0130.

IVT 0000:000Ch (type 03h)30010007IPCSvector = 0700:0130hstack (SS = 0700)FFF6····FFF8····FFFA····FFFC····FFFE0000← SPAXF000BX0000CS:IP0700:0119IF1output:

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.

IVT 0000:000Ch (type 03h)30010007IPCSvector = 0700:0130hstack (SS = 0700)FFF6····FFF8····FFFA····FFFC····FFFE0000← SPAX1234BX0000CS:IP0700:011CIF1output:

AX = 1234h: a value of the main program. The ISR must not destroy it.

IVT 0000:000Ch (type 03h)30010007IPCSvector = 0700:0130hstack (SS = 0700)FFF6····FFF8011D← SPFFFA0700FFFCF246FFFE0000AX1234BX0000CS:IP0700:0130IF0output:

int 3 (one byte, CCh): FLAGS, CS and the return IP 011Dh are pushed; CS:IP ← 0700:0130.

IVT 0000:000Ch (type 03h)30010007IPCSvector = 0700:0130hstack (SS = 0700)FFF61234← SPFFF8011DFFFA0700FFFCF246FFFE0000AX1234BX0000CS:IP0700:0131IF0output:

push ax: the ISR first saves the AX it is about to use (1234h on the stack).

IVT 0000:000Ch (type 03h)30010007IPCSvector = 0700:0130hstack (SS = 0700)FFF61234← SPFFF8011DFFFA0700FFFCF246FFFE0000AX0E21BX0000CS:IP0700:0134IF0output:

AX = 0E21h: AH = 0Eh (teletype), AL = 21h ('!').

IVT 0000:000Ch (type 03h)30010007IPCSvector = 0700:0130hstack (SS = 0700)FFF61234← SPFFF8011DFFFA0700FFFCF246FFFE0000AX0E21BX0000CS:IP0700:0136IF0output: !

int 10h: ! is printed.

IVT 0000:000Ch (type 03h)30010007IPCSvector = 0700:0130hstack (SS = 0700)FFF6····FFF8011D← SPFFFA0700FFFCF246FFFE0000AX1234BX0000CS:IP0700:0137IF0output: !

pop ax: AX = 1234h, the main program’s value is back.

IVT 0000:000Ch (type 03h)30010007IPCSvector = 0700:0130hstack (SS = 0700)FFF6····FFF8····FFFA····FFFC····FFFE0000← SPAX1234BX0000CS:IP0700:011DIF1output: !

iret: IP, CS and FLAGS are restored; SP = FFFEh.

IVT 0000:000Ch (type 03h)03100007IPCSvector = 0700:1003hstack (SS = 0700)FFF6····FFF8····FFFA····FFFC····FFFE0000← SPAX1234BX1003CS:IP0700:0126IF1output: !

The old IP is written back into the vector.

IVT 0000:000Ch (type 03h)031000F0IPCSvector = F000:1003hstack (SS = 0700)FFF6····FFF8····FFFA····FFFC····FFFE0000← SPAX1234BXF000CS:IP0700:012FIF1output: !

The old CS is written back too: type 3 points to the old routine again.

IVT 0000:000Ch (type 03h)031000F0IPCSvector = F000:1003hstack (SS = 0700)FFF6····FFF8····FFFA····FFFC····FFFE····AX1234BXF000CS:IP0700:0000IF1output: !

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

RuleIf 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

Interrupt

The processor sets its work aside, runs the ISR, and returns where it left off with IRET.

Vector table

0000:0000–03FF, 256 × 4 bytes. Type n → address n × 4 (IP), n × 4 + 2 (CS).

INT n

FLAGS, CS, IP onto the stack (SP −6), IF = TF = 0, CS:IP ← vector.

Hardware interrupt

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.

8259A

The IMR masks individual IRQs (port 21h); the ISR ends with an EOI: MOV AL, 20h · OUT 20h, AL.

Your own ISR

Write the vector between CLI/STI, save the registers you use, end with IRET; restore the old vector on exit.

Summary

Exercises

  1. At which physical addresses is the INT 13h vector?
  2. If SP = FFFEh before INT 21h executes, what is SP inside the ISR? What is on the stack?
  3. With IMR = B8h, write the instructions that also mask IRQ0 (the timer). What is the new IMR?
  4. The type 61h vector is to become 0700:0120h. Which bytes go to which physical addresses?
  5. On a 5 MHz 8086, INTR arrives just as MUL BX (at most 133 clocks) starts. What is the worst-case interrupt latency?
  6. What problems does ending an ISR with RET instead of IRET cause?
Hints / answers
  1. 13h × 4 = 4Ch → 0004Ch–0004Fh (IP: 4Ch–4Dh, CS: 4Eh–4Fh).
  2. SP = FFF8h. [FFF8h] = return IP, [FFFAh] = CS, [FFFCh] = FLAGS.
  3. IN AL, 21h · OR AL, 01h · OUT 21h, AL → IMR = B9h; IRQ3, 4, 5, 7 stay masked.
  4. 61h × 4 = 184h: 00184h–00187h ← 20 01 00 07 (IP first, then CS; little-endian).
  5. 133 + 61 (INTR acknowledge) = 194 clocks × 200 ns ≈ 39 µs.
  6. CS and FLAGS stay on the stack (a near RET leaves SP 4 bytes off), IF stays 0, and the next RET may 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).

The End

Chapter 5.1: Interrupts
Previous: Chapter 4.2: Variables · Next: Chapter 5.2: Interrupt Functions
1 / 1 Sercan KÜLCÜ. All rights reserved.