You are on page 1of 8

System Programming Course Code: CS609

Cs609@vu.edu.pk
Lecture # 4
Another Example:
#include<BIOS.H>
#include<DOS.H>
char st[80] ={"Hello orld!""#
char st$[80] ={"Hello Students%!""#
&oid interru't ()oldint*+,( ,#
&oid interru't ne-int*+( ,#
&oid .ain(,
{
oldint*+ = /et&ect(00*+,#
set&ect(00*+1 ne-int*+,#
2ee'(01 $000,#
"
&oid interru't ne-int*+( ,
{
i3 (( 45H , == 0, 66corrected
{
45H = 0007#
4D8 = (unsi/ned int, st#
/eninterru't (009$,#
"
else
{
i3 (( 45H , == $, 66corrected
{
45H = 0007#
4D8 = (unsi/ned int, st$#
/eninterru't (009$,#
"
"
"
arious interrupts provide a num!er o" servi#es. $he servi#e num!er is usually pla#ed in
the A% register !e"ore invoking the interrupt. $he &S' should in turn #he#k the value in
A% register and then per"orm the "un#tion a##ordingly. $he a!ove example exempli"ies
(ust that. &n this example int 6) is assigned t*o servi#es 0 and +. Servi#e 0 prints the
string st and servi#e + prints the string st$. $hese servi#es #an !e invoked in the
"ollo*ing manner.
#include<BIOS.H>
#include<DOS.H>
&oid .ain(,
{
irtual ,niversity o" Pakistan
-)
System Programming Course Code: CS609
Cs609@vu.edu.pk
45H = $#
/eninterru't (00*+,#
45H = 0#
/eninterru't (00*+,#
"
&nterrupt stealing or interrupt hooks
Previously *e have dis#ussed ho* a ne* interrupt #an !e *ritten and implemented.
&nterrupt stealing is a te#hni.ue !y *hi#h already implemented servi#es #an !e altered !y
the programmer.
$his te#hni.ue makes use o" the "a#t that the ve#tor is stored in the &$ and it #an !e read
and *ritten. $he interrupt *hi#h is to !e hooked its /original routine 0 ve#tor is "irst read
"rom the &$ and then stored in a interrupt pointer type varia!le1 a"ter this the ve#tor is
#hanged to point to one o" the interrupt "un#tion /ne* routine0 *ithin the program. &" the
interrupt is invoked no* it *ill "or#e the ne* routine to !e exe#uted provided that its
memory resident. 2o* t*o things #an !e done1 the original routine might !e per"orming
an important task so it also needs to invoked1 it #an either !e invoked in the start o" the
ne* routine or at the end o" the ne* routine using its pointer as sho*n in the "ollo*ing
exe#ution #harts !elo*
3ig + /2ormal Exe#ution o" an &S'0
Exe#ution &nterrupted
&S' Per"orm &45
2ormal Exe#ution o" &nterrupt
irtual ,niversity o" Pakistan
-6
System Programming Course Code: CS609
Cs609@vu.edu.pk
3ig - /$he original &S' !eing #alled at he end o" ne* routine0
&nterrupt &nter#eption
2e* 'outine
5riginal 'outine
3ig 6 /$he original &S' invoked at the start o" ne* &S'0
5ther "orm o" &nterrupt &nter#eption
2e* 'outine
5riginal 'outine
Care must !e taken *hile invoking the original interrupt. 7enerally in #ase hard*are
interrupts are inter#epted invoking the original interrupt at the start o" ne* routine might
#ause some pro!lems *hereas in #ase o" so"t*are interrupts the original interrupt #an !e
invoked any*here.
Sample Program "or interrupt &nter#eption
&oid interru't ne-int(,#
&oid interru't ()old,(,#
&oid .ain(,
{
old=/et&ect(0008,#
set&ect(00081ne-int,#
2ee'(01$000,#
"
irtual ,niversity o" Pakistan
-8
System Programming Course Code: CS609
Cs609@vu.edu.pk
&oid interru't ne-int (,
{
:
:
()old,(,#
"
$he a!ove program gets the address stored at the ve#tor o" interrupt 9 and stores it in the
pointer oldint. $he address o" the interrupt "un#tion ne*int is then pla#ed at the ve#tor o"
int 9 and the program is made memory resident. 3rom this point on*ards *henever
interrupt 9 o##urs the interrupt "un#tion ne*int is invoked. $his "un#tion a"ter per"orming
its operation #alls the original interrupt 9 *hose address has !een stored in oldint pointer.
$imer &nterrupt
&n the #oming "e* examples *e *ill inter#ept interrupt 9. $his is the timer interrupt. $he
timer interrupt has "ollo*ing properties.
&ts an %ard*are &nterrupts
&t is &nvoked !y :eans o" %ard*are
&t approximately o##urs +9.- times every se#ond !y means o" hard*are.
;&5S <ata Area
;&5S #ontains trivial &45 routines *hi#h have !een programmed into a '5: type devi#e
and is inter"a#ed *ith the pro#essor as a part o" main memory. %o*ever the ;&5S
routines *ould re.uire a "e* varia!les1 these varia!les are stored in the ;&5S data arera
at the lo#ation 00=0:0000% in the main memory.
5ne su#h !yte stored in the ;&5S data area is the key!oard status !yte at the lo#ation
=0:+8%. $his #ontains the status o" various keys like alt1 shi"t1 #aps lo#k et#. $his !yte
#an !e des#ri!ed !y the diagram !elo*
3ig = />ey!oard status !yte0
0 1 2 3 4 5 6 7 0 1 2 3 4 5 6 7
Right Shift key
Left Shift Key
Ctrl Key
Alt Key
Right Shift key
Left Shift Key
Ctrl Key
Alt Key
Insert key
Caps Lock Key
Num Lock key
Scroll lock key
Insert key
Caps Lock Key
Num Lock key
Scroll lock key
40:!"
Key#oar$ Status %or$
irtual ,niversity o" Pakistan
-9
System Programming Course Code: CS609
Cs609@vu.edu.pk
Another Example
#include <dos.h>
&oid interru't ()old,(,#
&oid interru't ne-(,#
char 3ar )scr=(char 3ar) , 0000;000$<#
&oid .ain(,
{
old=/et&ect(0008,#
set&ect(00081ne-,#
2ee'(01$000,#
"
&oid interru't ne- (,{
)scr=*;#
()old,(,#
"
$his "airly simple example inter#epts the timer interrupt su#h that *henever the timer
interrupt o##urs the "un#tion ne*/0 is invoked. 'emem!er this is .C program and not a
.CPP program. Save the #ode "ile *ith .C extension a"ter *riting this #ode. 5n o##urren#e
o" interrupt 9 the "un#tion ne* sets the #aps lo#k !it in key !oard status !y pla#ing 6= at
this position through its "ar pointer. So even i" the user turns o" the #aps lo#k on the next
o##urren#e o" int 9 / almost immediately0 the #aps lo#k *ill !e turned on again /turing on
the #aps lo#k on like this *ill not e""e#t its ?E< in the key!oard only letters *ill !e typed
in #aps0.
:emory :apped &45 and &solated &45
A devi#e may !e inter"a#ed *ith the pro#essor to per"orm memory mapped or isolated
&45. :ain memory and &45 ports !oth are physi#ally a kind o" memory devi#e. &n #ase o"
&solated &451 &45 ports are used to hold data temporary *hile sending4re#eiving the data
to4"rom the &45 devi#e. &" the similar "un#tion is per"ormed using a dedi#ated part o" main
memory then the &45 operation is memory mapped.
irtual ,niversity o" Pakistan
-9
System Programming Course Code: CS609
Cs609@vu.edu.pk
3ig ) /&solated &450
Isolate$ I&'
:
P
&45
IN
'()
:
P
&45
IN
'()
3ig 6 /:emory mapped &450
*emory *appe$ I&'
:
P
&45
*'+
*'+
:emory :apped &45 on :onitor
5ne o" the devi#es in standard PCs that per"orm memory mapped &45 is the display
devi#e /:onitor0. $he output on the monitor is #ontroller !y a #ontroller #alled video
#ontroller *ithin the PC. 5ne o" the reason "or adopting memory mapped &45 "or the
monitor is that a large amount o" data is needed to !e #onveyed to the video #ontroller in
order to des#ri!e the text or that graphi#s that is to !e displayed. Su#h large amount o"
data !eing output through isolated &45 does not "orm into a "easi!le idea as the num!er o"
port in PCs is limited to 6))66.
$he memory area starting "rom the address !900:0000%. $*o !ytes /a *ord0 are reserved
"or a single #hara#ter to !e displayed in this area. $he lo* !yte #ontains the ASC&& #ode
o" the #hara#ter to !e displayed and the high !yte #ontains the attri!ute o" the #hara#ter to
!e displayed. $he address !900:0000h #orresponds to the #hara#ter displayed at the top
le"t #orner o" the s#reen1 the next *ord !900:000- #orresponds to the next #hara#ter on
the same ro* o" the text s#reen and so on as des#ri!ed &n the diagram !elo*.
irtual ,niversity o" Pakistan
60
System Programming Course Code: CS609
Cs609@vu.edu.pk
3ig 8 /:emory mapped &45 on monitor0
*emory *appe$ I&' 'N *onitor
;955:0000
;955:000+
;955:000-
;955:0006
?o* ;yte @ ASC&& C5<E
%igh ;yte @Attri!ute ;yte
$he attri!ute !yte /higher !yte0 des#ri!es the "ore#olor and the !a#k#olor in *hi#h the
#hara#ter *ill !e displayed. $he <5S s#reen #arries !la#k as the !a#k#olor and *hite as
the "ore #olor !y de"ault. $he lo*er = !its /lo*er ni!!le0 represents the "ore#olor and the
higher = !its /higher ni!!le0 represents the !a#k #olor as des#ri!ed !y the diagram !elo*
3ig 9 /Attri!ute ;yte0
*emory *appe$ I&' 'N *onitor
X X X X X X X X X X X X X X X X
;a#k Color
Color
;old
;link
Lo, -yte . Ascii Co$e
"igh -yte . Attri#ute -yte
White 111
Blue 001
Green 010
Red 100
Black 000
White 111
Blue 001
Green 010
Red 100
Black 000
"ore #olor
$o understand all des#ri!e a!ove lets take a look at this example.
unsi/ned int 3ar )scr=00=8000000#
&oid .ain(,
{
()scr,=000<+*#
()(scr>$,,=00<0++#
"
irtual ,niversity o" Pakistan
6+
System Programming Course Code: CS609
Cs609@vu.edu.pk
$his example *ill generate the output ?@
$he "ar pointer s#r is assigned the value 0x!900% in the high *ord *hi#h is the segment
address and value 0x0000% in the lo* *ord *hi#h is the o""set address. $he *ord at this
address is loaded *ith the value 0x08)6% and the next *ord is loaded !y the value
0x80))%1 0x08 is the attri!ute !yte meaning !la#k !a#k #olor and *hite "ore #olor and
the !yte 0x80h means *hite !a#k #olor and !la#k "ore #olor. 0.0x)6 and 0x)) are the
ASC&& value o" AB and A,B respe#tively.
irtual ,niversity o" Pakistan
6-

You might also like