# TSR(Terminate and Stay Resident in memory) in C. A small Tutorial



## timemachine (Sep 27, 2007)

Terminate and Stay Resident(TSR) is a system call in the DOS operating systems that returned control to the system as if the program had quit, but kept the program in memory. Many times reffered as multitasking in DOS. 
         A DOS based C compiler( such as Turbo C++) offers 640KB of memory and for DOS 1 MB of memory is allocated. TSR uses the extended memory, i.e. the 1MB memory by the use of far pointers. A memory area is allocated and the program actively resides in it. Conventionally, when a program or a command is executed, the whole control is transferred to it. In this case the program actively resides in the memory. 
                            Here is a sample code which shows the working of a TSR. This TSR program hooks itself with timer interrupt and selects a random row and coloums position at each run and writes space at that position, the person using the computer feels that something is eating up the characters from the screen.



> #include"dos.h"
> #include<conio.h>
> #include<stdlib.h>
> 
> ...



Here the hex memory address 0xb8000000 refers to memory location of the VDU memory. In the function* mytimer*, the random numbers from 80 and 25 are picked. These are the row number and the column number. Then the writechar function puts a blank character at that place. This program continues when the DOS is open. The program name will be displayed in the title bar if you are in the window mode. 

Remember that its only in dos. You must go to the command line and then run the exe of the program or you will not be able to view any  response of the program. I insist perform a dir command and then you will be able to see the right work of code. By this screen will be filled by some characters and the blank spaces will be seen easily.


More information related to TSR will be attached to the thread. Respond if you like my first post : *TIMEMACHINE.
*


----------



## a_k_s_h_a_y (Sep 27, 2007)

yes i liked it..
post more.. from where to learn all that ??
i am only in 3rd sem CSE ...... yet..
i understand that u know all those system calls and you are making use of them...
far is not allowed in C++ right ?? and Dev C++ gives error with that..

welcome to digit forum... it needs programmers like u...

and 0xb8000000L is the start address of VRAM for colour monitor adapter..
i dunno if that will work out...


----------



## timemachine (Sep 27, 2007)

actually Dev C++ is a windows based compiler. Here we are talking about the DOS based compilers. These system calls and memory locations are used only in DOS. For windows, there are some other. They changes from OS 2 OS. 

Not exactly. C++ make use of it if you include the "dos.h". 

I started from Let us C by yashwant kanetkar
then Pointers in C by same
nd then TSR programming and Graphics programming 
I m currently studying Java Swings nd Networking API in C in UNIX plateform
i m in BSC III yr


----------



## infra_red_dude (Sep 27, 2007)

a very good example of TSR program is the realtime antivirus scanners. that 1MB extended is again divided split into two: UMB (upper memory block) bein one of them. if ppl from DOS era here remember there was line in config.sys DEVICEHIGH= whcih loaded the driver onto this high memory leaving the other 640KB free.

i remember the numerous tricks i've tried to free the 640K block freed by loading TSR to the high memory area so that games cud run


----------



## timemachine (Sep 27, 2007)

Ya that's the best part. But the worst is that you can't keep the memory free you have allocated. For that another program which is called the "GARBAGE COLLECTOR" in todays reference should be run to free the memory from that.


----------



## a_k_s_h_a_y (Sep 27, 2007)

timemachine said:
			
		

> Not exactly. C++ make use of it if you include the "dos.h".



sure ?? just now tired to compile this..in dev C++


```
#include <stdio.h>
#include <dos.h>

int main()
{
    char far *vram_start;
    return 0;
}
```

getting a syntax error...
C:\Documents and Settings\Aks\My Documents\C programming Practise\vram.c syntax error before '*' token 

or maybe as u said its a windows based compiler..

i use turbo c for my texteditor project.. which is pretty simple..but too many buttons to include.....and am too lazy.. got to improve that...


----------



## timemachine (Sep 27, 2007)

Ya. its because it is a windows based compiler, a improved version.
What i m talking about is a manual method of multitasking in DOS.
Windows supports that feature in a different manner. 

So you are making a text editor, using turboc2
good ol graphics..


----------



## Yamaraj (Sep 27, 2007)

TSRs are an old concept, remains of the DOS era and unfortunately still being taught in universities in this country, and others. Modern operating systems use daemons and services, and the programming model has changed a lot.


----------



## Faun (Sep 27, 2007)

Yamaraj said:
			
		

> TSRs are an old concept, remains of the DOS era and unfortunately still being taught in universities in this country, and others. Modern operating systems use daemons and services, and the programming model has changed a lot.



i hav gone through the TSR phase in SE.


----------



## timemachine (Sep 27, 2007)

One more program in TSR. It is a clock which is displayed at the upper right corner of the screen.



> #include "dos.h"
> 
> void interrupt ( *prevtimer )( ) ;
> void interrupt mytimer( ) ;
> ...


----------



## deepak.krishnan (Sep 29, 2007)

nice info friends...


----------



## lilovirus (Nov 11, 2007)

TSR used to be in DOS.
Now everything is TSR in windows.
All programms work using messaging and stays in memory until we explicitly call exit.


----------

