Professional Documents
Culture Documents
3002 Microprocessor - S4 - Interrupt
3002 Microprocessor - S4 - Interrupt
8086 Interrupt
Interrupt
Interrupt:
suspend (or interrupt) the “current” program,
run an interrupt service routine (ISR),
resume the suspended program upon completion of the
interrupt service routine.
CS:IP
Interrupt
Current Service
Program routine
(ISR)
IRET
Similar to a FAR CALL, except ...
2
How to raise an Interrupt?
3 ways to raise an interrupt (i.e. to activate or call an
Interrupt Service Routine):
1. Hardware: an external hardware raises a signal at
INTR or NMI pins of 8086.
2. Software: programmer issues an “INT n” instruction
(similar to a FAR CALL instruction – which can only be
issued via software instruction).
3. Internally Generated: µP automatically triggers an
interrupt upon certain events, such as “Divide by Zero”
or “General Protection Fault”.
Implementing Interrupt
For example, you have a switch connecting to the INTR pin.
Upon pressing the switch, you wish to sound a alarm.
Step 1: pick an Interrupt type number for your application
(says Type 54H).
Step 2: Write the Interrupt Service Routine, which will be
called upon activating the interrupt (pressing the switch):
ISR SEGMENT ;Segment begins at ABCD0H
ORG 1234H
ISR54H PROC
MOV DX, 88H ;Alarm connected to port 88H
MOV AL, 01H ;send 01H to sound the alarm
OUT DX, AL
IRET
ISR54H ENDP
ISR ENDS
6
Interrupt Vector (ISR Starting Address)
Each interrupt type is associated with a user-written Interrupt Service
Routine (ISR) via an Interrupt Vector.
An Interrupt Vector is a memory address (4-byte), in terms of a 16-bit
segment and 16-bit offset, that points to the starting address of the ISR
stored in memory.
Interrupt Vector = Starting Address of ISR.
Interrupt vector allows you to find the interrupt service routine.
Type N
Interrupt Type N
Interrupt Vector
Type N Interrupt
raised :
Service
Segment : Offset Routine
16-bit 16-bit
(ISR)
ISR N starting address
7
8
Interrupt Vector Table (cont.)
Int 5 vector : 15 14
17 16
Int 4 vector
13 12
: 11 10
Int 3 vector
F E : D C
Int 2 vector B A : 9 8
Int 1 vector 7 6 : 5 4
Int 0 vector 3 2 : 1 0
16-bit Segment 16-bit Offset
9
11
12
Interrupt Processing
Steps involved in an INT processing:
1. PUSHF
2. IF ← 0 and TF ← 0 (Disable INTR and Single-step
Interrupts to prevent nested INTR and Trap)
3. PUSH return-CS
4. PUSH return-IP
5. CS:IP ← [n×4] (retrieve the interrupt vector, which gives
the starting address of the ISR)
A corresponding IRET instruction terminates the ISR and
returns control to the interrupted routine, by popping the IP,
CS and Flags from the stack. Note that IRET is different
from RET.
13
14
003FFH
16
Predefined Interrupt Types (cont.)
Type 4 (INTO): responds to an INTO instruction, triggered
if the OF (Overflow Flag) is set.
Type 5 (BOUND): responds to a BOUND instruction,
triggered if the array index is out-of-bound.
Type 6 (Invalid Opcode): occurs when an invalid opcode
occurs in the program, fetched by CS:IP.
Type 8 (Double fault): activated whenever two separate
interrupts occur during the same instruction.
Type 9-12: others.
Type 13 (General Protection Fault): internally generated
by the µP whenever there is a protection error in the
protected mode.
Type 14-16: others.
Type 17-31: reserved by Intel
Type 32-255: user-defined, for your specific purpose.
17
Types of Interrupts
Recall that there are 3 broad categories of interrupts:
Software Interrupt: programmer issues instructions
such as INT n, INTO etc. For example,
MOV AX, 1234H
INT 21H ; call ISR for type 21H
MOV BX, AX
Internally generated: µP automatically triggers an
interrupt for certain conditions such as Divide Error (Type
0), General Protection Fault (Type 13), Invalid Opcode
(Type 6).
Hardware Interrupt: triggered by applying appropriate
hardware signal to INTR or NMI pins.
18
Hardware Interrupt
One of the main purpose of interrupt is to service external
I/O requests (such as keyboard, printer etc).
Important to note that I/O devices typically operate at a
much slower rate compared with CPU.
Two approaches for servicing external I/O requests:
Software Polling:
y program continuously checks for I/O request;
y branch to I/O service routine once an I/O request is
sensed;
y after I/O routine completed, revert back to check.
Hardware Interrupt:
y µp executing instructions, external I/O signals a service
request by triggering a hardware interrupt;
y branch to an interrupt service routine to process the I/O;
y revert back to the suspended routine after I/O serviced.
19
20
Hardware Interrupt
2 hardware interrupt request pins:
NMI (non-maskable interrupt request):
y positive edge-triggered (0→1 transition)
y cannot be masked or “disabled”, triggers type 2 interrupt.
y commonly used for major hardware faults such as RAM
parity check failure.
INTR (Interrupt Request) & INTA (Interrupt
Acknowledged):
y INTR is positive level-triggered, it must be held at logic 1
until it is recognized.
y maskable: can be enabled/disabled via the IF (Interrupt
Flag) using STI (Set Interrupt) and CLI (Clear Interrupt)
instruction.
y The µP responds to an INTR request by sending INTA
output, for receiving an interrupt type number via D0-D7.
21
24
Multiple Devices interfacing to INTR
D7
D6
D5 Low Data Bus
D4
D3
D2
D1
D0
1 1 1 1 1 2 2 2 74LS244
YYYYYYYY
12341234
1G 11112222
INTA 2G AAAAAAAA
12341234
IR0
IR1
IR2
INTR IR3
IR4
IR5
8086/8 74LS30 IR6
IR7 25
IRQ0 IRQ8
IRQ1 IRQ9
IRQ2 IRQ10
8259A IRQ3 8259A IRQ11
Master IRQ4 Slave IRQ12
IRQ5 IRQ13
IRQ6 IRQ14
IRQ7 IRQ15
26