October 5, 2011

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.

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 –

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.