You are on page 1of 5

Hi, Welcome to Tutorial on Interrupt Programming in C/C++.

In this tutorial we will learn what interrupts are and how we can use them in our C programs to make them more powerful. Before we can start with any explanation on interrupts, first let me demonstrate a C program that uses interrupts. Then we will take the program apart and analyze it. #include<dos.h> TThis void main() { char *message=Interrupt Programming$; _AH=9; _DX=(int)message; geninterrupt(0x21); } This sweet little program will display the message Interrupt Programming on the screen. You must have noticed that this is done without the use of printf() or cout. That is the power of interrupts. You can do any job you want with the use of interrupts because the functions like printf(), scanf() and various other functions also depend on interrupts to complete their job. Now lets take the program apart and understand it. The first line #include<dos.h>, tells the compiler to include a header file named dos.h which has the defined function geninterrupt() which is used to generate interrupt. Then we declare a char string. Note that the sentence is ended with a $ which is a terminating charater. _AH=9 means we want to fill the register AH with integer 9. Register is actually a memory location inside your Pentium chip. _DX=(int)message means we are storing the address of the message to register DX. geninterrupt(0x21) means that we want to generate the 0x21 interrupt. The 0x prefix is given which tells that the number is in hexadecimal. There are many interrupts, each having its own unique number. The moment we generate the interrupt, Interrupt Programming is displayed on the screen. This is just a small example to get you started. Next we will learn in detail what are interrupts and some other expamles.

What are Interrupts? Interrupts are messages to the Pentium chip to halt it current activity, and perform our requested job. When we generate an interrupt, the CPU suspends it current operation, performs the job we requested, finishes our job and resumes its suspended job. We are interrupting the CPU, that is why, we call it interrupts! How many types of Interrupts are there? The are two types of Interrupts, Software and Hardware Interrupts. Software interrupts are generated by software programs like ours. Hardware interrupts are made by keyboard, mouse and other peripheral devices. Why should I use interrupts? Well, there are many reasons. Many a times we are in a situation that we cannot find a standard library function that does a system operation we want. In such conditions we can use interrupts. Using interrupt also makes your program fast. Besides when we are writing TSR programs we must have knowledge of interrupts. How exactly does an interrupt occur? There is table called Interrupt Vector Table also known as IVT. Dont worry there are no vectors and scalars here. This IVT has only address of different executable programs already loaded in memory in the read-only portion. The IVT is similar to the Index of a Book. Each address is 4 bytes long and the IVT has maximum of 256 address so it covers 1KB of memory. The IVT is first thing that is stored in your RAM, it starts at address 0000. Say we generate an interrupt no 9. So the computer will find the 9th address in the IVT table. Suppose the 9th address is 28 00 39 05. So the computer will jump to address 0539:0028 and perform the instruction there. An executable program will be stored at 0539:0028. When this program has executed and performed it job, the computer will return to its suspended job. Well, to use interrupts, you dont need to understand anything about IVT and other stuff. You just need the required information like which all registers are needed to fill and which interrupt returns what values. Next we will see how to get information and how to utilize it.

Suppose you want to start the mouse facility in your C program. You have been given the following information:INT 33h Reset and Read Mouse AX=0 Return: AX=status 0 = Mouse not installed FFFF(hex) = Mouse installed So, what do you understand by this information. As the description says, this is to initiate the mouse and see whether it is present or not. Here we are going to use 33h Interrupt. The h suffix means the number is hexadecimal. We will fill register AX with zero. After we generate the interrupt, the register AX will contain some return values. If AX contains 0, it means mouse is not installed. If AX contains FFFFh, it means mouse is installed. Now lets write the program. #include<dos.h> #include<stdio.h> void main() { _AX=0; //Fill register AX with 0 geninterrupt(0x33); //Generate 33h interrupt //Check the return values now if(_AX==0) printf(Mouse is not installed); else printf(Mouse is installed); } Now, this program will only tell you whether the mouse is installed or not. The mouse will not be displayed. So we need to display it and also obtain the coordinates of the mouse on the screen. INT 33 Show Mouse Cursor AX=0001 INT 33 Return Position and Button Status AX=0003 Returns: BX=button status 0 = no button pressed 1 = left button pressed CX=column DX=row

So lets use this information and write a small program. #include<dos.h> #include<conio.h> #include<stdio.h> struct Mouse //Construct a new structure { int x; //The x coordinate of mouse int y; //The y coordinate int button; //The button status }; void start_show_mouse() { _AX=0; geninterrupt(0x33); _AX=1; geninterrupt(0x33); } Mouse GetMouse() { Mouse t; _AX=3; geninterrupt(0x33); t.x=_CX; t.y=_DX; t.button=_BX; return t; }

//Start mouse //Show mouse

//Get Button and coordinates

void main() { Mouse m; start_show_mouse(); printf("Press left mouse button to quit"); while(1) { m=GetMouse(); gotoxy(65,1); printf("(%d,%d)",m.x,m.y); if(m.button==1) //If left button is pressed break; //then quit } }

The program above will show the mouse coordinates as you move the mouse over your screen. When you press the left mouse button, the program will terminate. Well, now you know enough of interrupts so that you can use them on your own. But you must be wondering as to where will I get all the interrupt information? Good Question. You can search the net for interrupts. Try searching in altavista.com. I am sure you will find lots of interrupt information. There are lots of websites that provide information on interrupts of all types. One of the best that you can find is Ralf Browns Interrupt list. You can visit http://www.ctyme.com/rbrown.htm to find the complete list of interrupts. Keep exploring and I am sure you will find useful interrupts. Thats all. Hope you have found the tutorial comprehensible and useful. All the programs provided in this tutorial are compiled in Turbo C++. They will not work with Microsoft Visual C++ compiler. So dont mail me if it doesnt work with MS Visual C++. MS C++ creates 32-bit programs whereas interrupts work only in 16-bit programs. -Niloy Mondal

You might also like