home *** CD-ROM | disk | FTP | other *** search
/ Simtel MSDOS 1992 December / simtel1292_SIMTEL_1292_Walnut_Creek.iso / msdos / pcmag / vol6n07.arc / READENV.ASM < prev    next >
Assembly Source File  |  1987-12-13  |  11KB  |  236 lines

  1. ;       READENV.ASM -- Reads Environment Strings
  2. ;       --------------------------------------------------
  3.  
  4. ;This program reads the Environment strings to demonstrate:
  5. ;   1. How to find and print each of the strings in the Environment
  6. ;   2. How to search for and find an Environment string by
  7. ;           means of its variable name
  8.  
  9. ;Program structure:
  10. ;   1. Read the entire Environment in a loop
  11. ;           1. Copy each string to a buffer
  12. ;           2. 0 terminate it
  13. ;           3. Print it
  14. ;   2. Find a particular string by variable name
  15. ;   3. Print it
  16.  
  17. CSEG            Segment
  18.                 Assume  CS:CSEG, DS:CSEG, ES:CSEG, SS:CSEG
  19.                 Org     100h                    ; For COM file
  20. Entry:          Jmp     Main
  21.  
  22. ;All data is here:
  23. notice                  db 'Copyright 1986 Ziff-Davis Publishing Co.'
  24. notice2                 db 'Programmed by Richard Hale Shaw'
  25. Environment_Offset      Equ     02ch
  26. MAXIMUM_ENV_STRING      Equ     127
  27. String_prefix           db      0Dh,0Ah
  28. Env_String_Buffer       db      MAXIMUM_ENV_STRING+2 dup(1)
  29. Env_Offset              dw      0
  30. Environment             dw      0
  31. Variable_to_find        db      'path',0
  32.  
  33. ;
  34. ;---------------------------------------------------
  35. ;
  36. Main:
  37.                 Call    Set     ; Call subroutine to imitate DOS SET
  38.  
  39.                 ; Next, find "PATH" and display entire string if found
  40.                 ; "PATH" is stored at Variable_to_find
  41.  
  42.                 Lea     SI,Variable_to_find     ; Address of variable name
  43.                                                 ; to find into SI
  44.                 Call    Getvar                  ; Get the variable
  45.                 Cmp     CX,0                    ; If CX is 0, not found, so
  46.                 Je      Exit                    ; Get out of program
  47.                 Call    Print_Env_string        ; Else print the string found
  48.  
  49. Exit:
  50.                 Int     20h                     ; Exit program
  51.  
  52.  
  53. ; Subroutines: ----------------------------------------------------------------
  54.  
  55.  
  56. Set:            ; Like the DOS SET command, reads each environment string
  57.                 ; and prints each one out
  58.  
  59.                 Mov     Word Ptr Env_offset,0   ; Set to search Environment
  60.                                                 ; From the beginning
  61.  
  62. Find_string:    Call    Find_Env_String         ; Find a string
  63.                 Cmp     CX,0                    ; If CX is 0
  64.                 Je      End_set                 ; No string found, go ahead
  65.                 Call    Print_Env_string        ; else Call Print Environment
  66.                                                 ; string routine
  67.                 Jmp     Find_string             ; Try for another string
  68. End_Set:        Ret                             ; Return to caller
  69.  
  70.  
  71. ;-----------------------------------------------------------------------------
  72. Getvar:
  73.     ; requires SI to point to NULL-terminated variable to find
  74.     ; if CX is 0 upon return, the string was not found
  75.  
  76.                 Mov     Word Ptr Env_offset,0   ; Set to search Environment
  77.                                                 ; From the beginning
  78.  
  79.                 Call    String_length   ;Get length of variable to find
  80.                                         ; Pointed to by SI
  81.                 Cmp     CX,0            ; If length of string to compare is 0
  82.                 Je      End_getvar      ; No variable to compare
  83.                 Call    Upper           ; Upper case the variable
  84.  
  85. Another_string:
  86.                 Push    CX                      ; Save CX (length of variable)
  87.                 Push    SI                      ;Save SI (Address of variable)
  88.                 Call    Find_env_string         ; Find a string
  89.                 Pop     SI                      ; Get it back from stack
  90.                 Cmp     CX,0                    ; If CX is 0
  91.                 Pop     CX                      ; Restore CX from stack
  92.  
  93.                 Je      Not_found               ; String not found
  94.  
  95.                 Cld                             ; Clear direction flag
  96.                 Push    SI                      ; Save Address of variable
  97.                 Push    ES                      ; Save ES on stack
  98.                 Push    DS                      ; Save DS on stack
  99.                 Pop     ES                      ; Put DS into ES
  100.                 Lea     DI,Env_string_buffer    ; Address of buffer into DI
  101.                 Push    CX                      ; Save length on stack
  102.                 Repe Cmpsb                      ; Compare the two strings
  103.                 Pop     CX                      ; Restore length
  104.                 Pop     ES                      ; Restore ES
  105.                 Pop     SI                      ; Restore variable address
  106.                 Jne     Another_string
  107.                 Cmp     Byte Ptr[DI],'='        ; If '=' next character
  108.                 Je      End_getvar              ;Got it, length in CX, get out
  109.  
  110. Not_found:
  111.                 Xor     CX,CX                   ; Don't have it, clear CX
  112.  
  113. End_getvar:     Ret                             ; Return to caller
  114.  
  115. ;-----------------------------------------------------------------------------
  116. String_length:
  117.     ; requires SI to point to string to get length of
  118.     ; Length of string in CX on return
  119.     ; String should be NULL-terminated
  120.  
  121.                 Push    SI                      ; Save SI on stack
  122.                 Xor     CX,CX                   ; Clear CX to 0
  123. Next_one:
  124.                 Cmp     Byte Ptr[SI],0          ; If 0 found
  125.                 Je      End_string              ; Go to End string
  126.                 Inc     CX                      ; Add 1 to count in CX
  127.                 Inc     SI                      ; Next byte to compare
  128.                 Jmp     Next_one                ; Compare next one
  129. End_string:
  130.                 Pop     SI                      ; Restore SI from stack
  131.                 Ret                             ; Return to caller
  132.  
  133. ;-----------------------------------------------------------------------------
  134. Print_Env_string:
  135.     ; Prints NULL-terminated Environment string pointed to by  
  136.     ; Env_string_buffer
  137.     ; Calls Int 21h function 2 to print each character
  138.     ; Terminates when NULL-terminater is reached
  139.  
  140.                 Push    SI                      ; Save SI on stack
  141.                 Lea     SI, String_prefix       ;Load address of buffer into AX
  142. Print_another:
  143.  
  144.                 Mov     DL,Byte Ptr[SI]         ; Character to print into DL
  145.                 Cmp     DL,0                    ; If character is Zero
  146.                 Je      End_Print_Env_string    ; Get out
  147.                 Mov     AH,2                    ; Function to print string
  148.                 Int     21h                     ; Call DOS
  149.                 Inc     SI                      ; Point to next character
  150.                 Jmp     Print_another           ; Loop back
  151.  
  152. End_Print_Env_string:
  153.                 Pop     SI                      ; Restore SI
  154.                 Ret                             ; Return to caller
  155.  
  156. ;-----------------------------------------------------------------------
  157. Find_Env_String:
  158.     ; Finds an Environment string beginning with the first
  159.     ; One.  Copies each to the Env_string_buffer.  If End
  160.     ; of Environment found, places Zero in buffer, and CX
  161.     ; is 0.
  162.  
  163.                 Cmp     Word Ptr Environment,00 ; See if Environment address
  164.                                                 ; is available
  165.                 Ja      Get_Env_string          ; If Available, Get a string
  166.  
  167.                                                 ; If not,
  168.                 Mov     AX,CS:[2Ch]             ; Get Environment address
  169.                 Mov     Word Ptr Environment,AX ; Store Environment address
  170.  
  171. Get_Env_string:
  172.                 Lea     DI,Env_string_buffer    ; Address of buffer into DI
  173.                 Mov     SI,Word Ptr Env_offset  ; Current Environment Offset
  174.                                                 ; into SI
  175.                 Mov     AX,DS                   ; Get DS into AX
  176.                 Mov     ES,AX                   ; And put it in ES
  177.                 Mov     AX,Word Ptr Environment ;Get Environment Segment Addr.
  178.                 Mov     DS,AX                   ; Put it into DS
  179.                 Xor     CX,CX                   ; Clear CX to 0
  180.                 Push    SI                      ; Save SI on stack
  181.  
  182. Next_byte:
  183.                 Cmp     Byte Ptr[SI],00         ; Check for End of Environment
  184.                                                 ; String
  185.                 Je      End_env_string          ; If found, break out of loop
  186.                 Inc     CX                      ; Add 1 to count
  187.                 Inc     SI                      ; Point to next byte
  188.                 Jmp     Next_byte               ; Loop back to check next byte
  189.  
  190. End_env_string:
  191.                 Pop     SI                      ; Restore SI from stack
  192.                 Cmp     CX,0                    ; If CX is 0
  193.                 Je      End_Find_Env_string     ; Nothing to move, goto finish
  194.  
  195. Move_bytes:
  196.                 Push    CX                      ; Save count on stack
  197.                 Mov     AX,SI                   ; Put SI offset into AX
  198.                 Add     AX,CX                   ; Add number of bytes found
  199.                 Inc     AX                      ; Add one
  200.  
  201.                 Mov     Word Ptr ES:Env_offset,AX     ; Set Env_offset for
  202.                                                 ; current Environment offset
  203.  
  204.                 Cld                     ; Clear direction flag for forward move
  205.                 Rep Movsb               ; Copy Bytes from Environment to buffer
  206.  
  207.                 Mov     Byte Ptr ES:[DI],0      ; Set last byte to zero
  208.                 Pop     CX                      ; Restore count from stack
  209.  
  210. End_Find_Env_string:
  211.                 Mov     AX,ES                   ; Set Data Segment back into
  212.                 Mov     DS,AX                   ; DS
  213.                 Ret                             ; Return to caller
  214.  
  215. ;-----------------------------------------------------------------------------
  216. Upper:
  217.     ; Uppercases the Null-terminated string pointed to by SI
  218.  
  219.                 Push    SI                      ; Save SI Pointer
  220.  
  221. Upper_Another:  Cmp Byte Ptr[SI],0              ; If NULL
  222.                 Je      End_upper               ; Get out
  223.                 Xor     Byte Ptr[SI],32         ; Uppercase the character
  224.                 Inc     SI                      ;Increment SI to next character
  225.                 Jmp     Upper_Another           ; Uppercase the next character
  226.  
  227. End_upper:      Pop     SI                      ; Restore SI from stack
  228.                 Ret
  229.  
  230. ;-----------------------------------------------------------------------------
  231. EndProg         Label   Byte                    ; End of program
  232. CSEG            Ends
  233.                 End Entry
  234.  
  235. ; End of Readenv.asm
  236.