Path: utzoo!utgpu!jarvis.csri.toronto.edu!cs.utexas.edu!sun-barr!newstop!sun!imagen!qmsseq!pipkins From: pipkins@qmsseq.imagen.com (Jeff Pipkins) Newsgroups: comp.sys.ibm.pc Subject: Re: Problems with TSR Keywords: TSR Message-ID: <118@qmsseq.imagen.com> Date: 22 Feb 90 20:23:22 GMT Reply-To: pipkins@imagen.com (Jeff Pipkins) Organization: QMS Inc., Mobile, Alabama Lines: 58 In-Reply-To: <386@poppy.warwick.ac.uk> In article <386@poppy.warwick.ac.uk> you write: >HELP required! > >I am trying to write a Command Line Editor for my AT and it almost works! >[...] >I use the MSC 4.0 C compiler and MASM 4.0 Assembler. I have written the >routines to do the cursor handling, printing and tsr bit in assembly >language and everything else is in C. >In order to find the end of the program I use : > xor ax,ax > push ax > call _sbrk > [...] > >As I mentioned this seems to be ok ?!? I use malloc to allocate storage for >the commands so I can have a history of the commands I've used. This works >fine for DOS internals. [...] The problem seems to be that you have free the memory that your heap is in. malloc still works (why?), but your heap no longer belongs to you! This is "bad". >From the movie GhostBusters: Egon: I forgot to tell you -- we should never cross the beams. That could be "bad". Bill Murry's character: I'm a little fuzzy on this "good"/"bad" thing. What do you mean by "bad"? Egon: Well, imagine every molecule in your body simultaneously exploding at the speed of light... Bill Murry's character: Got it. Don't cross the beams, gentlemen. The man says that could be bad. [end of excerpt]. Suggestion on what to do about it: First of all, don't use sbrk(). A better way to find out the end of your memory block is to look at the word at psp:2. This will tell you the segment address of the first paragraph beyond the memory block your program is in. The problem with that is that if the block is expanded (by malloc), this field in the psp is not automatically updated. A reliable way to tell how big the memory block is, is to look at its memory control block. The word at segment (psp-1) and offset 3 will tell you the number of paragraphs in your program's memory block, even if malloc has extended it. Use this value to tell the TSR call how many paragraphs to keep. If you place the source in the public domain or put it out as shareware, I would be interested in getting a copy for the sake of curiosity. Hope this helps, Jeff D. Pipkins pipkins@imagen.com