Professional Documents
Culture Documents
Lecture 7 - Interrupt
Lecture 7 - Interrupt
1 2
Cannot be masked (disabled). Belonging to externally activated hardware Interrupts can be triggered by:
interrupts can be masked.
Software (also called Exceptions)
Automatically saves only CS:IP of the Saves FR (flag register) in addition to CS:IP
next instruction on the stack. of the next instruction. Execution of INT instruction
At the end of subroutine that has been The last instruction in the interrupt service some other internal event (e.g. division error)
called by the "CALL FAR" instruction, the routine (ISR) for "INT nn" is the instruction
Hardware
RETF (return FAR) is the last instruction IRET (interrupt return). The difference is that
RETF pops CS, IP off the stack but the IRET Peripheral devices usually generate external interrupt
3 4
Hardware Interrupt NMI (Input to 8088/86)
The NMI (Non-maskable interrupt) is a positive-edge-
The following 3 pins are associate with hardware sensitive external input to the 8088/86
interrupts:
INTR, (Interrupt Request) The NMI cannot be ignored by the 8088/86 – not
NMI (Non-maskable Interrupt) maskable - cannot be ignored by software (CLI & STI)
INTA (Interrupt Acknowledge).
An NMI is at INT 02 => 02*4=08 will automatically be called
INTR is an input signal into the CPU which can be Its interrupt vector is stored at addresses 08, 09, 0A,
masked (ignored) and unmasked through the use of
instructions CLI and STI. and 0B.
NMI, which is also an input signal into the CPU, cannot In order to be recognized as a positive edge, the NMI
be masked and unmasked using instructions CLI and line must be low for two clock cycles
STI, and for this reason it is called nonmaskable interrupt. The external device must hold NMI high after causing
INTR and NMI are activated externally by putting 5V on the edge.
the pins of NMI and INTR of the 80x86 microprocessor.
It is useful in small scale projects, and for emergency
operations (memory parity checks)
5 6
7 8
8088/86 Interrupt Sequence Interrupt Service Routines (ISR)
When an interrupt occur, it
An Interrupt Service Routine is simply an assembly
Completes execution of the current instruction
language program that handles the device requesting
Pushes the flag register onto the stack an interrupt.
Clears the interrupt enable bit
An interrupt service routine is terminated by an IRET
Pushes the CS of the next instruction onto the stack instruction that
Pushes the IP of the next instruction onto the stack pops the CS of the next instruction from the stack
Reads the interrupt “type” from the data bus pops the IP of the next instruction from the stack
Does a “Jump Indirect” to the address stored at pops the flag register from the stack
type*4, type*4 + 1 IP of interrupt service routine
type*4 + 2, type*4 + 3 CS of interrupt service routine Q: What function must the interrupt service routine perform?
e.g. INT 05 – Physical address = 00014H,
Logical address = 0000:0014 (CS:IP) A: To make the INTR line to go low!
Note: this implies that the lowest 1K (256x4) of memory is
reserved to hold the interrupt vector table.
9 10
E.g.
Intel has embedded "INT 02" into the 80x86
Multiple Interrupts
microprocessor to be used only for NMI.
Whenever the NMI pin is activated, the CPU will go to memory location 00008
to get the address (CS:IP) of the interrupt service routine (ISR) associated with A PC processor can receive interrupt requests from
NMI. more than one source
Memory locations 00008, 00009, 0000A, and 0000B contain the 4 bytes of CS:IP System has to resolve two things:
of the ISR belonging to NMI.
In contrast, this is not the case for the other hardware pin, INTR. There is no Identify what device requested the interrupt
specific location in the vector table assigned to INTR. Prioritise Interrupts when two requests happen at the same
The reason is that INTR is used to expand the number of hardware interrupts time.
and should be allowed to use any "INT nn" which has not been previously
assigned.
The 8259 programmable interrupt controller (PIC) chips can be connected to
INTR to expand the number of hardware interrupts up to 64.
In the case of the IBM PC, one Intel 8259 PIC chip is used to add a total of 8
hardware interrupts to the microprocessor.
IBM PC/AT, 80286, 80386, 80486, and Intel Pentium computers use two 8259
chips to allow up to 16 hardware interrupts.
11 12
Interrupt priorities
Either s/w or An interrupt service routine can itself be interrupted
h/w interrupt
only by a higher priority interrupt
Interrupt Priorities (from highest to lowest)
Vector Reset
Internal interrupts and exceptions
Table Software interrupts
Non-maskable interrupt
External hardware interrupts
For external hardware interrupts,
the higher the type number, the lower the priority
From the book The 80x86 IBM PC by Muhammad Ali From the book The 80x86 IBM PC by Muhammad Ali
17 18
Mazidi and Janice Gilispie pg. 434 Mazidi and Janice Gilispie pg. 436
Timing E.g.
The INTR pin must be externally decoded to select a vector.
Any vector is possible, but the interrupt vectors between 20H and An interrupting device interrupts the microprocessor each time the
FFH are usually used (Intel reserves vectors between 00H and 1FH). interrupt request input has a transition from 0 to 1.
INTA’ is an output of the microprocessor to signal the external 74LS244 creates the interrupt type number 60H as a response to
decoder to place the interrupt number on data bus connections D7- INTA.
D0.
Assume:
The INTR pin is set by an external device (8259A) and cleared in CS=DS=1000H
the Interrupt Service Routine.
SS=4000H
The input is automatically disabled by the microprocessor once it is Main program offset is 200H
recognized and re-enabled by IRET instruction. Count offset is 100H
Timing diagram of the handshake. Interrupt-service routine code segment is 2000H:0000H
Interrupt-service routine code offset is 1000H
Stack has an offset of 500H to the current stack segment
Sketch a circuit schematic which includes a 8088 processor, a
74LS74 Flip-flop and a & a 74LS244 Buffer.
Write a main program and a service routine to count the number of
positive interrupt transitions.
Interrupts the
microprocessor each time
Active-High the interrupt request
Level-Sensitive signal has a transition
from 0 to 1.
E.g.
The corresponding
interrupt number (cont’)
generated by the
hardware in response to
INTA is 60H
From the book Intel Microprocessor Barry. B. Brey 21 From the book Intel Microprocessor Barry. B. Brey 22
pg. 474 pg. 473
8088 /
8086
1000H
From the book Intel Microprocessor Barry. B. Brey 23 From the book Intel Microprocessor Barry. B. Brey 24
pg. 475 pg. 474
Interrupt Vector Generation: More than 1 IRQ Relation between Interrupt and Flag Register
Among bits D0 to D15 of the flag register, there are two bits that are
associated with the interrupt:
D9, or IF (interrupt enable flag), D8, or TF (trap or single-step flag).
IF: The interrupt flag is used to mask (ignore) any hardware interrupt
that may come in from the INTR pin.
If IF = 0, all hardware interrupt requests through INTR are ignored.
This has no effect on interrupts coming from the NMI pin or "INT nn"
instructions.
The instruction CLI (clear interrupt flag) will make IF = 0.
To allow interrupt requests through the INTR pin, this flag must be set to
one (IF = 1).
Note that if more than one IRQ The STI (set interrupt flag) instruction can be used to set IF to 1.
goes low,
• An interrupt priority needs to be The trap flag (TF) is explained in following slides when "INT 01", the
defined single-step interrupt, is discussed.
• A unique interrupt vector is
generated
INT 0 is also invoked if the quotient is too large to fit into the
65535/2 =32767 larger than 255 .maximum capacity of AL
27 28
INT 01 Control Flags - Revision
In executing a sequence of instructions, there is often a need to examine
the contents of the CPU's registers and system memory. FlagsH FlagsL
This is often done by executing the program one instruction at a time 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
and then inspecting registers and memory. X X X X OF DF IF TF SF ZF X AF X PF X CF
This is commonly referred to as single-stepping, or performing a trace.
Intel has designated INT 01 specifically for implementation of single-
stepping. CF Carry Flag high-order bit carry or borrow
To single-step, the Trap Flag (TF), D8 of the flag register, must be set to PF Parity Flag low-order 8 bits contain even number of 1’s
1.
Then after execution of each instruction, the 8086/88 automatically AF Auxiliary Carry low-order 4 bits carry or borrow of AL
jumps to physical location 00004 to fetch the 4 bytes for CS:IP of the ZF Zero Flag result is zero
interrupt service routine, whose job is, among other things, to dump the
registers onto the screen. SF Sign Flag equal to high-order bit
Resetting the Trap Flag
TF Single-step Flag if set, single-step interrupt occurs
PUSHF PUSHF control IF Interrupt-enable if set, maskable interrupts enabled
POP AX OR MOV BP,SP
AND AX,1111111011111111B AND [BP]+0,1111111011111111B
DF Direction Flag if set, auto-decrement for string instructions
PUSH AX POPF OF Overflow Flag signed result too large for destination
POPF 29 30
31 32
INT 04 (Signed Number Overflow) PIC – 8259 (Programmable Interrupt Controller)
Invoked by a signed number overflow condition.
There is an instruction associated with this, INTO (Interrupt On The 8259A PIC provides 8
Overflow).
priority-encoded interrupts to
If the instruction INTO is placed after a signed number arithmetic or
logic operation such as IMUL or ADD, the CPU will activate INT 04 if the CPU.
OF = 1.
By default IR0 is assigned
In cases where OF = 0, the INTO instruction is not executed but is
bypassed and acts as a NOP (no operation) instruction. the highest priority and IR7
E.g. MOV AL,DATA1
the lowest.
MOV BL,DATA2 D0 - D7 are connected to
ADD AL,BL
INTO system data bus to receive
commands from the CPU.
Assume DATA1 =+64 =0100 0000, DATA2 =+64 =0100 0000.
The INTO instruction will be executed and the 8086/88 will jump to physical
location 00010H, the memory location associated with INT 04.
33 34
39 40
8259A I/O Pins Connecting 8259A to 8086/88
D0-7
IR0
INTR
Each line being 8086/ 8259
connected to PIC
an 8088 INTA*
interrupt NMI IR7
requesting I/O
ICW1
E.g
The Initialization Command Words (ICWs) are programmed
before the 8259A is able to function in the system and dictate
Find the address for ICW1 - ICW4 if chip the basic operation of the 8259A.
How can the 8259 differentiate between ICW2, ICW3, and
select is activated by A7 - Al = 0010011.
ICW4 when they are sent to the same address?
A7 A6 A5 A4 A3 A2 A1 A0 When *ICW1.D0 (the LSB of ICW1) = 1, ICW4 will be referred to.
When *ICW1.D1 = 1, the system is configured in single mode.
0 0 1 0 0 1 1 0 ICW3 would be excluded in the initialization sequence.
* Refer to slides 45 and 46
0 0 1 0 0 1 1 1 The initialization sequence must always start with ICW1,
followed by ICW2, and finally the rests, if needed.
Solution:
Writing ICW1 to 8259 clears…
The above shows ISR and IMR
• 26H to be the port for ICW1 and Special Masked Mode in OCW3
• 27H to be the port for ICW2, ICW3, ICW4. IRR in OCW3
EOI in OCW2
From the book The 80x86 IBM PC by Muhammad Ali
43 44
Mazidi and Janice Gilispie pg. 422
ICW1
Initialization flow
chart
From 8259 datasheet From the book The 80x86 IBM PC by Muhammad Ali
45 46
Mazidi and Janice Gilispie pg. 423
ICW2
E.g.
What value should be written into ICW1, in order to configure the It is the function of ICW2 to assign interrupt numbers to
8259 so that ICW4 is needed in the initialization sequence, the
system is going to use multiple 8259 and its inputs are to be level IR0-IR7.
sensitive? Assume that all unused bits are to be logic 0.
The lower three bits, D0, D1, and D2, vary from 000 to
111.
Since ICW4 is to be initialized, D0=1. Along with D3 -D7 (T3 through T7), they form the 8-bit INT type
For cascaded mode , D1=0.
number assigned to the corresponding IR0 through IR7.
And for level sensitive inputs, D3=1. That means that D3 - D7 can only be programmed according to the
assignment of the INT type
D4=1
While the lower bits D0 to D2 being provided by the 8259, depending on
Bits D2,D5 to D7 are in Don’t Care states => D2=D5=D6=D7=0 which of IR0 to IR7 is activated.
To set the 8255 µp so that type numbers are in the range F016 to F716…
The device code bits must be D7 D6D5D4D3 = 111102
The D2D1D0 are in don’t care states and all can be 0s
1
Command word D7 D6D5D4D3 D2D1D0 = 11110000 = F016
0
From the book The 80x86 IBM PC by Muhammad Ali From the book The 8088 and 8086 microprocesssors,
49 50
Mazidi and Janice Gilispie pg. 4224 walter A.Triebel, Avtar Singh pg. 601
The other IR inputs of the master are to be supplied by INT outputs of slaves.
Therefore their control bits must be all logic 1:
Command word = 11110000
ICW4 (II)
D2 (M/S) and D3 (BUF) are for systems where data
buses are buffered with the use of bidirectional ICW4
transceivers.
The 8259 can work either in buffered (D3=1) or non-
buffered (D3=0) mode.
In buffered mode, SP’/EN’ line is configured as EN’
EN’ is a controls output that can be used to control the
direction of data transfer through the bus receiver.
Logic 0 when data transferred from the 8259 to the MPU.
In non-buffered mode, the SP’/EN’ is configured to
work as the master/slave mode select input.
SP’=1 for master mode and SP’=0 for slave mode
D4 is for SFNM (special fully nested mode).
Used only in cascaded mode and enabled (D4 =1)only for the
Master 8259;
Otherwise, it is 0
From the book The 80x86 IBM PC by Muhammad Ali
55 56
Mazidi and Janice Gilispie pg. 424
Solution:
E.g. (a) From Figure, we get the following for each of the ICWs:
ICW1
• D0=1 ICW4 needed
• Dl =1 single
(a) Find the ICWs of the 8259 if it is used with an • D2=0 this is always zero for 80x86 CPUs
• D3=1 level triggering
8088/86 CPU, single, level triggering IRs, and; IR0 is • D4=1 required by the ICWI itself
• D5=D6=D7=0 this is always zero for 80x86 CPUs
assigned "INT50H". The 8259 is in slave buffered This gives ICW1 = 00011011= 1BH.
mode with normal EOI. ICW2
• Always equate ICW2 to the INT # assigned to IR0;
(b) Show the program to initialize the 8259 using the • ICW2 = 01010000 = 50H.
• Notice that "INT nn", assigned to IR0, can decide only bits D7 - D3 (T7 - T3) of ICW2.
port addresses in previous example in slide 43 • This means that the "INT nn" assigned to IR0 must have the lower three bits = 000;
(c) Find the addresses associated with IR0, IR1, and therefore, it can take either values of X0H or X8H, where X is a hex number.
• For example "INT 45H" cannot be assigned to IR0.
IR2 in the interrupt vector table. No ICW3 is needed since it is single and not cascaded.
• ICW4
• Note: This example is not PC-compatible and is • D0=1 8088/86
given only for exercise. • D1=0 normal (we must issue EOI before IRET instruction)
• D2 =0;D3=1 slave buffered mode
• D4=0 not nested
• D5 =D6 =D7 =0 required by the ICW4
ICW4 =00001001 =09H.
From the book The 80x86 IBM PC by Muhammad Ali
57 58
Mazidi and Janice Gilispie pg. 425
(c) If "INT 50H" is assigned to IR0, then IR1 and IR2 have "INT 51H"
and "INT 52", respectively, and so on.
The vector memory locations associated with the IRs are as follows;
Vector Location
IRQ (Pin of 8259) INT Logical Address Physical Address
IR0 50H 0000:0140H-0143 00140H-00143
IR1 51H 0000:0144H-0147 00144H-00147
IR2 52H 0000:0148H-014B 00148H-0014B
59 60
OCW (Operation Command Word) E.g.
After ICW1, ICW2, and ICW4 have been issued in
sequence to the 8259 chip in order to initialize it, the Find the port addresses for the OCWs
8088/86 is ready to receive hardware interrupts through of the 8259 in eg. in slide 43
the 8259's IR0 - IR7 pins.
After the process of initialization, the OCW can be sent to
mask any of IR0 - IR7, or change the priority assigned to Solution:
each IR. A7 A6 A5 A4 A3 A2 Al A0
0 0 1 0 0 1 1 0 26H Port for OCW2 and OCW3
There are three operation command words: OCW1, OCW2, 0 0 1 1 0 1 1 1 27H Port for OCW1
and OCW3.
With the help of OCWs, a programmer can dynamically
change the priority associated with each of IR0 - IR7, or There are three registers inside the 8259.
mask any of them. Examples in subsequent slides show • ISR (in-service register),
how the OCWs are sent to the 8259. • IRR (interrupt request register),
• IMR (interrupt mask register).
* Refer to slide 37
61 62
OCW1
OCW1 OCW1 is used to mask any of IR0 - IR7.
Logic 1 is for masking (disabling) and 0 is for unmasking
(enabling).
For example, 11111000 is the OCW1 to enable (unmask) IR0, IR1,
IR2, and disable/mask the rest (IR3 - IR7).
When this byte is written to the 8259 (by making A0 =1
and CS =low), it goes into the internal register called IMR
(Interrupt Mask Register).
To see which IRs are enabled and which ones are
disabled.
Read OCW1, which is the contents of IMR. For example, to read
OCW1 using the port addresses in slide 64, code "IN AL,27H".
By examining the contents of AL, one can find out which IRs are
enabled and which ones are disable
The MPU address to access OCW1 is an odd address
65 66
71 72
Solution (1)
A0 is not used – don’t care – assume 0
Two I/O addresses are FF00h (A1=0) and FF02h (A1=1)
FF00h: ICW1, FF02h: ICW2, ICW3, ICW4, OCW1
Only one 8259 and interrupt input is an edge
ICW1 = 00010011b = 13h
Interrupt type 72 is used presented to IR0
ICW2 = 01001000b = 48h
ICW3 not needed
Non-buffered and auto EOI
ICW4 = 03h
For OCWs, use only OCW1 - mask all other interrupts
but IR0
OCW1 = 11111110b = FEh
Software organization Flowcharts
73 74
77 78
Solution (4)
; initialization
; service routine
MOV DX, 0FF00h
SRV72: PUSH AX
MOV AL, 13h
MOV AL, [COUNT]
OUT DX, AL
INC AL
MOV DX, 0FF02h
DAA
MOV AL, 48h
MOV [COUNT], AL
OUT DX, AL
POP AX
MOV AL, 03h
IRET
OUT DX, AL
MOV AL, 0FEh
OUT DX, AL
STI
; wait for interrupt
HERE: JMP HERE
79