home *** CD-ROM | disk | FTP | other *** search
/ Microsoft Programmer's Library 1.3 / Microsoft-Programers-Library-v1.3.iso / sampcode / advos2 / ch03 / showenv.asm < prev    next >
Encoding:
Assembly Source File  |  1988-12-12  |  4.0 KB  |  159 lines

  1.         title    SHOWENV -- demo of GETENV subroutine
  2.     page    55,132
  3.     .286
  4.  
  5. ; SHOWENV.ASM - Demonstrates use of GETENV routine (OS/2 version).
  6. ;        Prompts user for the name of an environment 
  7. ;        variable, then displays the associated string.
  8. ; Copyright (C) 1987 Ray Duncan
  9. ;
  10. ; Build:    MASM SHOWENV;  
  11. ;        MASM GETENV;
  12. ;        LINK SHOWENV+GETENV,,,OS2,SHOWENV
  13. ;
  14. ; Usage:    SHOWENV
  15.  
  16. stdin    equ    0        ; standard input handle
  17. stdout    equ    1        ; standard output handle
  18.  
  19. cr    equ    0dh           ; ASCII carriage return
  20. lf    equ    0ah           ; ASCII line feed
  21.  
  22.     extrn    DosExit:far    ; OS/2 API functions
  23.         extrn    DosRead:far
  24.         extrn    DosWrite:far
  25.  
  26.     extrn    getenv:near    ; returns address of env. string
  27.  
  28. DGROUP    group    _DATA
  29.  
  30. _DATA    segment    word public 'DATA'
  31.  
  32. msg1    db    cr,lf,lf,'Enter name:    '
  33. msg1_len equ $-msg1
  34.  
  35. msg2    db    cr,lf,   'The value is:  '
  36. msg2_len equ $-msg2
  37.  
  38. msg3    db    'not found!'
  39. msg3_len equ $-msg3
  40.  
  41. inbuf    db    64 dup (0)    ; keyboard input buffer
  42. inbuf_len equ $-inbuf
  43.  
  44. rlen    dw    ?        ; receives actual number
  45.                 ; of bytes read
  46.  
  47. wlen    dw    ?        ; receives actual number
  48.                 ; of bytes written
  49.  
  50. _DATA    ends
  51.  
  52.  
  53. _TEXT    segment    word public 'CODE'
  54.  
  55.     assume    cs:_TEXT,ds:DGROUP
  56.  
  57. main    proc    far           ; entry point from OS/2
  58.  
  59.                 ; get env. variable,
  60.                 ; display cue to user...
  61.     push    stdout        ; standard output handle
  62.         push    ds        ; address of message
  63.         push    offset DGROUP:msg1
  64.         push    msg1_len    ; length of message
  65.         push    ds        ; receives bytes written
  66.         push    offset DGROUP:wlen
  67.         call    DosWrite    ; transfer to OS/2
  68.  
  69.                 ; get name of environment
  70.                     ; variable from user...
  71.     push    stdin        ; standard input handle
  72.         push    ds            ; address of input buffer
  73.         push    offset DGROUP:inbuf
  74.         push    inbuf_len    ; length of input buffer
  75.         push    ds            ; receives actual length
  76.         push    offset DGROUP:rlen
  77.     call    DosRead            ; transfer to OS/2
  78.  
  79.     mov    ax,rlen        ; get length of input
  80.     sub    ax,2        ; remove cr-lf characters
  81.     or    ax,ax        ; anything entered?
  82.     jz    main2        ; no, exit
  83.  
  84.     mov    bx,ax        ; append null to string
  85.     mov    byte ptr [bx+inbuf],0
  86.  
  87.                 ; display "The value is:"
  88.     push    stdout        ; standard output handle
  89.         push    ds        ; address of message
  90.         push    offset DGROUP:msg2
  91.         push    msg2_len    ; length of message
  92.         push    ds        ; receives bytes written
  93.         push    offset DGROUP:wlen
  94.         call    DosWrite    ; transfer to OS/2
  95.  
  96.     mov    si,offset inbuf    ; address of ASCIIZ string
  97.     call    strupr        ; fold to upper case
  98.     call    getenv        ; then search environment    
  99.  
  100.     or    ax,ax        ; find anything?
  101.     jz    main1        ; no, display error message
  102.  
  103.                 ; yes, display value of
  104.                     ; environment string
  105.     push    stdout        ; standard output handle
  106.         push    es            ; address of string
  107.         push    di
  108.         push    ax            ; length of string
  109.         push    ds            ; receives bytes written
  110.         push    offset DGROUP:wlen
  111.     call    DosWrite    ; transfer to OS/2    
  112.  
  113.     jmp    main        ; go ask for another
  114.  
  115. main1:                ; env. variable not found
  116.     push    stdout        ; standard output handle
  117.         push    ds        ; address of error message
  118.         push    offset DGROUP:msg3
  119.         push    msg3_len    ; length of message
  120.         push    ds        ; receives bytes written
  121.         push    offset DGROUP:wlen
  122.         call    DosWrite        ; transfer to OS/2
  123.  
  124.     jmp    main        ; go look for another
  125.  
  126. main2:                ; final exit to OS/2
  127.     push    1        ; terminate all threads
  128.         push    0        ; return code = 0
  129.         call    DosExit        ; transfer to OS/2
  130.  
  131. main    endp
  132.  
  133.  
  134. strupr    proc    near        ; convert ASCII string to
  135.                 ;  upper case
  136.                 ; call with DS:SI = string
  137.     push    si        ; save string address
  138.  
  139. strup1:    lodsb            ; next character
  140.     or    al,al        ; found end (null byte) ? 
  141.     jz    strup2        ; yes, jump
  142.     cmp    al,'a'        ; test if in range 'a'-'z'
  143.     jb    strup1        ; skip it if not >= a
  144.     cmp    al,'z'
  145.     ja    strup1        ; skip it if not <= z
  146.                 ; change char to lower case
  147.     sub    byte ptr [si-1],'a'-'A'
  148.     jmp    strup1        ; get another char
  149.  
  150. strup2:    pop    si        ; restore original string
  151.     ret            ; address and return
  152.     
  153. strupr    endp
  154.  
  155. _TEXT    ends
  156.  
  157.     end    main
  158.  
  159.