[comp.os.msdos.programmer] Help in relocating a program in DOS memory

crystal@eleazar.dartmouth.edu (Richard Brittain) (06/21/91)

Hello,
   I'm trying to make a small (actually tiny model) C program relocate
itself to the top of the 640k memory space in order to prevent fragmentation
later on.  What I naively tried was:
 - find out how big the current program is from the MCB
 - set the DOS allocation strategy to last-fit
 - allocate the same size block at the top of memory
 - deallocate my own environment since I don't need it
 - copy myself from here to there
 - deallocate myself
 - set all the segment registers equal to the new segment address + 100h

I crash and burn at the last two steps.  The program has no separately allocated
memory, is tiny model and has no hooked interrupts.  What else do I need to
do - any suggestions?
The better way to do this of course would be for DOS to respect the exe
header blocks saying how much memory a program needs and then allocate only
that much to it - I presume setting last-fit strategy before doing a spawn()
would then achieve what I'm trying to do.  Is there some magic flag to make
the exec service work like this ?

Thanks a lot,

Richard Brittain,

richard@einstein.dartmouth.edu

pshuang@athena.mit.edu (Ping-Shun Huang) (06/24/91)

In article <1991Jun21.111320.3481@dartvax.dartmouth.edu> crystal@eleazar.dartmouth.edu (Richard Brittain) writes:

 >    I'm trying to make a small (actually tiny model) C program relocate
 > itself to the top of the 640k memory space in order to prevent fragmentation

What kind of fragmentation are you trying to avoid?  Are you attempting
to make your C program a TSR?

 >  - copy myself from here to there
 >  - deallocate myself
 >  - set all the segment registers equal to the new segment address + 100h

One possible reason for your program to be crashing and burning might be
that it is an .EXE file.  You specified that it is a tiny model C
program, but have you used EXE2BIN on it to convert it to a .COM file?
If not, what's probably happening is that your program when loaded by
DOS, is automatically patched (with information from the .EXE header) so
that memory pointer variables are corrected for the memory location your
program is now in.  In doing a memcpy, you are *NOT* correcting these
addresses, even though you are resetting the segment registers.

 > The better way to do this of course would be for DOS to respect the exe
 > header blocks saying how much memory a program needs and then allocate only
 > that much to it - I presume setting last-fit strategy before doing a spawn()
 > would then achieve what I'm trying to do.

I think DOS *DOES* respect the .EXE header information in how much
memory should be allocated to a program.  However, the last-fit strategy
does *NOT* include taking the last-fit block, in this case most likely
all the space between memory already used by DOS and TSR's and the 640Kb
boundary (and is probably bigger than what your program wants/needs),
and chopping it into pieces just because your program asked for less
space than is in that block.

--
Above text where applicable is (c) Copyleft 1991, all rights deserved by:
UNIX:/etc/ping instantiated (Ping Huang) [INTERNET: pshuang@athena.mit.edu]