[comp.sys.ibm.pc] Problems with TSR

estxrah@warwick.ac.uk (Worm) (02/20/90)

HELP required!

I am trying to write a Command Line Editor for my AT and it almost works!
I do this by intercepting the int 21h 0ah buffered keyboard input routine and
substituting my own routine. All is well with DOS internal commands which are
passed back to DOS ok and get executed correctly. However, external
commands seem to be another matter entirely. The machine just hangs and I
have to reset it using CTRL-ALT-DEL or the reset button.
What am I doing that is wrong or not doing that is right ?

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
        ; Quick check for -1 goes here
        mov     dx,ax
        add     dx,1024 ; This is for stack
        add     dx,15
        shr     dx,1
        shr     dx,1
        shr     dx,1
        shr     dx,1
        mov     ax,3100h
        int     21h

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. I can recall and edit any previously typed command.

If anyone can help then I would very much appreciate it. I will gladly give
any information I can concerning the program.

        Stu

estxrah@warwick.UUCP ** estxrah@uk.ac.warwick.cs ** estxrah@cs.warwick.ac.uk
      "She's not Ben Johnson, but then who is ?" - David Coleman

pipkins@qmsseq.imagen.com (Jeff Pipkins) (02/23/90)

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