| Programs which remain running and resident in memory while other 
            programs are running are the most exciting line of programming for 
            many PC developers. This type of program is known as a "Terminate 
            and Stay Resident" or "TSR" program. eg.(Operating Doskey, etc.) In theory a TSR is quite simple. It is an ordinary program which 
            terminates not through the usual DOS terminate function, but through 
            the DOS "keep" function - interrupt 27h. This function reserves an 
            area of memory, used by the program so that no other programs will 
            overwrite it. This in itself is not a very difficult task, excepting 
            that the program needs to tell DOS how much memory to leave it! Basically to make a TSR program we have to decide which interrupt 
            to catch.What i mean is that we have to decide when to pop up the 
            TSR. Like if we want that it should popup when user has pressed F1 
            key or any key we have to catch the keyboard interrupt. Likewise if you wanna popup TSR after every 2 minutes we have to 
            capture Timer interrupt or if you wanna make TSR active when any 
            reading or writing on hard disk is done you have to capture that 
            interrupt which governs the task.Poping a TSR means that the code 
            executes from the ashes ie. the code portion which we make resident 
            and was not responding is now to be executed. In our TSR code first 
            we have to capture the interrupt ie we have to get it's address 
            where in the memory it is stored then we have to replace the address 
            with the address of our code and then within our code we have to 
            call the actual procedure after or before executing our code. The difficulties in programming TSRs comes from the limitations 
            of DOS which is not a multi-tasking operating system, and does not 
            react well to re-enterant code. That is it's own functions 
            (interrupts) calling themselves. The problems stem mainly from not 
            being able to use DOS function calls within the TSR program once it 
            has "gone resident". There are a few basic rules which help to clarify the problems 
            encountered in programming TSRs: 1. Avoid DOS function calls 2. Monitor the DOS 
            busy flag, when this flag is nonzero, DOS is executing an interrupt 
            21h function and MUST NOT be disturbed!
 3. Monitor 
            interrupt 28h. This reveals when DOS is busy waiting for console 
            input. At this time you can disturb DOS regardless of the DOS busy 
            flag setting.
 4. Provide some way of checking whether the 
            TSR is already loaded to prevent multiple copies occuring in memory.
 5. Remember that other TSR programs may be chained to 
            interrupts, and so you must chain any interrupt vectors that your 
            program needs.
 6. Your TSR program must use its own 
            stack, and NOT that of the running process.
 7. TSR 
            programs must be compiled in a small memory model with stack 
            checking turned off.
 8. When control passes to your TSR 
            program, it must tell DOS that the active process has 
        changed.
 |