Presentation is loading. Please wait.

Presentation is loading. Please wait.

The slides must be understood in Lecture 5

Similar presentations


Presentation on theme: "The slides must be understood in Lecture 5"— Presentation transcript:

1 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. 11/11/2018 CS3369 Real Time Control Software/DENG Xiaotie

2 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. 11/11/2018 CS3369 Real Time Control Software/DENG Xiaotie

3 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. 11/11/2018 CS3369 Real Time Control Software/DENG Xiaotie

4 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 11/11/2018 CS3369 Real Time Control Software/DENG Xiaotie

5 CS3369 Real Time Control Software/DENG Xiaotie
Types of Interrupt • Hardware Interrupts • CPU Interrupts • Software Interrupts 11/11/2018 CS3369 Real Time Control Software/DENG Xiaotie

6 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 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 how to invoke an interrupt given the interrupt number and the service number 11/11/2018 CS3369 Real Time Control Software/DENG Xiaotie

7 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# functionality 0x Read Next Keyboard Character 0x Report Whether Character Ready 0x Get Shift Status 0x Set Typematic Rate and Delay 0x Keyboard Write 0x Extended Keyboard Read 0x Get Extended Keystroke Status 0x 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. 11/11/2018 CS3369 Real Time Control Software/DENG Xiaotie

8 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. 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; _AH=0x0; geninterrupt(0x16); x=_AL; 11/11/2018 CS3369 Real Time Control Software/DENG Xiaotie

9 CS3369 Real Time Control Software/DENG Xiaotie
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. 11/11/2018 CS3369 Real Time Control Software/DENG Xiaotie

10 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. 11/11/2018 CS3369 Real Time Control Software/DENG Xiaotie

11 CS3369 Real Time Control Software/DENG Xiaotie
A Function to get a key char get_key_number () { //return type of the function is char char a; _AH=0x00; //service number 0x00 geninterrupt(0x16); //interrupt 0x16 a=_AL; //_AL is the key return a; //return the value } Demo the program A:key.cpp in the lecture. 1. Show the program 2. Run the program 11/11/2018 CS3369 Real Time Control Software/DENG Xiaotie

12 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. 11/11/2018 CS3369 Real Time Control Software/DENG Xiaotie

13 CS3369 Real Time Control Software/DENG Xiaotie
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 */ if (temp==0) { _AH=0; ch=_AL; } The difference is that the computer does not have to wait for users to type it. 11/11/2018 CS3369 Real Time Control Software/DENG Xiaotie

14 CS3369 Real Time Control Software/DENG Xiaotie
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. 11/11/2018 CS3369 Real Time Control Software/DENG Xiaotie

15 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; //service number 0x01 geninterrupt(0x16); //interrupt 0x16 x=_FLAGS &0x40; //get flag register if (x==0) {return 1;} //if x==0 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; 11/11/2018 CS3369 Real Time Control Software/DENG Xiaotie

16 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}; 11/11/2018 CS3369 Real Time Control Software/DENG Xiaotie

17 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; setcolor(BLACK); 11/11/2018 CS3369 Real Time Control Software/DENG Xiaotie

18 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. 11/11/2018 CS3369 Real Time Control Software/DENG Xiaotie


Download ppt "The slides must be understood in Lecture 5"

Similar presentations


Ads by Google