You are on page 1of 23

AAiT

Lecture 07

Interrupts & Interrupt Service


Routines (ISRs)

ECEg - 4501 Daniel D. DECE


Issues AAiT

 Interrupt Cycles

 Interrupt Types

 Interrupt Programming

 More programming exercises

ECEg - 4501 1
Basics
AAiT

While the CPU is executing a program, an 'interrupt' breaks the normal


sequence of execution of instructions, diverts its execution to some
other program called Interrupt Service Routine (ISR).

After executing ISR, the control is transferred back again to the main
program which was being executed at the time of interruption.

If more than one type of INTR interrupts occurs at a time, then an


external chip called programmable interrupt controller (PIC) is required
to handle them.

ECEg - 4501 2
Interrupt Cycle AAiT

Broadly, there are two types of interrupts.


External and internal interrupts.

At the end of each instruction cycle, the 8086 checks to


see if any interrupts have been requested.

Suppose an external device interrupts the CPU at the


interrupt pin, either NMI or INTR of 8086, while the CPU
is executing an instruction of a program.

 The following is the basic logic of execution

ECEg - 4501 3
Interrupt Cyclecntd
AAiT

 finish execution of the current instruction.


The IP is incremented to point to the next instruction.
Is it NMI, TRAP or divide by zero?
YES:

CPU acknowledges the requesting device immediately


NO:
CPU checks the IF flag (set or clear)
Clear: The interrupt request will be ignored
SET: The interrupt will be acknowledged

When the CPU acknowledges the interrupt, the


following series of routines will be performed.

ECEg - 4501 4
Interrupt Cyclecntd AAiT

It decrements the stack pointer by 2 and pushes the


flag register on the stack.

It disables the 8086 INTR interrupt input by clearing the


interrupt flag (IF) in the flag register.

It resets the trap flag (TF) in the flag register.

It decrements the stack pointer by 2 and pushes the


current code segment register contents on the stack.

It decrements the stack pointer again by 2 and pushes


the current instruction pointer contents on the stack.

ECEg - 4501 5
Interrupt Cyclecntd AAiT

It does an indirect far jump to the start of the


procedure you wrote to respond to the Interrupt.

An IRET instruction at the end of the interrupt service


procedure returns execution to the main program.

The next picture illustrates the above procedure


graphically.

ECEg - 4501 6
Interrupt Cyclecntd AAiT

Main line
Program ISR
PUSH FLAGS
CLEAR IF
CLEAR TF
PUSH CS
Interrupt PUSH IP
FETCH ISR ADDRESS

IRET

POP IP
POP CS
POP FLAGS

ECEg - 4501 7
Interrupt typescntd AAiT

Every external and internal interrupt is assigned with a


type (N)

The type is either implicit (in case of NMI, TRAP and


divide by zero) or specified in the instruction INT N (in
case of internal interrupts).
In the zeroth segment of physical address space,
i.e. CS = 0000, Intel has reserved 1024 locations for
storing the interrupt vector table.

ECEg - 4501 8
Interrupt typescntd AAiT

The 8086 supports a total of 256 types of interrupts,


from 00 to FFH.

Each interrupt requires 4 bytes:


Two bytes each for IP and CS of its ISR.
Thus a total of 1024 bytes are required for 256 interrupt
types, hence the interrupt vector table starts at location
0000:0000 and ends at 0000:03FFH.

The ff figure shows the interrupt vector table

ECEg - 4501 9
Interrupt typescntd AAiT

3FF

3FC
Available Interrupts
(224)

84

Reserved Interrupts
(27)

10
0C
Dedicated Interrupts
(5) 08
04
00
ECEg - 4501 10
Interrupt Priority AAiT

What happens if two or more interrupts occur at the


same time?

The highest-priority interrupt will be serviced first and


then the next-highest-priority interrupt will be serviced
and

ECEg - 4501 11
Interrupt Prioritycntd AAiT

Datasheet
Priority of 8086 interrupts. (Intel Corporation, 1978)

ECEg - 4501 12
example AAiT

This program:
reads a string form the key board and convert the
characters into upper case letters and display on the
screen.

Uses software interrupt type 21 to input characters from


the keyboard as well as to display the strings.

ah = 01, INT 21hchar. i/p from keyboard


Ah = 09, INT 21hString display

ECEg - 4501 13
example AAiT

;data segment
msg1 db 'Enter the String ',0Ah,0Dh,'$'
msg2 db 0ah,'The String in Caps is :, '$' stop:mov str[bx],'$'
str db 80 dup(0) mov dx, offset msg2
data ends mov ah,09h
up: mov ah,01
;code segment int 21h
int 21h
assume cs:code, ds:data mov dx,offset str
cmp al,0Dh
start: mov ax,data mov ah,09h
je stop
mov ds,ax int 21h
cmp al,60H
lea dx,msg1 mov ah,4ch
jc dwn
mov ah,09h int 21h
sub al,20H
int 21h code ends
dwn: mov [bx],al
mov bx,offset str ;end start
inc bx
mov str[bx],0ah
inc bx jmp up

ECEg - 4501 14
More on Assembly language
AAiT

Besides standard instruction mnemonics and operands an


assembly language program may contain:
Operators length, offset, segment
Directives .org, name, assume, db, .
Labels
Labels ..any combination of letters and/or nos.
Comments.anything after comma
Subroutinesa sub-program just like a function in HLL
Interrupt service routine
Macros?

Whats a macro?

ECEg - 4501 15
Macros AAiT

A macro is a shorthand for a sequence of other statements


Be they instructions, directives, even other macros
Syntax:
Name MACRO variable(s)
assembly statements
ENDM
e.g. a macro to convert ASCII to decimal integer:
atoi macro number
mov al, number
and al, 0Fh
mov number, al
endm
ECEg - 4501 16
Macros AAiT

Now in the program atoi x can be used instead of the


three instructions.
e.g.2 A macro that swaps two integer words
swap MACRO word1, word2
push ax
mov ax, word1
xchg ax, word2
mov word1, ax
pop ax
ENDM
Hence, swap x,y replaces these five instructions.

ECEg - 4501 17
Class exercise AAiT

1. Write a program that reads five numbers from the


keyboard and print them out in increasing order.

2. write the above program again using macros to read


and display characters form the I/O devices.

3. Write a program that finds the GCF of two integers

ECEg - 4501 18
Solution to the exercises
AAiT

1. arr db 5 dup(0) ;define array of 5 elements


mov cx, 5 ;5 times
mov ah, 01
lea bx, arr
input: int 21h ;input loop
mov [bx], al
inc bx
loop input

mov bx, 0 ;start from the 1st element of the array


mov ax, 4 ;max no of comparisons in internal loop
next: mov dx, bx ;outer loop (for each element of the array)
sub ax,bx ;loop 4 times for the 1st element, trice for
mov cx, al ;the second, twice for the third, etc.
Next slide
ECEg - 4501 19
Solution to the exercises
AAiT

1. cntd

each: inc dx ;inner loop ( with every other elements)


cmp arr[bx], arr[dx] ;a[bx] - a[dx]
jc done ;if a[0] is less (CF=1) do nothing
xchg arr[bx], arr[dx] ;else swap the numbers(smaller on top)
done: loop each ;do this al-bx times

cmp bx, 5 ;check if every element is compared


jz disp ;display the result if all done
jmp next ;perform the outer loop once more if not

disp: mov cx, 5 ;5 characters to display


lea bx, arr ;starting with the first
mov ah, 02
Next slide
ECEg - 4501 20
Solution to the exercises
AAiT

1. cntd
do: mov dx, [bx] ;element to be displayed in dx
int 21h ;display interrupt (with ah=2)
mov dx, , ;print comma b/n the numbers
int 21h
Inc bx ;point to the next element
loop do ;do this 5 times

More to do: Modify this prog. to take any length of array from the
user. Prompt the user for the length of the array and the elements
So the display looks like this:
Enter the length of the array: 4
Enter the elements of the array: 2 9 7 5
The array in increasing order is: 2, 5, 7, 9

ECEg - 4501 21
Solution to the exercises
AAiT

2. simply, define a macro that replaces the i/o functions


at the beginning of the program.

inp macro address ;macro definition (keep this at the
int 21h ;start of the program)
mov [adress], al
endm ;end macro definition
;now u can use inp adress in the input loop in the previous prog
;
Input: inp bx ;adress is in bx
inc bx
loop input
Same can be done for the o/p print function

ECEg - 4501 22

You might also like