home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / assemblr / library / environm / mastrenv.asm next >
Assembly Source File  |  1988-06-05  |  4KB  |  96 lines

  1. ==========
  2. tech.notes/pc.code #29, from pmaupin, 3407 chars, Sat Jun  4 22:40:45 1988
  3. ----------
  4. TITLE: Finding DOS's master environment pointer
  5. This is a fragment of code that my SD.COM program uses to find
  6. the environment.  This fragment is different than most ways of
  7. finding the environment, in that it finds the MASTER environment block,
  8. not the current process's parent's environment.
  9.  
  10. This is useful in some cases, and has the added advantage that
  11. it does NOT behave differently when executing under CodeView,
  12. so you do NOT have to hard-code your system's DOS environment address
  13. into your program in order to debug it.
  14.  
  15.  
  16. EnvPtr             EQU       2CH       ; Offset in PSP
  17.  
  18. CommandInterrupt   EQU       2EH       ; entry point into first Command.Com
  19.                                        ; through interpreter
  20.  
  21. DosSegPtr          EQU       CommandInterrupt * 4 + 2
  22.  
  23.  
  24. ; FindEnvironment is passed:
  25.  
  26. ;   DS should point to program PSP
  27.  
  28. ; FindEnvironment returns:
  29.  
  30. ;   ES points to master environment block, or program's copy if couldn't
  31. ;              find the master.
  32.  
  33. ;   CX is length of block, or 0 if couldn't find the master.
  34.  
  35. ; FindEnvironment destroys:
  36.  
  37. ;   AX, SI
  38.  
  39.  
  40. FindEnvironment    PROC  NEAR
  41.                    xor   si,si                ; Point to segment 0
  42.                    mov   es,si
  43.                    mov   si, word ptr es:[DosSegPtr]
  44.                    mov   ax,si
  45.                    call  VerifyBlock          ; make sure we've found COMMAND
  46.                    jnz   GotBlock             ; jump if not a good block --
  47.                                               ; use process's environment
  48.  
  49.                    mov   ax,es:[EnvPtr+10h]   ; get COMMAND's environment ptr
  50.                    or    ax,ax                ; jump if COMMAND has a
  51.                    jnz   MaybeGoodBlock       ; subsidiary environment
  52.  
  53.                    mov   ax,si                ; If no subsidiary, just use
  54.                    add   ax,cx                ; the allocation block
  55.                    inc   ax                   ; immediately after COMMAND
  56.  
  57. MaybeGoodBlock:    call  VerifyBlock          ; verify that we have a good
  58.                                               ; one, one way or another
  59. GotBlock:
  60.                    shl   cx,1                 ; multiply by 16 to get
  61.                    shl   cx,1                 ; length in bytes
  62.                    shl   cx,1
  63.                    shl   cx,1
  64.                    mov   es,ax
  65.                    ret
  66.  
  67.  
  68. ; VerifyBlock tries to insure that we're pointing to a valid DOS
  69. ; allocation block.  If not, returns the current process's environment
  70. ; block.
  71.  
  72.  
  73. VerifyBlock        PROC  NEAR
  74.                    dec   ax                      ; get block header into ES
  75.                    mov   es,ax
  76.                    inc   ax
  77.  
  78.                    cmp   byte ptr es:[0],04Dh    ; make sure signature is valid
  79.                    jnz   UseCurrent
  80.                    cmp   word ptr es:[1],si      ; make sure owner is valid
  81.                    jnz   UseCurrent
  82.                    mov   cx, word ptr es:[3]     ; retrieve the length
  83.                    ret
  84.  
  85. UseCurrent:        mov   ax,word ptr ds:[EnvPtr] ; get current process's env
  86.                    xor   cx,cx                   ; zero length
  87.                    ret
  88. VerifyBlock        ENDP
  89.  
  90. FindEnvironment    ENDP
  91.  
  92. So far, this seems to work.  I would welcome any feedback on its
  93. efficacy, but if the feedback is negative, please give the DOS version
  94. and a detailed problem description.  Thanks,
  95. Pat
  96.