3 Interrupts and Interrupt Service Routines: 3.1 What Is An Interrupt?
3 Interrupts and Interrupt Service Routines: 3.1 What Is An Interrupt?
3 Interrupts and Interrupt Service Routines: 3.1 What Is An Interrupt?
Suppose you are reading a novel and have completed up to page 100. At this instant,
your younger brother asks you to solve his difficulty. You will somehow mark the
line and the page you are reading, so that you may be able to continue after you solve
his difficulty. Say you have marked page number 101. You will now go to his room
to solve his difficulty. While you are helping him a friend of yours comes and asks
you for a textbook. Now, there are two options in front of you. The first is to make
the friend wait till you complete serving your brother, and thereafter you serve his
request. In this, you are giving less priority to your friend. The second option is to ask
your brother to wait; remember the solution of the difficulty at the intermediate state;
serve the friend; and after the friend is served, continue with the solution that was in
the intermediate state. In this case, it may be said that you have given higher priority
to your friend. After serving both of them, again you may continue reading from page
101 of the novel. Here, first you are interrupted by your brother. While you are
serving your brother, you are again interrupted by a friend. This type of sequence of
appearance of interrupts is called nested interrupt, i.e. interrupt within interrupt.
Whenever a number of devices interrupt a CPU at a time, and if the processor is able
to handle them properly, it is said to have multiple interrupt processing capability.
For example, 8085 has five hardware interrupt pins and it is able to handle the
interrupts simultaneously under the control of software. In case of 8086, there are two
interrupt pins, viz. NMI and INTR. The NMI is a nonmaskable interrupt input pin
which means that any interrupt request at NMI input can not be masked or disabled
by any means. The INTR interrupt, however, may be masked using the interrupt flag
(IF). The INTR, further, is of 256 types. The INTR types may be from 00 to FFH (or
00 to 255). If more than one type of INTR interrupt occurs at a time, then an external
chip called programmable interrupt controller is required to handle them. The same is
the case for INTR interrupt input of 8085. Interrupt Service Routines (ISRs) are the
programs to be executed by interrupting the main program execution of the CPU,
after an interrupt request appears. After the execution of ISR, the main program
continues its execution further from the point at which it was interrupted.
1
3.2 Interrupts Sources in 8086
Broadly, there are two sources of interrupts. The first out of them is external
(hardware) interrupt and the second is internal (software) interrupts. The external
interrupt occurs when an external device or a signal interrupts the processor from
outside or, in other words, the interrupt is generated outside the processor, for
example, a keyboard interrupt. The internal interrupt, on the other hand, is generated
internally by the processor circuit, or by the execution of an interrupt instruction. The
examples of this type are divide by zero interrupt, overflow interrupt, interrupts due
to INT instructions, etc.
Figure 3.1 shows how the 256 interrupt pointers are arranged in the memory table.
Each double word interrupt pointer is identified by a number from 0 to 255. Intel
calls this number the type of the interrupt. The lowest five types are dedicated to
specific interrupts such as the divide by zero interrupt and the nonmaskable interrupt
which we will explain in details later. The next 27 interrupt types, from 5 to 31, are
reserved by Intel for use in future microprocessors. The upper 224 interrupt types,
from 32 to 255, are available for you to use for hardware or software interrupts.
For obtaining an interrupt address vector, the 8086 uses the two addresses in the
pointer table where IP and CS are stored for a particular interrupt type.
For example, for the interrupt type nn (instruction INT nn), the table address
for IP = 4 * nn and the table address for CS = (4 * nn) + 2. For servicing the
8086’s nonmaskable interrupt (NMI pin), the 8086 assigns the type code 2 to this
interrupt. The 8086 automatically execute the INT2 instruction internally to
obtain the interrupt address vector as follows:
2
Address for IP = 4 * 2 = 00008H
The 8086 loads the values of IP and CS from the 20-bit physical addresses 00008H
and 0000AF in the pointer table. The user must store the desired 16-bit values of IP
and CS in these locations. Similarly, the IP and CS values for other interrupts are
calculated.
3
The NMI pin should remain high for at least two clock cycles and is not needed to be
synchronized with the clock for being sensed. When NMI is activated, the current
instruction being executed is completed, and then the NMI is served. Another high
going edge on the NMI pin of 8086, during the period, in which the first NMI is
served, triggers another response. The signal on the NMI pin must be free of logical
bounces to avoid erratic NMI responses.
All software interrupts are nonmaskable interrupts since the processor is going to
respond to such interrupts whatever was the status of the Interrupt flag (IF).
In the other hand, maskable interrupts can be sourced to the processor 8086/88
through the pin INTR. This pin can source various interrupt types. The priorities,
within the INTR types, are decided by the type of the INTR signal that is to be passed
to the processor via data bus by some external device like the programmable interrupt
controller. The INTR signal is level triggered and can be masked by resetting the
interrupt flag. It is internally synchronized with the high transition of CLK. For the
INTR signal, to be responded to in the next instruction cycle, it must go high in the
last clock cycle of the current instruction or before that, The INTR requests appearing
after the last clock cycle of the current instruction will be responded to after the
execution of the next, instruction. The status of the pending interrupts is checked at
the end of each instruction cycle.
If the IF is reset, the processor will not serve any interrupt appearing at this pin. If the
IF is set, the processor is ready to respond to any INTR interrupt. INTA is only
generated by the 8086 in response to INTR signal, telling the external device that it is
ready to accept the interrupt and asking for interrupt type to be sent on the lower data
bus D7-D0. Once the processor responds to an INTR signal, the IF is automatically
reset. If one wants the processor to further respond to any type of INTR signal, the IF
should again be set.
A priority interrupt controller such as the 8259A can be used with the 8086 INTR to
produce eight levels of hardware interrupts. The 8259A has built-in features for
expansion of up to 64 levels with additional 8259As. The 8259A is programmable
and can be readily used with 8086 to obtain multiple interrupts from the single 8086
INTR pin.
4
these interrupts through hardware or software. If a predefined interrupt is not used in
a system, the user may assign some other function to the associated type.
Once TF is set to one, the 8086 automatically generates a TYPE 1 interrupt after
execution of each instruction. The user can write a service routine at the interrupt
address vector to display memory locations and/or register to debug a program.
Single step is nonmaskable and cannot be enabled by STI (enable interrupt) or CLI
(disable interrupt) instruction.
The nonmaskable interrupt is initiated via the 8086 NMI pin. It is edge triggered
(LOW to HIGH) and must be active for two clock cycles to guarantee recognition. It
is normally used for catastrophic failures such as power failure. The 8086 obtains the
interrupt vector address by automatically executing the INT2 (type 2) instruction
internally.
Type 3 interrupt is used for breakpoint and is nonmaskable. The user inserts the one-
instruction INT3 into a program by replacing an instruction. Breakpoints are useful
program debugging.
5
Interrupt is requested
Internal (software)
Source?
External (hardware)
Complete the current
instruction
Maskable (INTR)
Source?
Nonaskable (NMI) =0
IF?
=1
Ignore Interrupt
Clear IF, TF
Execute ISR
6
controller in case of external interrupts. The contents of IP and CS are next pushed to
the stack. The contents of IP and CS now point to the address of the next instruction
of the main program from which the execution is to be continued after executing the
ISR. The flag register is also pushed to the stack. The interrupt flag (IF) is cleared.
The TF is also cleared. The new address of ISR is found out from the interrupt vector
table. The execution of the ISR starts. If further interrupts are to be responded to
during the time the first interrupt is being serviced, the IF should again be set to l by
the ISR of the first interrupt. If the interrupt flag is not set, the subsequent interrupt
signals will not be acknowledged by the processor, till the current one is completed.
The programmable interrupt controller is used for managing such multiple interrupts
based on their priorities. At the end of ISR the last instruction should be IRET. When
the CPU executes IRET, the contents of flags, IP and CS which were saved at the
start by the CALL instruction are now retrieved to the respective registers. The
execution continues onwards from this address, received by IP and CS. Fig. 3.2
shows how 8086 response to an interrupt request.
As a second example of how this priority works, suppose that a rising-edge signal
arrives at the NMI input while the 8086 is executing a DIV instruction, and that the
division operation produces a divide error. Since the 8086 checks for internal
interrupts before it checks for an NMI interrupt, the 8086 will push the flags on the
stack, clear TF and IF, push the return address on the stack, and go to the start of the
divide error (type 0) service procedure. However, because the NMI interrupt request
is not disabled, the 8086 will then do an NMI (type 2) interrupt response. In other
7
Interrupt Priority
DIVIDE ERROR, INT n, INTO HIGHEST
NMI
INTR
SINGLE-STIP LOWEST
Table 3.1: Priority of 8086 interrupts.
words, the 8086 will push the flags on the stack, clear TF and IF, push the return
address on the stack, and go execute the interrupt service procedure. When the 8086
finishes NMI procedure, it will return to the divide error procedure, finish executing
that procedure, and then return to the mainline program.
To finish our discussion of 8086 interrupt priorities let’s see how the single step
TRAP (or type 1) interrupt fits in. If the trap flag is set, the 8086 will do a type 1
interrupt response after every mainline instruction. When the 8086 responds to any
interrupt, however, part of its response is to clear the trap flag. This disables single-
step function, so the 8086 will not normally single-step through the instructions of the
intend service procedure. In actuality, If the 8086 is in single step mode, when it
enters an interrupt service procedure, it will execute the single-step procedure once
before it executes the called interrupt procedure. The trap flag can be set again in the
single-step procedure if single stepping is desired in the interrupt service procedure.
8
Fig. 3.4: Transfer of Control during Execution of an Interrupt Service Routine
Program 3.1 Write a program to Create a file RESULT and store in it 500H bytes
from the memory block starting at 1 000:1 000, if either an interrupt appears at INTR
pin with Type 0AH or an instruction equivalent to the above interrupt is executed.
9
FURTHER INT 0AH ; If the file is created successfully,
MOV AH, 4CH ; write intc it and return
INT 21H ;to DOS prompt.
;This interrupt service routine
;writes 500 bytes into the file
;RESULT and returns to the main
;program.
To execute the above program, first assemble it using MASM.EXE, link it using
LINK.EXE. Then execute the above program at DOS prompt. After execution, you
will find a new file RESULT in the directory. Then apply an external pulse to IRQ2
pin of the IBM PC 10 channel. This will again cause the execution of ISR that writes
500 H bytes into the file. For further details of the DOS function calls under INT
21H, refer the MSDOS Encyclopedia or IVIS-DOS Technical Reference.
Program 4.4 Write a program that gives display ‘IRT2 is OK’ if a hardware signal
appears on IRQ2 pin and ‘IRT3 is OK’ if it appears on IRQ3 pin of PC 10 Channel.
DATA SEGMENT
MSG1 DB "IRT2 IS OK", 0AH,
0DH,"$"
MSG2 DB "IRT3 IS OK", 0AH,
0DH,"$"
DATA ENDS
CODE SEGMENT
START: MOV AX, CODE
10
MOV DS, AX ;Set IVT for Type 0AH
MOV DX, OFFSET ISR1
MOV AX, 250AH ; IRQ2 is equivalent to Type 0AH.
INT 21H
MOV DX, OFFSET ISR2 ; Set IVT for Type 0BH.
MOV AX, 250BH ;IRQ3 is equivalent to TYPE 0BH.
INT 21H
HERE: JMP HERE
CODE ENDS
END START
Prepare the EXE file of the above program as usual. Execute it at DOS prompt that
will hang the system. Now apply a pulse to IRQ9 pin. The message ‘IRT2 is OK’ is
displayed on the screen. Then apply a pulse to IRQ pin of 10 channel. The message
‘IRT3 is OK’ is displayed on screen.
11