home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / PASCAL / BMULKY.ZIP / ENV.ASM < prev    next >
Encoding:
Assembly Source File  |  1988-04-08  |  4.2 KB  |  104 lines

  1. ;  Environment variable routines assembler interface to Turbo Pascal
  2. ;  Copyright 1988 by Mark R. Boler  -  All Rights Reserved
  3.  
  4. TITLE     ENV
  5.  
  6. DATA      SEGMENT WORD PUBLIC
  7.  
  8.           ASSUME  DS:DATA
  9.  
  10.           EXTRN   PREFIXSEG: WORD
  11.  
  12. DATA      ENDS
  13.  
  14. CODE      SEGMENT BYTE PUBLIC
  15.  
  16.           ASSUME  CS:CODE
  17.  
  18.           PUBLIC  EnvFirst, EnvNext
  19.  
  20. ; PROCEDURE EnvFirst(VAR St: STRING;
  21. ;                    VAR EnvData;
  22. ;                    VAR Status: WORD); EXTERNAL;
  23.  
  24. ; PROCEDURE EnvNext (VAR St: STRING;
  25. ;                    VAR EnvData;
  26. ;                    VAR Status: WORD); EXTERNAL;
  27.  
  28. EnvDest   EQU     DWORD PTR [bp + 14]
  29. EnvData   EQU     DWORD PTR [bp + 10]
  30. Status    EQU     DWORD PTR [bp + 6]
  31.  
  32. MaxStrLen EQU     255                  ; maximum length of turbo string
  33. EnvOfs    EQU     [2CH]                ; offset into the PSP of env segment
  34.  
  35. ; If an error occured, Status returns a non-zero error code
  36. ; possible values for Status are:
  37. ;
  38. ;  0 - No error.  A valid string is returned in St
  39. ;  1 - No more environment strings.  String St is not altered
  40. ;
  41. ; WARNING: NEVER NEVER call EnvNext without first calling EnvFirst
  42. ;          AND passing the valid EnvData block returned from EnvFirst
  43. ;          undetermined results occur if passing an invalid EnvData
  44. ;          pointer.  The EnvData block is a 4 byte pointer so if your
  45. ;          program knows the address of the environment it can pass
  46. ;          that address to EnvNext without using EnvFirst.
  47. ;          NEVER call EnvNext if Status was returned non-zero.
  48.  
  49. EnvFirst  PROC    FAR
  50.           push    bp                   ; save bp
  51.           mov     bp, sp               ; set up stack frame
  52.           push    ds                   ; save ds
  53.           mov     ds, PREFIXSEG        ; get PSP segment into ds
  54.           mov     ds, ds:EnvOfs
  55.           xor     si, si               ; si = 0
  56. ComStrt:
  57.           xor     dx, dx               ; clear out dx - Status = 0
  58.           cld                          ; string ops go forward
  59.           lodsb                        ; see if first byte is Nul
  60.           or      al, al               ; if so, there is a null string
  61.           jnz     SHORT GetStr         ; else go test this string
  62.           lodsb
  63.           or      al, al               ; see if this one is null also
  64.           jnz     SHORT GetStr         ; if not, then get a string
  65.           inc     dx                   ; end of environment Status = 1
  66.           jmp     SHORT Done           ; return error
  67. GetStr:
  68.           les     bx, EnvDest          ; es:bx points to destination
  69.           mov     di, bx               ; es:di points to destination also
  70.           inc     di                   ; point to the first character in Dest
  71.           mov     cx, MaxStrLen        ; cx = maximum length of string
  72.           mov     ah, cl               ; ah = same
  73. Loop1:
  74.           stosb                        ; put the character in destination
  75.           lodsb                        ; get a character
  76.           or      al, al               ; see if its a null
  77.           loopnz  Loop1                ; if not done then loop
  78.           sub     ah, cl               ; compute length of string
  79.           mov     es:[bx], ah          ; set the length byte
  80.           les     bx, EnvData          ; get the data buffer
  81.           dec     si                   ; save offset of last character tested
  82.           mov     es:[bx], si
  83.           mov     es:[bx + 2], ds      ; save segment of environment
  84. Done:
  85.           lds     bx, Status           ; get address of Status
  86.           mov     ds:[bx], dx          ; put value into Status
  87.           pop     ds                   ; restore ds
  88.           pop     bp                   ; restore bp
  89.           ret     12
  90. EnvFirst  ENDP
  91.  
  92. EnvNext   PROC    FAR
  93.           push    bp                   ; save bp
  94.           mov     bp, sp               ; set up stack frame
  95.           push    ds                   ; save ds
  96.           lds     si, EnvData          ; get pointer to environment
  97.           lds     si, ds:[si]          ; make ds:si => next environment char
  98.           jmp     SHORT ComStrt        ; go process it
  99. EnvNext   ENDP
  100.  
  101. CODE      ENDS
  102.  
  103.           END
  104.