From: spc@pineal.sci.fau.edu (Sean 'Captain Napalm' Conner)
Subject: Re: TASM and TLINK .EXE generation
Organization: Florida Atlantic University, but who cares?
Date: Mon, 28 Sep 1992 17:40:17 GMT

In article <1992Sep28.143953.29969@ncsu.edu> dspascha@eos.ncsu.edu (DAVID SCOTT PASCHAL) writes:
>
>In article <1992Sep28.072710.15731@lugb.latrobe.edu.au>, 9125113g@lux.latrobe.edu.au (Mitch Davis) writes:
>|>In article <1992Sep25.183508.12907@ncsu.edu> dspascha@eos.ncsu.edu (DAVID SCOTT PASCHAL) writes:
>|> > I don't know about TLINK, but with the Microsoft Linker there's a
>|> > command-line switch to change the maximum allocation.
>|> 
>|>Do you know what switch it is?  Once again I've looked over the TLINK
>|>options, and I can't see it.
>
>Something like /MAXALLOC I think.
>
>|> 
>|> > Alternatively, you could use the DOS function to resize a memory
>|> > block (I think function 4Ah) to dynamically size-down your executable
>|>> at run-time.
>|> 
>|>That's the right function, but I'd need to know which segment (PSP, DS,
>|>CS, etc) to throw at it, and how do I easily work out the new size?
>
>Give it the PSP segment value.  If you're writing .COM programs it's easy to
>figure out the size, but for .EXE programs it's a little bit harder.  In my
>.COM programs I always say something like
>		mov		bx,(offset Eof-offset Start+100h)/16+1
>which is really (offset Eof)/16+1 but MASM won't let me because it doesn't like
>doing anything other than addition or subtraction on references to absolute
>label addresses which have to get passed to the linker to get resolved.
>The label Eof is just a dummy label I put after the last instruction, and the
>label Start is what I put right after the ORG 100h.  At the end of the source
>file I say
>		end		Start
>
>Since my assembler programs haven't (yet) gotten so big that they need to be
>.EXE files, I've never had to figure out how to calculate the size to pass to
>the DOS reallocate function, but try this: the paragraph difference between
>the segment address of your last segment and the PSP segment address, PLUS
>the number of paragraphs in your last segment rounded UP to the next paragraph
>value, PLUS any additional space you need (in paragraphs).  If you're trying
>to write an .EXE TSR this can get kind of tricky if you want it to chuck your
>initialization code and data segments when you go resident.  What you could do
>is put your resident segment(s) first and then your initialization segments.
>
>Tschuess,
>David Paschal

  To resize the memory block for an EXE file under MSDOS, I do the
following:

		mov	bx,zzlast		;zzlast is a segment - see
						;below
		mov	ax,es			;es (on entry to an EXE
						;program, contains the PSP
		sub	bx,ax			;size of program (about) in 
						;paragraphs (segments)
		inc	bx			;adjust just in case
	
		.				;rest of code ...
		.
		.

;**********
zzlast		segment
zzlast		ends
;**************

  Because of the way MASM and LINK order segments (I think it's
alphabetical, but it may be something else ...) zzlast is always the last
segment of the program, and the above code seems to work okay.  You may have
to play around with the mov bx,zzlast line a bit (I've had problems with
MASM puking on the line, so I made a macro to force the issue, so to speak,
but that was about a year ago, so ...)

  And I've never made an EXE as a TSR (while I like dabbling in DOS arcana,
writing a TSR as an EXE is a bit arcane for me 8-)

  -spc (Oh Great MASM, please accept this code as a sacrafice from your	
	unworthy servant .... 8-)