Title: Lecture 5: Interrupts in Turbo C
1Lecture 5 Interrupts in Turbo C
- Concepts of Interrupts
- Programs for interrupts
- Keyboard
- Video
- Time
2Execution of an Instruction (review)
Fetch the instruction from the memory
Execution of an instruction is achieved by a
sequence of commands issues by the control unit.
Fetch the operands from the memory
Perform the operation
Update the program counter
no
Check interrupt
yes
Interrupt processing
Next Instruction
3Execution of a Program (review)
Fetch the instruction from the memory
Fetch the operands from the memory
LOAD b, R1
Perform the operation
Update the program counter
A program is a sequence of instructions.
no
Check interrupt
yes
Fetch the instruction from the memory
Fetch the operands from the memory
Perform the operation
ADD R1, 1
Update the program counter
a b 1
no
Check interrupt
yes
Fetch the instruction from the memory
Fetch the operands from the memory
STORE R1, a
Perform the operation
Update the program counter
no
Check interrupt
yes
...
4Interrupt
- Interrupt is a mechanism for diverting the
attention of a processor when a particular event
occurs, such as I/O device requests. - Interrupts cause a break in the normal
execution of a program.
5A Break in the Normal Execution of a Program
Fetch the instruction from the memory
Interrupt The execution of the program can be
temporarily stopped to allow a special piece of
software -- an interrupt service routine -- to
run. When the routine has finished, the
program resumes. efficient and responsive -
difficult to program
Fetch the operands from the memory
Perform the operation
Update the program counter
no
Check interrupt
yes
Fetch the instruction from the memory
Fetch the operands from the memory
Perform the operation
Interrupt Processing and Service
Update the program counter
Check interrupt
yes
Fetch the instruction from the memory
Fetch the operands from the memory
Perform the operation
Update the program counter
no
Check interrupt
yes
Interrupt processing
6Steps taken to process Interrupt
1. On receipt of the interrupt, the processor
after executing the current instruction, branches
to an interrupt processing routine. The routine
is commonly known as Interrupt Handler. 2. The
Interrupt Handler will save the current processor
data (registers, status register, PC) and
determine which device has interrupt the
processor (polling). 3. Execution then branches
to the so called Interrupt Service Routine
associated with the device (causing the
interrupt) and the processor executes data
transfer. 4. The interrupt system is enable so
that further interrupts may be recognized. 5. Ret
urn to that program the execution of which was
suspended after recognizing the interrupt.
7Interrupt Processing and Service
Disable interrupts
Save environment
Find out which device causes interrupt
Branch to specific interrupt service routine
ISR 1
ISR i
ISR n
Restore environment
Enable interrupts
8 Interrupt Vectors
- the segmented addresses that specify the
locations of interrupt handlers are called
interrupt vectors - an interrupt handler is a function/subroutine
that takes care of the interrupt. - There are 256 interrupt vectors stored in
interrupt vector table located at the beginning
of the memory - some are reserved and some can be used by users
9Types of Interrupt
Hardware Interrupts CPU Interrupts
Software Interrupts
10Hardware Interrupts
Hardware interrupts are generated by device
control and supervised by PIC (programmable
interrupt control) chip. Interrupt No.
Functions
- 0x02 NMI non-maskable interrupt, memory parity
- 0x08 timer (18 per second)
- 0x09 keyboard
- 0x0A interrupt from controller 2
- 0x0B, 0x0C serial port2,1
- 0x0D parallel port 2
- 0x0E diskette
- 0x0F parallel port 1 (for printer)
Here, NMI has top priority and is serviced
immediately, and it cannot be turned off. It
occurs due to some significant error, such as
power failure. HERE 0x stands for hexadecimal
notations 0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F, where
A stands for ten, B eleven, C, twelve, D
thirteen, E fourteen, F fifteen. Therefore, 0x200
is 2x16x16 plus 0 which is 512 in decimal. For
decimal notations 2002x10x10.
11CPU Interrupts
CPU interrupts are generated in response to a
fatal program error, or for program flow control
- 0x00 division by zero
- 0x01 generated after every instruction when in
single-step mode - 0x02 generated when a program reaches a
breakpoint set by user - 0x04 arithmetic overflow
12Software Interrupts
Software interrupts are generated by using the
software interrupt instruction. In turbo C
include ltdos.hgt geninterrupt(n)
- BIOS interrupts provides the most direct, low
level interaction with the I/O devices and give a
device-independent interface which hides all the
details of the hardware architecture from
programmers and includes interrupt numbers 0x05,
0x10-0x1C, 0x48. - DOS interrupts are parts and the DOS operating
system, handle file and memory management and
executive services 0x20-0x60 - General use interrupts can be written by users
for their own service routines 0x61-0x67
Notice no need to remember all the interrupt
vectors. Focus on
13A Register
INPUT
OUTPUT
Q
D
CLOCK
D data input
Q output of data in the register
14Architecture of PC Registers
Scratch-pad Registers
AH BH CH DH
AL BL CL DL
- AX (accumulator)
- BX (base)
- CX (count)
- DX (data)
- CS (code segment)
- DS (data segment)
- SS (stack segment)
- ES (extra segment)
- IP (instruction pointer)
- SP (stack pointer)
- BP (base pointer)
- SI (source index)
- DI (destination index)
- Flags
0
7
0
7
Segment Registers
15
0
Offset Registers
15
0
CF
PF
AF
ZF
SF
TF
IF
DF
OF
15Access Registers in Turbo C
REGISTERS
PSUDO-VARIABLES in C
- AX _AX (_AH _AL)
- BX _BX (_BH, _BL)
- CX _CX (_CH, _CL)
- DX _DX (_DH, _DL)
- CS _CS
- DS _DS
- SS _SS
- ES _ES
- SP _SP
- BP _BP
- SI _SI
- DI _DI
- Flags _FLAGS
16The Flag Register in IBM PC
15
0
CF
PF
AF
ZF
SF
TF
IF
DF
OF
IF is the interrupt flag which controls whether
interrupts are enabled. IF1 The CPU will deal
with interrupt requests. IF0 The CPU will
ignore interrupt requests. CF Carry flag
Indicates an arithmetic carry OF Overflow
flag Indicates a signed arithmetic overflow ZF
Zero flag Indicates a zero result or
an equal comparison SF Sign flag
Indicates a negative result or comparison PF
Parity flag Indicates an even number of 1
bits AF Auxiliary carry flag . DF Direction
flag Controls increment direction in string
operastions TF Trap flag Controls
single-step operation (used by DEBUG).
17BIOS Keyboard Services
They are invoked with interrupt 0x16 with the
following Service numbers in the register AH (_AH
in Turbo C).
service functionality
- 0x00 Read Next Keyboard Character
- 0x01 Report Whether Character Ready
- 0x02 Get Shift Status
- 0x03 Set Typematic Rate and Delay
- 0x05 Keyboard Write
- 0x10 Extended Keyboard Read
- 0x11 Get Extended Keystroke Status
- 0x12 Get Extended Shift Status
Notice 1. Service numbers are parameters pass to
the subroutines/interrupt handlers 2. Try to
remember the meaning of service 0x00 and 0x01
for interrupt 0x16.
18Interrupt 0x16 Service 0x00
Service 0x00 reports the next keyboard input
character. If there is no character in the BIOS
keyboard buffer, it waits until one is ready.
The character is removed from the BIOS keyboard
buffer after it is read.
- The auxiliary byte returned in AH is either the
character ID for special characters or the
standard PC-keyboard scan code identifying the
pressed key.
- Each character is reported as a pair of bytes.
The main byte returned in AL is either 0 for
special characters, or an ASCII code for ASCII
characters.
To invoke this service to read a character into
a variable x in Turbo C, we do char x
_AH0x0 geninterrupt(0x16) x_AL
19An Example of Getting a Key from Keyboard
- char x int y
- _AH0x0
- geninterrupt(0x16)
- x_AL
- cout ltltThe key is ltlt x ltltendl
- declare x to be char type
- choose service 0x0 with _AH
- invoke interrupt 0x16
- get ASCII code of key from _AL
- print the key out
Remember to include dos.h in your file.
20A Function to get a key
- char get_key_number () //return type of the
function is char - char a
- _AH0x00 //service number 0x00
- geninterrupt(0x16) //interrupt 0x16
- a_AL //_AL is the key
- return tmp //return the value
-
Demo the program Akey.cpp in the lecture. 1.
Show the program 2. Run the program
21Interrupt 0x16 Service 0x01
Service 0x01 tests whether a keyboard input
character is ready. The zero flag (ZF) is used
as the signal 1 indicates no input is ready, 0
indicates a character is ready. In the latter
case, the character is not removed from the BIOS
keyboard buffer until it is read by service 0x00.
To invoke this service in Turbo C, we do
char x _AH0x01 geninterrupt(0x16)
To test whether a character is ready after the
above steps in Turbo C, we do if
(_FLAGS0x4064) to check whether ZF is 1.
22Use Keyboard BIOS to Read
- To write a program to read a char from BIOS
keyboard buffer, one may first use service 0x01
interrupt 0x16 to test whether there is a key
stored in the BIOS buffer, then use service 0x00
interrupt 0x16 to read it. - int ch
- _AH0x01
- geninterrupt(0x16)
- temp_FLAGS0x40
- /must put the data to temp, see slide 32 /
- if (temp0)
- _AH0
- geninterrupt(0x16)
- ch_AL
-
The difference is that the computer does not have
to wait for users to type it.
23Caution when using Pseudo-Variables
Pseudo-variables refer to CPU registers which are
used by other programs which may run at the same
time. One must assign values right before using
them and read values right after obtaining them,
especially when we program in C. Be careful about
the following
- A pseudo-variable has no address
- The values one place in pseudo-variables may NOT
be preserved for any length of time. - Values of pseudo-variables may not remain the
same across a function call. - Do not change values of _CS, _SS, _SP, nor _BP
since they are used by machine code produced by
Turbo C compiler.
24Write a function to get a character
- int key_ready() //return 1 if a key is ready, 0
otherwise - long int x
- _AH1 //service number 0x01
- geninterrupt(0x16) //interrupt 0x16
- x_FLAGS //get flag register
- if (x0x400) return 1 //if ZF0 a key is
ready - else return 0 //else no key
-
- char read_a_key() //return char if a key is
ready - if (key_ready())
- return get_key_number().x
- else return 0
25Shoot a bullet when press a key
includeltdos.hgt include ltgraphics.hgt includeltio
stream.hgt includeltconio.hgt void show(int i,float
h, float v) void erease(int i, float h, float
v) void main(void) int driver
DETECT,mode int i,j,i1,s1 int y1
initgraph(driver,mode,"D\\bc31\\bgi")
setcolor(WHITE) line(1,400,400,400)
for ( i 0 i lt 80 i ) show(i, 5.0,
9.0) y1 _AH0x01 geninterrupt(0x16)//
y_FLAGS0x40
if(y 0) s11i1i _AH0x00
geninterrupt(0x16) if (s11) show(i-i1, 10.0,
8.0) delay (300) erease(i, 5.0, 9.0) if
(s11) erease(i-i1, 10.0, 8.0)
closegraph() void show(int i, float h, float
v) int x, y xhi yvi-0.15ii
setcolor(RED) circle(400-x,400-y,2) voi
d erease(int i, float h, float v) int x, y
xhi yvi-0.15ii
setcolor(BLACK) circle(400-x,400-y,2)
26Video Interrupt Services(for general knowledge,
will not be tested)
Most of the useful video services are found in
the BIOS through interrupt 0x10. Some MS-DOS
video services are provided through interrupt
0x21. They are user programmed interrupts to
produce output to the video screen. Usually, one
puts the function/service number in the register
AH and then invokes the corresponding interrupt.
Very often there are some parameters for
these functions/services which are put in
register AL, BX, CX, or DX.
27BIOS Video Service Interrupt 0x10
service functionality
service functionality
- 0x0C write pixel
- 0x0D read pixel
- 0x0E write char in tty mode
- 0x0F get current video mode
- 0x10 color palette interface
- 0x11 char generator interface
- 0x12 alternate select
- 0x13 write character string
- 0x14/15 (PC convertible only)
- 0x1A read/wri. dsp. cmb. code
- 0x1B return functionality
- 0x1C save/restore video state
- 0x00 set video mode
- 0x01 set cursor size
- 0x02 set cursor position
- 0x03 read cursor position
- 0x04 read light-pen position
- 0x05 set active display page
- 0x06 scroll window up
- 0x07 scroll window down
- 0x08 read character/attribute
- 0x09 write character/attribute
- 0x0A write character
- 0x0B set 4-color palette
28Interrupt 0x10 Service 0x0E Write char in TTY
mode
.
- The service number 0x0E is put in register AH.
- The char to be written is put in AL. The display
page number is put in BH and the foreground color
is in BL. - The character is written at the cursor location,
and the cursor is advanced one position, wrapping
over to new line or scrolling the screen as
needed. - There are four characters that service 0x0E
reacts to according to their ASCII meaning 0x07
beep, 0x08 backspace, 0x0A linefeed, 0x0D
carriage return. All other characters are
displayed normally.
29Output Characters
Display character a
Feed a new line
- _AH0x0E
- _AL97
- _BH1
- geninterrupt(0x10)
- _AH0x0E
- _AL0x0A
- _BH1
- geninterrupt(0x10)
Back one space
Beep
- _AH0x0E
- _AL0x08
- _BH1
- geninterrupt(0x10)
- _AH0x0E
- _AL0x07
- _BH1
- geninterrupt(0x10)
30A Function for Output a Char
- void output_a_char(int x) //x is the ASCII code
of char - _AH0x0E
- _ALx
- _BH1
- geninterrupt(0x10)
Call the function to output characters
- output_a_char(97) //output A
- output_a_char(8) //backspace
- output_a_char(7) //output a ring
- output_a_char(0x0A) //a new line
31Another Function for Output a Char
- void output2_a_char(char x) //x is a char type
- int tmp
- tmpx //convert to ASCII code
- _AH0x0E //service number 0x0E
- _ALtmp //output tmp
- _BH1
- geninterrupt(0x10) //interrupt 0x10
Call the function to output characters
- output2_a_char(A) //output A
- output2_a_char(B) //output B
32Time Services (Clock)
- Interrupt 0x21 Service 0x2C
- CH contains the hours (0-23)
- CL contains the minutes (0-59)
- DH contains the seconds (0-59)
- DL contains hundredths of a second (0-99).
33Time Services (Clock)
- int i,j,k,l
- _AH0x2C //service 0x2C for get time
- interrupt(0x21) //interrupt 0x21
- i_CH j_CL k_DH l_DL
- coutltltiltlt Hoursltltjltlt Minutes ltltkltlt Seconds
ltltl
34Get the Time of Hitting a Key
Using the above functions written with
interrupts, we may obtain the approximate time
of hitting a key as follows int
i,j,k,l read_a_key() /call function
read_a_key() / _AH0x2C //service 0x2C for
get time interrupt(0x21) //interrupt
0x21 i_CH j_CL k_DH l_DL coutltltiltlt
Hoursltltjltlt Minutes ltltkltlt Seconds ltltl
35Arrays
- An array is a collection of two or more adjacent
memory cells, called array elements, that are
associated with a particular symbolic name.
To declare an array int x3 (We
declared an array with name x. It contains
3 elements. Each is of int
type.The index starts with 0)
x0 x1 x2
36Use an array in the program
include ltiostream.hgt void main(void) int
x10,i for (i0 ilt9 i)
cingtgtxi for (i0 ilt9 i) coutltlt
xi
The index of the elements can change. The index
of the first element of an array is 0. x0,
x1, x2, x3, x4 x5, x6, x7, x8,
x9
37Initialize an array
include ltiostream.hgt void main(void) int
x101,2,2,2,3,4,5,6,7,9 ,i for
(i0 ilt9 i) coutltlt xiltlt
38An array of characters
include ltiostream.hgt void main(void) char
x3a, b, c ,i for (i0
ilt9 i) coutltlt xi
39 Exercise
What does the following program output?
include ltiostream.hgt void main(void) int
x108, 7, 9, 10, 1, 3, 3, 8, 2, 5 , i, j
for (i0 ilt9 i)
for(j9 jgti j--) if (xigtxj)
yxi
xixj xjy
for(j0 jlt9 j) coutltlt xj
coutltlt\n
40Shoot bullets whenever press key plane
includeltdos.hgt include ltgraphics.hgt includeltco
nio.hgt void show(int i,float h, float v) void
erease(int i, float h, float v) void
planeshow(int i) void ereasep(int i) void
main(void) int driver DETECT,mode
int i,j,i1,s1,k int y1 int
a100 for(i0 ilt99 i)
ai0 initgraph(driver,mode,"D\\bc31\\bgi
") setcolor(WHITE)
line(1,400,400,400) j1 for ( i 0
i lt 80 i ) setcolor(BLUE)
planeshow(5i) setcolor(YELLOW)
planeshow(5(i-8)) show(i, 5.0, 9.0)
y1 _AH0x01 geninterrupt(0x16)//
y_FLAGS0x40 if(y 0) jj1aji _AH0x00
geninterrupt(0x16) for (k2 kltj k) if
(ak!0) show(i-ak, 10.00.5k, 8.00.1k)
delay (300) ereasep(5i) ereasep(5(i-8)) ere
ase(i, 5.0, 9.0) for (k2 kltj k) if
(ak!0) erease(i-ak, 10.00.5k, 8.00.1k)
closegraph() void show(int i,
float h, float v) int x, y xhi
yvi-0.15ii setcolor(RED)
circle(400-x,400-y,2)
void erease(int i, float h, float v) int x,
y xhi yvi-0.15ii
setcolor(BLACK) circle(400-x,400-y,2) void
planeshow(int i) int j circle(i5,
202, 2) circle(i3, 204, 2) for (j0
jlt8 j) circle(ij, 200, 2)
circle(i5, 198, 2) circle(i3, 196,
2) void ereasep(int i) int j
setcolor(BLACK) circle(i5, 202, 2)
circle(i3, 204, 2) for (j0 jlt8 j)
circle(ij, 200, 2) circle(i5, 198, 2)
circle(i3, 196, 2)
41a-shoot bullet u-plane up i-plane down
includeltdos.hgt include ltgraphics.hgt includeltcon
io.hgt void show(int i,float h, float v) void
erease(int i, float h, float v) void
planeshow(int i,int k) void ereasep(int i, int
k) void main(void) int driver
DETECT,mode int i,j,i1,s1,k int
y1,xx0 char x int a100
for(i0 ilt99 i) ai0
initgraph(driver,mode,"a\\bgi")
setcolor(WHITE) line(1,400,400,400)
j1 for ( i 0 i lt 80 i )
setcolor(BLUE) planeshow(5i, 5) show(i, 5.0,
9.0) y1 _AH0x01 geninterrupt(0x16)
y_FLAGS0x40
if(y 0) _AH0x00 geninterrupt(0x16)
x_AL if (x 'a') jj1aji
if (x 'u' ) sound(700) xxxx-5 if (x
'i' ) sound(200) xxxx5
setcolor(YELLOW) planeshow(5(i-8), xx) for
(k2 kltj k) if (ak!0) show(i-ak,
10.00.5k, 8.00.1k) delay
(300) nosound() ereasep(5i, 5)
ereasep(5(i-8), xx) erease(i, 5.0, 9.0) for
(k2 kltj k) if (ak!0) erease(i-ak,
10.00.5k, 8.00.1k)
closegraph() void show(int i, float h, float
v) int x, y xhi yvi-0.15ii
setcolor(RED) circle(400-x,400-y,2)
void erease(int i, float h, float v) int x,
y xhi yvi-0.15ii
setcolor(BLACK) circle(400-x,400-y,2) void
planeshow(int i,int k) int j
circle(i5, 202k, 2) circle(i3, 204k,
2) for (j0 jlt8 j) circle(ij,
200k, 2) circle(i5, 198k, 2)
circle(i3, 196k, 2) void ereasep(int i, int
k) int j setcolor(BLACK)
circle(i5, 202k, 2) circle(i3, 204k,
2) for (j0 jlt8 j) circle(ij,
200k, 2) circle(i5, 198k, 2)
circle(i3, 196k, 2)