home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c081_11 / 9.ddi / CHAPXMPL.ZIP / ENVSTR.ASM < prev    next >
Encoding:
Assembly Source File  |  1991-02-13  |  3.8 KB  |  84 lines

  1. ; Turbo Assembler    Copyright (c) 1988, 1991 By Borland International, Inc.
  2.  
  3. ; ENVSTR.ASM - Example program to scan the DOS environment
  4.  
  5. ; From the Turbo Assembler Users Guide - Interfacing Turbo Assembler with
  6. ;                                          Turbo Pascal
  7.  
  8. DATA      SEGMENT PUBLIC
  9.           EXTRN prefixSeg : Word       ;gives location of PSP
  10. DATA      ENDS
  11. CODE      SEGMENT PUBLIC
  12.           ASSUME cs:CODE,ds:DATA
  13.  
  14. EnvString PROC FAR
  15.           PUBLIC  EnvString
  16.           push    bp
  17.           cld                          ;work upward
  18.           mov     es,[prefixSeg]       ;look at PSP
  19.           mov     es,es:[2Ch]          ;ES:DI points at environment
  20.           xor     di,di                ;which is paragraph-aligned
  21.           mov     bp,sp                ;find the parameter address
  22.           lds     si,ss:[bp+6]         ;which is right above the return address
  23.           ASSUME  ds:NOTHING
  24.           lodsb                        ;look at length
  25.           or      al,al                ;is it zero?
  26.           jz      RetNul               ;if so, return
  27.           mov     ah,al                ;otherwise, save in AH
  28.           mov     dx,si                ;DS:DX contains pointer to first parm char
  29.           xor     al,al                ;make a zero
  30. Compare:
  31.           mov     ch,al                ;we want ch=0 for next count, if any
  32.           mov     si,dx                ;get back pointer to string sought
  33.           mov     cl,ah                ;get length
  34.           mov     si,dx                ;get pointer to string sought
  35.           repe    cmpsb                ;compare bytes
  36.           jne     Skip                 ;if compare fails, try next string
  37.           cmp     byte ptr es:[di],'=' ;compare succeeded. Is next char '='?
  38.           jne     NoEqual              ;if not, still no match
  39. Found:
  40.           mov     ax,es                ;make DS:SI point to string we found
  41.           mov     ds,ax
  42.           mov     si,di
  43.           inc     si                   ;get past the equal (=) sign
  44.           les     bx,ss:[bp+10]        ;get address of function result
  45.           mov     di,bx                ;put it in ES:DI
  46.           inc     di                   ;get past the length byte
  47.           mov     cl,255               ;set up a maximum length
  48. CopyLoop:
  49.           lodsb                        ;get a byte
  50.           or      al,al                ;zero test
  51.           jz      Done                 ;if zero, we're done
  52.           stosb                        ;put it in the result
  53.           loop    CopyLoop             ;move up to 255 bytes
  54. Done:     not     cl                   ;we've been decrementing CL from
  55.                                        ; 255 during save
  56.           mov     es:[bx],cl           ;save the length
  57.           mov     ax,SEG DATA
  58.           mov     ds,ax                ;restore DS
  59.           ASSUME  ds:DATA
  60.           pop     bp
  61.           ret     4
  62.           ASSUME  ds:NOTHING
  63. Skip:
  64.           dec     di                   ;check for null from this char on
  65. NoEqual:
  66.           mov     cx,7FFFh             ;search a long way if necessary
  67.           sub     cx,di                ;environment never >32K
  68.           jbe     RetNul               ;if we're past end, leave
  69.           repne   scasb                ;look for the next null
  70.           jcxz    RetNul               ;exit if not found
  71.           cmp     byte ptr es:[di],al  ;second null in a row?
  72.           jne     Compare              ;if not, try again
  73. RetNul:
  74.           les     di,ss:[bp+10]        ;get address of result
  75.           stosb                        ;store a zero there
  76.           mov     ax,SEG DATA
  77.           mov     ds,ax                ;restore DS
  78.           ASSUME  ds:DATA
  79.           pop     bp
  80.           ret     4
  81. EnvString ENDP
  82. CODE      ENDS
  83.           END
  84.