02
02
02
Cs609@vu.edu.pk
Lecture #2
Interrupt Mechanism
Interrupt follow a follow a certain mechanism for their invocation just like near or far
procedures. To understand this mechanism we need to understand its differences with
procedure calls.
The general concept for procedure call in most of the programming languages is that on
invocation of the procedure the parameter list and the return address (which is the value if
IP register in case of near or the value of CS and IP registers in case of far procedure) is
pushed Moreover in various programming languages whenever a procedure is called its
address need to be specified by some notation i.e. in C language the name of the
procedure is specified to call a procedure which effectively can be used as its address.
However in case of interrupts the a number is used to specify the interrupt number in the
call
• Int 21h
• Int 10h
• Int3
Fig 1 (Call to interrupt service routine and procedures/functions)
Main
Call proc1()
Call proc1()
Int 21h
Proc1()
Int 10h
Proc2()
Moreover when an interrupt is invoked three registers are pushed as the return address i.e.
the values of IP, CS and Flags in the described order which are restored on return. Also
no parameters are pushed onto the stack on invocation parameters can only be passed
through registers.
INTFF 0000:03FFH
Moreover it is important to understand the meaning of the four bytes within the interrupt
vector. Each entry within the IVT contain a far address the first two bytes (lower word) of
which is the offset and the next two bytes (higher word) is the segment address.
0000:0007
IO.SYS
Device Driver
Command. COM
USER PROGRAM
This fact can be practically analyzed by the DOS command mem/d which gives the status
of the memory and also points out which memory area occupied by which process as
shown in the text below. The information given by this command indicates the address
where IO.SYS and other device drivers have been loaded but the location of ROM BIOS
is not shown by this command.
C:\>mem /d
Address Name Size Type
------- -------- ------ ------
000000 000400 Interrupt Vector
000400 000100 ROM Communication Area
000500 000200 DOS Communication Area
Interrupt Invocation
Although hardware and software interrupts are invoked differently i.e hardware interrupts
are invoked by means of some hardware whereas software interrupts are invoked by
means of software instruction or statement but no matter how an interrupt has been
invoked processor follows a certain set steps after invocation of interrupts in exactly
same way in both the cases. These steps are listed as below
This can be analyzed practically by the use of debug program, used to debug assembly
language code, by assembling and debugging INT instructions
C:\>debug
-d 0:84
0000:0080 7C 10 A7 00-4F 03 55 05 8A 03 55 05 |...O.U...U.
0000:0090 17 03 55 05 86 10 A7 00-90 10 A7 00 9A 10 A7 00 ..U.............
0000:00A0 B8 10 A7 00 54 02 70 00-F2 04 74 CC B8 10 A7 00 ....T.p...t.....
0000:00B0 B8 10 A7 00 B8 10 A7 00-40 01 21 04 50 09 AB D4 ........@.!.P...
0000:00C0 EA AE 10 A7 00 E8 00 F0-B8 10 A7 00 C4 23 02 C9 .............#..
0000:00D0 B8 10 A7 00 B8 10 A7 00-B8 10 A7 00 B8 10 A7 00 ................7
0000:00E0 B8 10 A7 00 B8 10 A7 00-B8 10 A7 00 B8 10 A7 00 ................
0000:00F0 B8 10 A7 00 B8 10 A7 00-B8 10 A7 00 B8 10 A7 00 ................
0000:0100 8A 04 10 02 ....
-a
0AF1:0100 int 21
0AF1:0102
-r
AX=0000 BX=0000 CX=0000 DX=0000 SP=FFEE BP=0000 SI=0000 DI=0000
DS=0AF1 ES=0AF1 SS=0AF1 CS=0AF1 IP=0100 NV UP EI PL NZ NA PO NC
0AF1:0100 CD21 INT 21
The dump at the address 0000:0084 H shows the value of the vector of the interrupt #
21H i.e. 21H * 4 = 84H. This address holds the value 107CH in lower word and 00A7H
in the higher word which indicates that the segment address of interrupt # 21 is 00A7H
and the offset address of this ISR is 107CH.
Moreover the instruction INT 21H can be assembled and executed in the debug program,
on doing exactly so the instruction is traced through and the result is monitored. It can be
seen that on execution of this instruction the value of IP is changed to 107CH and the
value of CS is changed to 00A7H which cause the execution to branch to the Interrupt #
21H in memory and the previous values of flags, CS and IP registers are temporarily
saved onto the stack as the value of SP is reduced by 6 and the dump at the location
SS:SP will show these saved values as well.
as 21H/09H in short. It is used to print a string ending by a ‘$’ character and other
parameters describing the string are as below
Inputs
AH = 0x09
DS = Segment Address of string
DX = Offset Address of string
Output
The ‘$’ terminated string at the address DS:DX is displayed
One thing is note worthy that the service # is placed in AH which is common with almost
all the interrupts and its service. Also this service is not returning any siginificant data, if
some service needs to return some data it too is received in registers depending upon the
particular interrupt.
Example:
#include<stdio.h>
#include<BIOS.H>
#include<DOS.H>
#include<conio.h>
void main()
{
clrscr(); //to clear the screen contents
_DX = (unsigned int) st;
_AH = 0x09;
geninterrupt(0x21);
getch(); //waits for the user to press any key
}
this is a simple example in which the parameters of int 21H/09H are loaded and then int
21H is invoked. DX and AH registers are accessed through pseudo variables and then
geninterrupt()is called to invoke the ISR. Also note that _DS is not loaded. This is
the case as the string to be loaded is of global scope and the C language compiler
automatically loads the segment address of the global data into the DS register.
struct full
{
unsigned int ax;
unsigned int bx;
unsigned int cx;
unsigned int dx;
};
struct half
{
unsigned char al;
unsigned char ah;
unsigned char bl;
unsigned char bh;
unsigned char cl;
unsigned char ch;
unsigned char dl;
unsigned char dh;
};
typedef union tagREGS
{
struct full x;
struct half h;
}REGS;
This union can be used to signify any of the full or half general purpose register shows if
the field ax in x struct is to be accessed then accessing the fields al and ah in h will also
have the same effect as show in the example below.
Example:
#include<DOS.H>
output:
9955
contains the value of parameters that should be passed as inputs, and third parameter is a
reference to a REGS union which will contain the value of registers returned by this
function. All the required parameters for an ISR are placed in REGS type of union and its
reference is passed to an int86() function. This function will put the value in this union
into the respective register and then invoke the interrupt. As the ISR returns it might leave
some meaningful value in the register (ISR will return values), these values can be
retrieved from the REGS union whose reference was passed into the function as the third
parameter.
Example using interrupt # 21H service # 42H
To make it more meaningful we can again elaborate it by means of an example. Here we
make use of ISR 21H/42H which is used to move the file pointer. Its detail is as follows
BOF
BOF cp
cp EOF
EOF
------------------- --------------------
This service is used to move the file pointer to a certain position relative to a certain
point. The value in AL specify the point relative to which the pointer is moved. If the
value of AL = 0 then file pointer is moved relative to the BOF (begin of File) if AL=1
then its moved relative to current position and if AL = 2 then its moved relative to the
EOF (end of file).
CX-DX specify the number of bytes to move a double word is needed to specify this
value as the size of file in DOS can be up to 2 GB.
On return of the service DX-AX will contain the number of bytes the file pointer is
actually moved eg. If the file pointer is moved relative to the EOF zero bytes the DX-AX
on return will contain the size of file if the file pointer was at BOF before calling the
service.