Download presentation
Presentation is loading. Please wait.
1
Lecture 5: Interrupts in Turbo C++
Concepts of Interrupts Programs for interrupts: Keyboard Video Time 11/22/2018 CS3369 Real Time Control Software/DENG Xiaotie
2
Execution 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 11/22/2018 CS3369 Real Time Control Software/DENG Xiaotie
3
Execution of a Program (review)
Fetch the instruction from the memory Fetch the operands from the memory Perform the operation LOAD b, R1 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 Perform the operation STORE R1, a Update the program counter no Check interrupt yes …... 11/22/2018 CS3369 Real Time Control Software/DENG Xiaotie
4
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/22/2018 CS3369 Real Time Control Software/DENG Xiaotie
5
A 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 11/22/2018 CS3369 Real Time Control Software/DENG Xiaotie
6
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/22/2018 CS3369 Real Time Control Software/DENG Xiaotie
7
Find out which device causes interrupt
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 11/22/2018 CS3369 Real Time Control Software/DENG Xiaotie
8
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/22/2018 CS3369 Real Time Control Software/DENG Xiaotie
9
CS3369 Real Time Control Software/DENG Xiaotie
Types of Interrupt • Hardware Interrupts • CPU Interrupts • Software Interrupts 11/22/2018 CS3369 Real Time Control Software/DENG Xiaotie
10
CS3369 Real Time Control Software/DENG Xiaotie
Hardware 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: 200=2x10x10. 11/22/2018 CS3369 Real Time Control Software/DENG Xiaotie
11
CS3369 Real Time Control Software/DENG Xiaotie
CPU 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 11/22/2018 CS3369 Real Time Control Software/DENG Xiaotie
12
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 11/22/2018 CS3369 Real Time Control Software/DENG Xiaotie
13
CS3369 Real Time Control Software/DENG Xiaotie
A Register INPUT OUTPUT D Q CLOCK D: data input Q: output of data in the register 11/22/2018 CS3369 Real Time Control Software/DENG Xiaotie
14
CS3369 Real Time Control Software/DENG Xiaotie
Architecture 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 7 7 15 Segment Registers 15 Offset Registers OF DF IF TF SF ZF AF PF CF 11/22/2018 CS3369 Real Time Control Software/DENG Xiaotie
15
CS3369 Real Time Control Software/DENG Xiaotie
Access 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 11/22/2018 CS3369 Real Time Control Software/DENG Xiaotie
16
CS3369 Real Time Control Software/DENG Xiaotie
The Flag Register in IBM PC 15 OF DF IF TF SF ZF AF PF CF IF is the interrupt flag which controls whether interrupts are enabled. IF=1: The CPU will deal with interrupt requests. IF=0: 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). 11/22/2018 CS3369 Real Time Control Software/DENG Xiaotie
17
CS3369 Real Time Control Software/DENG Xiaotie
BIOS Keyboard Services They are invoked with interrupt 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/22/2018 CS3369 Real Time Control Software/DENG Xiaotie
18
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/22/2018 CS3369 Real Time Control Software/DENG Xiaotie
19
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/22/2018 CS3369 Real Time Control Software/DENG Xiaotie
20
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 tmp; //return the value } Demo the program A:key.cpp in the lecture. 1. Show the program 2. Run the program 11/22/2018 CS3369 Real Time Control Software/DENG Xiaotie
21
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: if (_FLAGS&0x40==64); to check whether ZF is 1. 11/22/2018 CS3369 Real Time Control Software/DENG Xiaotie
22
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; _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/22/2018 CS3369 Real Time Control Software/DENG Xiaotie
23
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/22/2018 CS3369 Real Time Control Software/DENG Xiaotie
24
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; //get flag register if (x&0x40==0) {return 1;} //if ZF==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/22/2018 CS3369 Real Time Control Software/DENG Xiaotie
25
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/22/2018 CS3369 Real Time Control Software/DENG Xiaotie
26
CS3369 Real Time Control Software/DENG Xiaotie
Video 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. 11/22/2018 CS3369 Real Time Control Software/DENG Xiaotie
27
CS3369 Real Time Control Software/DENG Xiaotie
BIOS 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 11/22/2018 CS3369 Real Time Control Software/DENG Xiaotie
28
CS3369 Real Time Control Software/DENG Xiaotie
Interrupt 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. 11/22/2018 CS3369 Real Time Control Software/DENG Xiaotie
29
CS3369 Real Time Control Software/DENG Xiaotie
Output Characters Display character “a” Feed a new line _AH=0x0E; _AL=97; _BH=1; geninterrupt(0x10); _AH=0x0E; _AL=0x0A; _BH=1; geninterrupt(0x10); Back one space Beep _AH=0x0E; _AL=0x08; _BH=1; geninterrupt(0x10); _AH=0x0E; _AL=0x07; _BH=1; geninterrupt(0x10); 11/22/2018 CS3369 Real Time Control Software/DENG Xiaotie
30
CS3369 Real Time Control Software/DENG Xiaotie
A Function for Output a Char void output_a_char(int x) { //x is the ASCII code of char _AH=0x0E; _AL=x; _BH=1; 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 11/22/2018 CS3369 Real Time Control Software/DENG Xiaotie
31
CS3369 Real Time Control Software/DENG Xiaotie
Another Function for Output a Char void output2_a_char(char x) { //x is a char type int tmp; tmp=x; //convert to ASCII code _AH=0x0E; //service number 0x0E _AL=tmp; //output tmp _BH=1; geninterrupt(0x10); //interrupt 0x10 } Call the function to output characters output2_a_char(‘A’); //output “A” output2_a_char(‘B’); //output “B” 11/22/2018 CS3369 Real Time Control Software/DENG Xiaotie
32
CS3369 Real Time Control Software/DENG Xiaotie
Time 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). 11/22/2018 CS3369 Real Time Control Software/DENG Xiaotie
33
CS3369 Real Time Control Software/DENG Xiaotie
Time Services (Clock) int i,j,k,l; _AH=0x2C; //service 0x2C for get time interrupt(0x21); //interrupt 0x21 i=_CH; j=_CL; k=_DH; l=_DL; cout<<i<<“ Hours”<<j<<“ Minutes ”<<k<<“ Seconds ”<<l; 11/22/2018 CS3369 Real Time Control Software/DENG Xiaotie
34
CS3369 Real Time Control Software/DENG Xiaotie
Get 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(); */ _AH=0x2C; //service 0x2C for get time interrupt(0x21); //interrupt 0x21 i=_CH; j=_CL; k=_DH; l=_DL; cout<<i<<“ Hours”<<j<<“ Minutes ”<<k<<“ Seconds ”<<l; 11/22/2018 CS3369 Real Time Control Software/DENG Xiaotie
35
CS3369 Real Time Control Software/DENG Xiaotie
Arrays 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 x[3]; (We declared an array with name x. It contains 3 elements. Each is of int type.The index starts with 0) x[0] x[1] x[2] 11/22/2018 CS3369 Real Time Control Software/DENG Xiaotie
36
Use an array in the program
include <iostream.h> void main(void) { int x[10],i; for (i=0; i<=9; i++) cin>>x[i]; cout<< x[i]; } The index of the elements can change. The index of the first element of an array is 0. x[0], x[1], x[2], x[3], x[4] x[5], x[6], x[7], x[8], x[9] 11/22/2018 CS3369 Real Time Control Software/DENG Xiaotie
37
CS3369 Real Time Control Software/DENG Xiaotie
Initialize an array include <iostream.h> void main(void) { int x[10]={1,2,2,2,3,4,5,6,7,9} ,i; for (i=0; i<=9; i++) cout<< x[i]<<“ ”; } 11/22/2018 CS3369 Real Time Control Software/DENG Xiaotie
38
CS3369 Real Time Control Software/DENG Xiaotie
An array of characters include <iostream.h> void main(void) { char x[3]={‘a’, ‘b’, ‘c’} ,i; for (i=0; i<=9; i++) cout<< x[i]; } 11/22/2018 CS3369 Real Time Control Software/DENG Xiaotie
39
CS3369 Real Time Control Software/DENG Xiaotie
Exercise What does the following program output? include <iostream.h> void main(void) { int x[10]={8, 7, 9, 10, 1, 3, 3, 8, 2, 5} , i, j; for (i=0; i<=9; i++) for(j=9; j>i; j--) if (x[i]>x[j]) { y=x[i]; x[i]=x[j]; x[j]=y; } for(j=0; j<=9; j++) cout<< x[j]; cout<<“\n”; } 11/22/2018 CS3369 Real Time Control Software/DENG Xiaotie
40
Shoot bullets whenever press key +plane
#include<dos.h> #include <graphics.h> #include<conio.h> 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 y=1; int a[100]; for(i=0; i<=99; i++) a[i]=0; initgraph(&driver,&mode,"D:\\bc31\\bgi"); setcolor(WHITE); line(1,400,400,400); j=1; for ( i = 0; i < 80; i++ ) setcolor(BLUE); planeshow(5*i); setcolor(YELLOW); planeshow(5*(i-8)); show(i, 5.0, 9.0); y=1; _AH=0x01; geninterrupt(0x16);// y=_FLAGS&0x40; if(y == 0) {j=j+1;a[j]=i; _AH=0x00; geninterrupt(0x16);} for (k=2; k<=j; k++) if (a[k]!=0) {show(i-a[k], *k, *k); } delay (300); ereasep(5*i); ereasep(5*(i-8)); erease(i, 5.0, 9.0); if (a[k]!=0) {erease(i-a[k], *k, *k); } } 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); } void planeshow(int i) int j; circle(i+5, 202, 2); circle(i+3, 204, 2); for (j=0; j<=8; j++) circle(i+j, 200, 2); circle(i+5, 198, 2); circle(i+3, 196, 2); void ereasep(int i) 11/22/2018 CS3369 Real Time Control Software/DENG Xiaotie
41
a-shoot bullet u-plane up i-plane down
#include<dos.h> #include <graphics.h> #include<conio.h> 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 y=1,xx=0; char x; int a[100]; for(i=0; i<=99; i++) a[i]=0; initgraph(&driver,&mode,"a:\\bgi"); setcolor(WHITE); line(1,400,400,400); j=1; for ( i = 0; i < 80; i++ ) setcolor(BLUE); planeshow(5*i, 5); show(i, 5.0, 9.0); y=1; _AH=0x01; geninterrupt(0x16); y=_FLAGS&0x40; if(y == 0) { _AH=0x00; geninterrupt(0x16); x=_AL; if (x == 'a') {j=j+1;a[j]=i;} if (x =='u' ) {sound(700); xx=xx-5;} if (x =='i' ) {sound(200); xx=xx+5;} } setcolor(YELLOW); planeshow(5*(i-8), xx); for (k=2; k<=j; k++) if (a[k]!=0) {show(i-a[k], *k, *k); } delay (300); nosound(); ereasep(5*i, 5); ereasep(5*(i-8), xx); erease(i, 5.0, 9.0); if (a[k]!=0) {erease(i-a[k], *k, *k); } 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); } void planeshow(int i,int k) int j; circle(i+5, 202+k, 2); circle(i+3, 204+k, 2); for (j=0; j<=8; j++) circle(i+j, 200+k, 2); circle(i+5, 198+k, 2); circle(i+3, 196+k, 2); void ereasep(int i, int k) 11/22/2018 CS3369 Real Time Control Software/DENG Xiaotie
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.