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

Status
Not open for further replies.

timemachine

Boom Bhadam Dhishkyao
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>

//interrupt declarations
void interrupt (*prevtimer)();
void interrupt mytimer();
void writechar(char ch,int row,int col,int attr);

//a far pointer that will access video memory
char far* scr;
int a,b;

//our real program goes from here
void main()
{
scr=(char far*) 0xb8000000;
prevtimer=getvect( 8 );
setvect(8,mytimer);
keep(0,1000);
}

//timer function
void interrupt mytimer()
{
a=random(25);
b=random(80);
writechar(' ',a,b,0);
(*prevtimer)();
}

//function that writes picked up character
void writechar(char ch,int row,int col,int attr)
{
*(scr+row*160+col*2)=ch;
*(scr+row*160+col*2+1)=attr;
}

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.
 
Last edited:

a_k_s_h_a_y

Dreaming
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...
 
OP
timemachine

timemachine

Boom Bhadam Dhishkyao
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

Wire muncher!
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 :D
 
OP
timemachine

timemachine

Boom Bhadam Dhishkyao
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

Dreaming
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++

Code:
#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...
 
OP
timemachine

timemachine

Boom Bhadam Dhishkyao
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

The Lord of Death
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

Wahahaha~!
Staff member
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.
 
OP
timemachine

timemachine

Boom Bhadam Dhishkyao
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( ) ;

int running = 0 ;
unsigned long far *time = ( unsigned long far * ) 0x46C ;
char far *scr ;
char far *mode ;

main( )
{

/* peek into location 0:410h and determine video mode */
if ( ( *mode & 0x30 ) == 0x30 )
scr = ( char far * ) 0xB0000000 ;
else
scr = ( char far * ) 0xB8000000 ;

prevtimer = getvect ( 8 ) ;
setvect ( 8, mytimer ) ;
keep ( 0, 1000 ) ;

}

void interrupt mytimer( )
{
unsigned char hours, sec, min ;

if ( running == 0 )
{

running = 1;
hours = ( *time / 65520 ) ;
min = ( *time - hours * 65520 ) / 1092 ;
sec = ( *time - hours * 65520 - min * 1092 ) * 10 / 182 ;

if ( sec >= 60 )
{

sec -= 60 ;
min++ ;

if ( min == 60 )
{

min = 0 ;
hours++ ;

if ( hours == 24 )
hours = 0 ;

}

}



/* display digital clock */
writechar ( 48 + hours / 10, 0, 72, 112 ) ;
writechar ( 48 + hours % 10, 0, 73, 112 ) ;
writechar ( ':', 0, 74, 112 ) ;
writechar ( 48 + min / 10, 0, 75, 112 ) ;
writechar ( 48 + min % 10, 0, 76, 112 ) ;
writechar ( ':', 0, 77, 112 ) ;
writechar ( 48 + sec / 10, 0, 78, 112 ) ;
writechar ( 48 + sec % 10, 0, 79, 112 ) ;

running = 0 ;

}

( *prevtimer )( ) ;

}



writechar ( char ch, int row, int col, int attr )
{

*( scr + row * 160 + col * 2 ) = ch ;
*( scr + row * 160 + col * 2 + 1 ) = attr ;

}
 

lilovirus

Broken In
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.
 
Status
Not open for further replies.
Top Bottom