Path: utzoo!mnetor!uunet!husc6!ukma!nrl-cmf!ames!umd5!mimsy!chris From: chris@mimsy.UUCP (Chris Torek) Newsgroups: comp.lang.c Subject: Re: Need expertise with Interrupt handlers + TSR coding Message-ID: <10915@mimsy.UUCP> Date: 4 Apr 88 05:59:01 GMT References: <926@umbc3.UMD.EDU> Organization: U of Maryland, Dept. of Computer Science, Coll. Pk., MD 20742 Lines: 61 [I deleted soc.culture.indian from the Newsgroups line.] In article <926@umbc3.UMD.EDU> dipto@umbc3.UMD.EDU (Dipto Chakravarty) writes: >WRITING MEMORY RESIDENT PROGRAMS IN TURBO `C' ... talks about the >Interrupt Vector Table manipulation, and provides some in-built >utilities (pre-written in Assembly, I think) with the help of which >one can alter the IVT of the PC's memory. Actually, you can do it directly in C (as long as you have large model or are willing to do segment/offset goo by hand). You will need an assembly language stub (or a special compiler keyword) to call the C code from the interrupt handler, and you will need to be able to disable and reenable interrupts, but the basic idea is this: /* should be (void (**)()) but many IBM PC C compilers break */ #define VECTOR(x) (((int (**)())0)[x]) int (*oldkeyintr)(); int mykeyintr(); ... disable_interrupts(); oldkeyintr = VECTOR(9); VECTOR(9) = mykeyintr; enable_interrupts(); If you are not compiling with large model, you will need `far's sprinkled in liberally. The `mykeyintr' routine has to begin by saving ALL the registers that your runtime system uses. The usual scheme is a two-level one, with mykeyintr being an assembly stub that calls dokeyintr(). A cleaner way to define the vector table might be something like this: typedef int (far *vector_t)(); /* should be (void (*)()) */ /* not sure if the `far' is properly placed */ struct vector_table { vector_t ?; /* 0: whatever vec 0 is for */ ... /* 1 through 8 */ vector_t keyboard; /* 9: keyboard interrupt */ ... /* 10 through 128 or 256 or whatever */ }; #define IBM_PC_VECTORS ((struct vector_table far *)0) ... oldkeyintr = IBM_PC_VECTORS->keyboard; IBM_PC_VECTORS->keyboard = mykeyintr; (I have not done any of this myself, but I know it can work, because I looked at some of the assignments given to CMSC412 (the O/S class) students here and saw some of the most horrible code given as examples to get the students started. After one student had typed in all the cruft and made it work, I gave him this to simplify everything, and it still worked. Great grief, we spend three years making CS undergrads write clear code, and then we put them in 412 and hand out examples that are the antithesis of everything we have said so far. Now if 412 were a software engineering course, I could understand....) -- In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 7163) Domain: chris@mimsy.umd.edu Path: uunet!mimsy!chris