October 5, 2011

Interrupt Programming in C

Do you know how C.P.U comes to know that a key is pressed, left mouse button is clicked??

To know the reason we roll back to when it all started. Earlier the approach to get input data was too clumsy it made C.P.U. to enter in an infinite loop and ask every peripheral about data received and then process it and send back instructions this is called polling. A serious drawback to this approach was loss of data if C.P.U. got busy with users program and lost vital information of both Program and Input.


In modern systems this polling is relieved by use of interrupt’s you don’t wait in loop instead now it’s peripherals responsibility to make you aware of data arrived by generating specific interrupt. Like mouse generates interrupt 33h, keyboard generates 60h, etc…. apart from it interrupts are of great use since they do very amazing tasks.. imagine multithreading in TURBO C.

Second thing to know is C.P.U’s reaction on interrupt it suspends all its tasks and takes FAR jump to interrupt handler (every interrupt has its specific handler).executes the code and then returns to suspended task.

Far Jump
Far jump means CS:IP (registers which point to the next instruction of your program) both are pushed(saved) on stack and restored when C.P.U. returns from call.

Before You Program Interrupts
You need solid knowledge of what you are going to do by getting yourselves an interrupt list. GO for Ralph Brown’s interrupt list. Basically there are 256 interrupts but the sub functions of a interrupt are what makes it really big list.

Check it out here
http://www.ctyme.com/intr/int.htm

Classification of Interrupt’s


Do you know - all the working of your favorite turbo c functions is via interrupts. Feel free to ask Source code of them here, but the whole coding is assembly language so you need to Be familiar with it.

Hooking Interrupts
Hooking an interrupt means setting your handler in place of preset handler. To a programmer level this means calling of our handler whenever hooked interrupt occurs. In TURBO C we use getvect() and setvect() to set our interrupt handler.

Vector
A Vector is the address of that specific interrupt handler. The function getvect() gives address of preset handler while setvect()sets our address.
CAUTION – when you hook interrupt do restore it back at the end of program.

Do you know - if you hook some interrupt’s you can implement quite good exception handling in TURBO C.

Practical
Ques – print “Hello world” 20 times without using any loop not using printf().
ANS –
/* interrupt.c*/
#include <stdio.h>
#include <dos.h>
#include <conio.h>

// clock tick interrupt happens 18.2 times per second but practically
// more or less, not always 18
#define INTERRUPT 0X1C

//pointer to old function (will point to old vector)
void interrupt (*oldhandler)();


// GLOBAL VARS
int count = 0;
char str[] = “Hello World!!!$”;
void interrupt newhandler(); // prototype of our new handler


int main() {
clrscr();

// Save old vector
oldhandler = getvect(INTERRUPT);

// Set our handler
setvect(INTERRUPT,newhandler);

// give time for handler to be called
delay (1200);

printf(“ROUTINE CALLED %d TIMES”,count);

// restore old vector
setvect(INTERRUPT,oldhandler);

getch();
return 0;
}



// our CLOCK-TICK handler
void interrupt newhandler() {
count++;
asm mov dx , offset str
asm mov ah , 09h
asm int 21h
// give chance to old handler to clean up nicely
oldhandler();
}

Explanation :
newhandler() => it just displayed hello world on screen and no of times it gets displayed. Using following service.
INT 21h
AH = 9
DS: DX = pointer to buffer containing string terminated by $
Though it’s perfectly valid to call any function within our handler but normally you should rely on assembly only because it might CRASH!!!
The last thing it does was to call old handler because we don’t know what old handler’s coding is and what it does (you can omit it if you want .it’s just a good practice to call old handler). While this is fun with interrupt later we will implement multitasking via clock tick interrupt.

Hope you enjoyed it!
Don’t forget to ask queries..

Related Posts :



1 comments:

lots of new things for someone like me, who is new to low level programming, somehow got the theory but the code was like bouncer...better if u provide little easier code to get started :)

Post a Comment

Twitter Delicious Facebook Digg Stumbleupon Favorites More

 
Related Posts with Thumbnails