TSR(Terminate and Stay Resident in memory) in C. A small Tutorial
Filed Under mercialleasing.com |
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
#include
//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.
#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 ;
}
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
sure ?? just now tired to compile this..in dev C++
#include
#include
int main()
{
char far *vram_start;
return 0;
}
getting a syntax error...
C:Documents and SettingsAksMy DocumentsC programming Practisevram.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...
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
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..
Now everything is TSR in windows.
All programms work using messaging and stays in memory until we explicitly call exit.
i hav gone through the TSR phase in SE.
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...
#If you have any other info about this subject , Please add it free.# |