The slides must be understood in Lecture 5

advertisement
The slides must be understood in
Lecture 5
 pages 4, 6, 8,9,12,17,18,19,20,21,22,23,24,25.
 Some slides have errors. Please use the new one
I distributed today.
Page 1
5/29/2016
CS3369 Real Time Control
Software/DENG Xiaotie
Interrupt
• 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.
Page 2
5/29/2016
CS3369 Real Time Control
Software/DENG Xiaotie
Steps 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.
Return to that program the execution of which was
suspended after recognizing the interrupt.
Page 3
5/29/2016
CS3369 Real Time Control
Software/DENG Xiaotie
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
Page 4
5/29/2016
CS3369 Real Time Control
Software/DENG Xiaotie
Types of Interrupt
• Hardware Interrupts
• CPU Interrupts
• Software Interrupts
Page 5
5/29/2016
CS3369 Real Time Control
Software/DENG Xiaotie
Software Interrupts
Software interrupts are generated by using the software interrupt
instruction. In turbo C:
#include <dos.h>
geninterrupt(n);
 BIOS interrupts: provides the most direct, low level
interaction with the I/O devices and give a deviceindependent 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 how to invoke
an interrupt given the interrupt
number and the service number
Page 6
5/29/2016
CS3369 Real Time Control
Software/DENG Xiaotie
BIOS Keyboard Services
They are invoked with interrupt number 0x16 with the following
Service numbers in the register AH (_AH in Turbo C).
service#








0x00
0x01
0x02
0x03
0x05
0x10
0x11
0x12
functionality
Read Next Keyboard Character
Report Whether Character Ready
Get Shift Status
Set Typematic Rate and Delay
Keyboard Write
Extended Keyboard Read
Get Extended Keystroke Status
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.Page 7
5/29/2016
CS3369 Real Time Control
Software/DENG Xiaotie
Interrupt 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.
 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.
 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.
To invoke this service to read a character
into a variable x in Turbo C, we do:
char x; _AH=0x0; geninterrupt(0x16); x=_AL;
5/29/2016
CS3369 Real Time Control
Software/DENG Xiaotie
Page 8
The Steps to Invoke an Interrupt





(1) send the service number to _AH; _AH=0x01;
(2) invoke the interrupt by using geninterrupt(interrupt number);
(3) immediate store the result (if any). Usually the result is in _AL;
(int x; x=_AL);
(Also somethimes, when input is required, _AL is used to store the input.)
See examples later.
 (If more outputs/inputs are required, other registors like, B or C are used.)
 We have to referee to book for details. No need to remember ALL. However, the
examples given in lectures should be remembered.
Remember to include dos.h in your file.
Page 9
5/29/2016
CS3369 Real Time Control
Software/DENG Xiaotie
An Example of Getting a Key from Keyboard





char x; int y;
_AH=0x0;
geninterrupt(0x16);
x=_AL;
cout <<“The key is: ”<< x <<endl;





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.
Page 10
5/29/2016
CS3369 Real Time Control
Software/DENG Xiaotie
A Function to get a key
 char get_key_number () {

char a;

_AH=0x00;

geninterrupt(0x16);

a=_AL;

return a;

}
//return type of the function is char
//service number 0x00
//interrupt 0x16
//_AL is the key
//return the value
Demo the program A:key.cpp in the lecture.
1. Show the program
2. Run the program
Page 11
5/29/2016
CS3369 Real Time Control
Software/DENG Xiaotie
Interrupt 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; _AH=0x01; geninterrupt(0x16);
To test whether a character is ready after the above
steps in Turbo C, we do:
int y;
y=_FLAGS&0x40
if (y==0)
…
to check whether ZF is 0 or 1.
5/29/2016
CS3369 Real Time Control
Software/DENG Xiaotie
Page 12
Use 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, temp;
_AH=0x01;
geninterrupt(0x16);
temp=_FLAGS&0x40;

/*must put the data to temp, see slide 32 */




5/29/2016
The difference is that the
computer does not have to
wait for users to type it.
if (temp==0) {
– _AH=0;
– geninterrupt(0x16);
– ch=_AL;
}
CS3369 Real Time Control
Software/DENG Xiaotie
Page 13
Caution 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.
Page 14
5/29/2016
CS3369 Real Time Control
Software/DENG Xiaotie
Write a function to get a character

int key_ready() {//return 1 if a key is ready, 0 otherwise
•
•
•
•
•
•


long int x;
_AH=1;
geninterrupt(0x16);
x=_FLAGS &0x40;
if (x==0) {return 1;}
else return 0;
//service number 0x01
//interrupt 0x16
//get flag register
//if x==0 a key is ready
//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;

}
Page 15
5/29/2016
CS3369 Real Time Control
Software/DENG Xiaotie
The following does not work

int key_ready() {//return 1 if a key is ready, 0 otherwise
•
•
•
•
•


long int x;
_AH=1;
//service number 0x01
geninterrupt(0x16);
//interrupt 0x16
if (_FLAGS&0x40==0) {return 1;}
else return 0;
//else no key
}
if (_FLAGS&0x40==0) {return 1;} must be replaced by
•
•
x=_ FLAGS&0x40;
if(x==0) {return 1};
Page 16
5/29/2016
CS3369 Real Time Control
Software/DENG Xiaotie
Shoot a bullet when press a key
#include<dos.h>
#include <graphics.h>
#include<iostream.h>
#include<conio.h>
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 y=1;
initgraph(&driver,&mode,"D:\\bc31\\bgi");
setcolor(WHITE);
line(1,400,400,400);
for ( i = 0; i < 80; i++ )
{
show(i, 5.0, 9.0);
y=1;
_AH=0x01;
geninterrupt(0x16);//
y=_FLAGS&0x40;
if(y == 0)
{s1=1;i1=i;
_AH=0x00;
geninterrupt(0x16);}
if (s1==1) {show(i-i1, 10.0, 8.0); }
delay (300);
erease(i, 5.0, 9.0);
if (s1==1) erease(i-i1, 10.0, 8.0);
}
closegraph();
}
void show(int i, float h, float v)
{ int x, y;
x=h*i;
y=v*i-0.15*i*i;
setcolor(RED);
circle(400-x,400-y,2);
}
void erease(int i, float h, float v)
{
int x, y;
x=h*i;
y=v*i-0.15*i*i;
setcolor(BLACK);
circle(400-x,400-y,2);
}
Page 17
5/29/2016
CS3369 Real Time Control
Software/DENG Xiaotie
Shoot a bullet when press a key
The previously program shows one bullet when you
execute the program. If you press ANY key during
the execution, the second bullet will start. The
starting time is controlled by keys.
Page 18
5/29/2016
CS3369 Real Time Control
Software/DENG Xiaotie
Download