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

  1.         title     LC.ASM --- lowercasing filter
  2.         page      55,132
  3.         .286
  4. ;
  5. ; LC.ASM        Filter to translate upper case 
  6. ;               chararacters to lower case.
  7. ; Copyright (C) 1988 Ray Duncan
  8. ;
  9. ; Build:        MASM LC;
  10. ;               LINK LC,,,OS2,LC;
  11. ;
  12. ; Usage:        LC [<source] [>destination]
  13.  
  14. stdin   equ     0                       ; standard input device
  15. stdout  equ     1                       ; standard output device        
  16. stderr  equ     2                       ; standard error device
  17.  
  18. cr      equ     0dh                     ; ASCII carriage return
  19. lf      equ     0ah                     ; ASCII linefeed
  20.  
  21. bufsize equ     256                     ; max amount to read or write
  22.  
  23.         extrn   DosExit:far             ; references to OS/2 API
  24.         extrn   DosRead:far
  25.         extrn   DosWrite:far
  26.  
  27. DGROUP  group   _DATA           
  28.  
  29. _DATA   segment word public 'DATA'
  30.  
  31. input   db      bufsize dup (?)         ; storage for input line 
  32. output  db      bufsize dup (?)         ; storage for output line
  33.  
  34. rlen    dw      ?                       ; receives bytes read   
  35. wlen    dw      ?                       ; receives bytes written
  36.  
  37. _DATA   ends
  38.  
  39.  
  40. _TEXT   segment word public 'CODE'
  41.  
  42.         assume  cs:_TEXT,ds:DGROUP
  43.  
  44. main    proc    far                     ; entry point from OS/2
  45.  
  46.         mov     ax,ds                   ; make DGROUP addressable
  47.         mov     es,ax                   ; via ES register too
  48.  
  49. main1:                                  ; read line from standard input
  50.         push    stdin                   ; standard input handle
  51.         push    ds                      ; buffer address
  52.         push    offset DGROUP:input
  53.         push    bufsize                 ; buffer length
  54.         push    ds                      ; receives bytes read
  55.         push    offset DGROUP:rlen
  56.         call    DosRead                 ; transfer to OS/2
  57.  
  58.         mov     ax,rlen                 ; get length of input
  59.         or      ax,ax                   ; any characters read?
  60.         jz      main2                   ; jump if end of stream
  61.  
  62.         call    translate               ; translate line if necessary
  63.  
  64.         or      ax,ax                   ; anything to output?
  65.         jz      main1                   ; no, go read another line
  66.  
  67.                                         ; write line to standard output 
  68.         push    stdout                  ; standard output handle
  69.         push    ds                      ; buffer address
  70.         push    offset DGROUP:output
  71.         push    ax                      ; buffer length
  72.         push    ds                      ; receives bytes written
  73.         push    offset DGROUP:wlen
  74.         call    DosWrite                ; transfer to OS/2
  75.  
  76.         jmp     main1                   ; go read another line
  77.  
  78. main2:                                  ; end of stream reached
  79.         push    1                       ; 1 = end all threads
  80.         push    0                       ; 0 = exit code
  81.         call    DosExit                 ; final exit to OS/2
  82.  
  83. main    endp                            ; end of main procedure
  84.  
  85.  
  86. ; Copy input buffer to output buffer, translating
  87. ; any upper case characters to lower case.
  88. ;
  89. ; Call with:    AX = length of data in 'input' buffer.
  90. ;
  91. ; Return:       AX = length to write to standard output.
  92.  
  93. translate proc  near
  94.  
  95.         push    ax                      ; save original length
  96.  
  97.         mov     si,offset DGROUP:input  ; address of original line
  98.         mov     di,offset DGROUP:output ; address for converted line
  99.         mov     cx,ax                   ; length to convert
  100.         jcxz    trans3                  ; exit if empty line
  101.  
  102. trans1:                                 ; convert char. by char...
  103.         lodsb                           ; get input character
  104.         cmp     al,'A'                  ; if not A-Z, leave it alone
  105.         jb      trans2
  106.         cmp     al,'Z'
  107.         ja      trans2
  108.  
  109.         add     al,'a'-'A'              ; it's A-Z, convert it
  110.  
  111. trans2: stosb                           ; put into output buffer
  112.  
  113.         loop    trans1                  ; loop until all characters
  114.                                         ; have been transferred
  115.  
  116. trans3: pop     ax                      ; get back line length 
  117.         ret                             ; and return it in AX
  118.  
  119. translate endp
  120.  
  121. _TEXT   ends
  122.  
  123.         end     main            
  124.