[comp.os.msdos.programmer] TSR's - Must they be converted to .COM?

cs00chs@unccvax.uncc.edu (charles spell) (02/16/91)

If TSR's must be converted to .COM, why? I know that there are some .EXE
files launch a TSR, how? Thanx in advance.

uunet!mcnc!unccvax!cs00chs
-- 
 .--------------------------.  ...  |On the border of your mind lies a place
 |uunet!mcnc!unccvax!cs00chs|  (")  |where dreams and reality are one...I will 
 `--------------------------'-w-U-w-|take you there, for I am the subject...
 \%\%\%\%\%\%\%\%\%\%\%\%\%\%\%\%\%\|the subject of your imagination. -Aldo Nova

n8541751@unicorn.cc.wwu.edu (Where there is darkness, light) (02/16/91)

cs00chs@unccvax.uncc.edu (charles spell) writes:

>If TSR's must be converted to .COM, why? I know that there are some .EXE
>files launch a TSR, how? Thanx in advance.

No, TSR's don't have to be converted to .COM files.  Usually it simplifies
programming though, because well-behaved TSR programs must follow certain
unofficial rules, such as using their own built in stack, in order to
be universally successful.  Limitations such as this make the use of
a single segment very convenient, thus the traditional way of writing
a TSR is in assembly language, using a .COM organization.

One of the most common TSR programs is the installable DOS device driver.
This had to be a .COM file until DOS 3.0 arrived, with the capability to
load an .EXE format driver.  Most programmers, myself included, still
write these as .COM files to remain compatible with older DOS versions
which are unfortunately still in use.

A good book which explains this subject further is:

DOS Programmer's Reference by Terry R. Dettmann, QUE books,
ISBN0-88022-327-8, LC 87-60811.

It also has much other information, including undocumented DOS functions,
critical to writing TSR's.

Kris.
-- 
Kriston M. Bruland          |    . .         . .      . . .      .       . .
n8541751@unicorn.cc.wwu.edu |    .   .     . .        .        . .       .   .
8541751@nessie.cc.wwu.edu   |    .             .         .     .   .     .

boutell@brahms.udel.edu (Thomas B Boutell) (02/16/91)

In article <3188@unccvax.uncc.edu> cs00chs@unccvax.uncc.edu (charles spell) writes:
>
>If TSR's must be converted to .COM, why? I know that there are some .EXE
>files launch a TSR, how? Thanx in advance.

No, it is not necessary to convert TSRs to .COM. The problem is, or was, that
under DOS versions prior to 2.0, the only way to leave a TSR in memory was
a system call that would allocate only up to one 64k segment. .COM files must
fit within one 64k region, I believe. (Sorry, I can't find my DOS bible;
somebody borrowed it about a year ago. )-: Otherwise I'd give you the
interrupt number.) However, in versions of DOS from 2.0 on up, there is a
call that will allocate as many pages (16- byte increments) as you wish,
and of course nobody seriously uses versions of DOS prior to 2.0 any longer.
Nonetheless a lot of folks still use the earlier call.

resnicks@netcom.COM (Steve Resnick) (02/16/91)

In article <3188@unccvax.uncc.edu> cs00chs@unccvax.uncc.edu (charles spell) writes:
>
>If TSR's must be converted to .COM, why? I know that there are some .EXE
>files launch a TSR, how? Thanx in advance.
>
There is nothing which says that a TSR must be made a .COM file. In fact,
some code simply cannot be made into a .COM file (any code which the linker
is required to generate fixups on will not be able to be made a .COM)

Making an EXE TSR is a little more complicated, in that you need to know
where the code segment is relative to the PSP, and how  many paragraphs the 
TSR is going to take when you make the DOS call. I don't think the INT 27H
TSR interrupt will work for this, but rather DOS function 31H will do the TSR
in this "style'

Hope this helps ....
Steve

-- 
-------------------------------------------------------------------------------
resnicks@netcom.com, apple!camphq!105!steve.resnick, IFNA:	1:143/105.0, 
USNail: 530 Lawrence Expressway, Suite 374 
        Sunnyvale, Ca 94086
- In real life: Steve Resnick. Flames, grammar and spelling errors >/dev/null
0x2b |~ 0x2b, THAT is the question.
The Asylum OS/2 BBS - (408)263-8017 12/2400,8,1 - Running Maximus CBCS 1.2
-------------------------------------------------------------------------------

david@csource.oz.au (david nugent) (02/17/91)

In <18798@brahms.udel.edu> boutell@brahms.udel.edu (Thomas B Boutell) writes:

> .COM files must fit within one 64k region, I believe. (Sorry, I can't 
> find my DOS bible; somebody borrowed it about a year ago. )-: Otherwise
> I'd give you the interrupt number.) 

INT 27H.

.COM files must be < 64K for two reasons:

	1) SS==DS==ES==CS on startup.  That means that your stack pointer is
	   pointing into your code segment at the top of the first 64K, and
	   can randomly trash stuff there due to hardware interrupts.  In
	   reality, the limit is something less than 64K, dependant on the
	   stack usage of ISR handlers, and maybe a little dependant on
	   luck. :-)

	2) DOS will simply not load a .COM file larger than 64K.  It's
	   easily possible to produce one (though most programming tools -
	   specifically linkers - tend to discourage it), but it won't
	   be too successful when it comes to executing it.
	   
	   And I'm talking about REAL .COM format binaries, not just .EXE's
	   which are renamed to .COM.  .EXE format is already known by DOS
	   and is tested for prior loading any binary file for execution,
	   despite it's extension which is why it's possible to rename a
	   .COM to a .EXE with no serious effects other than changing the
	   "execution priority" from the command line of that file.


> However, in versions of DOS from 2.0 on up, there is a call that will 
> allocate as many pages (16- byte increments) as you wish, and of course 
> nobody seriously uses versions of DOS prior to 2.0 any longer.

Just for reference, INT 21H, function 31H.

One important aspect of using this function is that you can pass an
errorlevel back to the parent, which is not possible with INT 27H.


	david
-- 
        Fidonet: 3:632/348   SIGnet: 28:4100/1  Imex: 90:833/387
              Data:  +61-3-885-7864   Voice: +61-3-826-6711
 Internet/ACSnet: david@csource.oz.au    Uucp: ..!uunet!munnari!csource!david

david@csource.oz.au (david nugent) (02/17/91)

In <24314@netcom.COM> resnicks@netcom.COM (Steve Resnick) writes:

> Making an EXE TSR is a little more complicated, in that you need to know
> where the code segment is relative to the PSP, and how  many paragraphs the 
> TSR is going to take when you make the DOS call. I don't think the INT 27H
> TSR interrupt will work for this, but rather DOS function 31H will do the TSR
> in this "style'


INT 27H will still work alright, provided you have CS set to the program's
PSP.  That's often a little hard to arrange in a .EXE format, but it's
nevertheless possible.  With an easier alternative around, it's no wonder
that that sort of contortion is rarely, if ever, seen.


	david

-- 
        Fidonet: 3:632/348   SIGnet: 28:4100/1  Imex: 90:833/387
              Data:  +61-3-885-7864   Voice: +61-3-826-6711
 Internet/ACSnet: david@csource.oz.au    Uucp: ..!uunet!munnari!csource!david

mcl9337@aim1.tamu.edu (MARK CHRISTOPHER LOWE) (02/18/91)

In article <1991Feb16.061311.22010@unicorn.cc.wwu.edu>n8541751@unicorn) writes:
>A good book which explains this subject further is:
>
>DOS Programmer's Reference by Terry R. Dettmann, QUE books,
>ISBN0-88022-327-8, LC 87-60811.

This book is actually in its second edition now (I just bought it last week).
It is, as you say, a very good book.  I only hope they keep it going when the
much-rumored DOS 5.0 is released!  I'm sure there will be many changes from
earlier versions that we will want to know how to work with.

DOS Programmer's Reference, 2nd Edition - Terry Dettmann and Jim Kyle, QUE
ISBN:0-88022-458-4, LC 89-61067

MC"B!"L

IO92203@MAINE.BITNET (Scott Maxell) (02/18/91)

      TSRs must be a .COM file if I recall, because of the way DOS loads
COM vs. .EXE. A .COM file is a memory image, and is loaded in the same
place in memory. An .EXE file can be loaded anywhere in memory. I don't have
my tech reference at the moment, or I would give you a more detailed
explanation.
 
//////////////////////////////////////////\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
+---------+ Scott Maxell           -- IO92203 @ maine
|         | Pres. MaxSoft Software -- IO92203 @ maine.maine.edu
|    O    |
|    |    | "What I need is a computer that will do what I want it to do
+---------+ not what I tell it to do..."

brandis@inf.ethz.ch (Marc Brandis) (02/19/91)

In article <91049.093800IO92203@MAINE.BITNET> IO92203@MAINE.BITNET (Scott Maxell) writes:
>
>      TSRs must be a .COM file if I recall, because of the way DOS loads
>COM vs. .EXE. A .COM file is a memory image, and is loaded in the same
>place in memory. An .EXE file can be loaded anywhere in memory. I don't have
>my tech reference at the moment, or I would give you a more detailed
>explanation.
> 

No, TSRs can be .EXE files. The only thing that is different is that int 27h
cannot be used to terminate and stay resident. Instead, one of the int 21h
functions (sorry, I do not remember the number) has to be used, and the size
of the area to keep resident in paragraphs has to be computed. Of course,
at the place where the interrupt handler gets control, you have to make sure
that your segment registers contain correct values, but this is not very
different from what you have to do in .COM type files anyway.


Marc-Michael Brandis
Computer Systems Laboratory, ETH-Zentrum (Swiss Federal Institute of Technology)
CH-8092 Zurich, Switzerland
email: brandis@inf.ethz.ch

resnicks@netcom.COM (Steve Resnick) (02/21/91)

In article <1014@csource.oz.au> david@csource.oz.au (david nugent) writes:
>In <24314@netcom.COM> resnicks@netcom.COM (Steve Resnick) writes:
>
>> Making an EXE TSR is a little more complicated, in that you need to know
>> where the code segment is relative to the PSP, and how  many paragraphs the 
>> TSR is going to take when you make the DOS call. I don't think the INT 27H
>> TSR interrupt will work for this, but rather DOS function 31H will do the TSR
>> in this "style'
>
>
>INT 27H will still work alright, provided you have CS set to the program's
>PSP.  That's often a little hard to arrange in a .EXE format, but it's
>nevertheless possible.  With an easier alternative around, it's no wonder
>that that sort of contortion is rarely, if ever, seen.
>
INT 27H will work, ONLY if these conditions are met:

	The total code is <64K
	The code segment is within the area kept resident by int 27H.

The latter can be tricky if the code segment is different than the 
PSP segment, which is usually the case with .EXE programs. Remember
that int27H will keep only 1 segment resident, whereas INT 21H, AH=31H
will keep any number of paragraphs (16 byte chunks) resident, up to
available memory. This makes it much easier to make an EXE TSR..

Cheers!
Steve

-- 
-------------------------------------------------------------------------------
resnicks@netcom.com, apple!camphq!105!steve.resnick, IFNA:	1:143/105.0, 
USNail: 530 Lawrence Expressway, Suite 374 
        Sunnyvale, Ca 94086
- In real life: Steve Resnick. Flames, grammar and spelling errors >/dev/null
0x2b |~ 0x2b, THAT is the question.
The Asylum OS/2 BBS - (408)263-8017 12/2400,8,1 - Running Maximus CBCS 1.2
-------------------------------------------------------------------------------