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

  1.     title      PROTO -- Filter Template
  2.         page      55,132
  3.         .286
  4.  
  5. ;
  6. ; PROTO.ASM
  7. ;
  8. ; MASM filter template for OS/2.
  9. ;
  10. ; Assemble with:  C> masm proto.asm;
  11. ; Link with:  C> link proto,,,os2;
  12. ;
  13. ; Usage is:  C> proto <source >destination
  14. ;
  15. ; Copyright (C) 1988 Ray Duncan
  16. ;
  17.  
  18. stdin   equ     0               ; standard input device
  19. stdout  equ     1               ; standard output device        
  20. stderr  equ     2               ; standard error device
  21.  
  22. cr      equ     0dh             ; ASCII carriage return
  23. lf      equ     0ah             ; ASCII linefeed
  24.  
  25. bufsize equ     256             ; I/O buffer size
  26.  
  27.         extrn   DosRead:far
  28.         extrn   DosWrite:far
  29.         extrn   DosExit:far
  30.  
  31.  
  32. DGROUP  group   _DATA           ; 'automatic data group'
  33.  
  34. _DATA   segment word public 'DATA'
  35.  
  36. input   db      bufsize dup (?) ; storage for input line 
  37. output  db      bufsize dup (?) ; storage for output line
  38.  
  39. rlen    dw      ?               ; receives bytes read   
  40. wlen    dw      ?               ; receives bytes written
  41.  
  42. _DATA   ends
  43.  
  44.  
  45. _TEXT   segment word public 'CODE'
  46.  
  47.         assume  cs:_TEXT,ds:DGROUP
  48.  
  49. main    proc    far             ; entry point from OS/2
  50.  
  51.         push    ds              ; make DGROUP addressable
  52.         pop     es              ; via ES register too
  53.         assume  es:DGROUP
  54.  
  55.         cld                     ; safety first
  56.  
  57. main1:                          ; read line from standard input...
  58.         push    stdin           ; standard input handle
  59.         push    ds              ; buffer address
  60.         push    offset DGROUP:input
  61.         push    bufsize         ; buffer length
  62.         push    ds              ; receives bytes read
  63.         push    offset DGROUP:rlen
  64.         call    DosRead         ; transfer to OS/2
  65.         or      ax,ax           ; was read successful?
  66.         jnz     main3           ; exit if any error
  67.  
  68.         mov     ax,rlen         ; get length of input
  69.         or      ax,ax           ; any characters read?
  70.         jz      main2           ; jump if end of stream
  71.  
  72.         call    translate       ; translate line if necessary
  73.  
  74.         or      ax,ax           ; anything to output?
  75.         jz      main1           ; no, go read another line
  76.  
  77.                                 ; write line to standard output...
  78.         push    stdout          ; standard output handle
  79.         push    ds              ; buffer address
  80.         push    offset DGROUP:output
  81.         push    ax              ; buffer length
  82.         push    ds              ; receives bytes written
  83.         push    offset DGROUP:wlen
  84.         call    DosWrite        ; transfer to OS/2
  85.         or      ax,ax           ; write successful?
  86.         jnz     main3           ; exit if any error
  87.  
  88.         jmp     main1           ; go read another line
  89.  
  90. main2:                          ; end of stream reached
  91.         push    1               ; 1 = end all threads
  92.         push    0               ; 0 = exit code
  93.         call    DosExit         ; final exit to OS/2
  94.  
  95. main3:                          ; error encountered
  96.         push    1               ; 1 = end all threads
  97.         push    1               ; 1 = exit code
  98.         call    DosExit         ; final exit to OS/2
  99.  
  100. main    endp                    ; end of main procedure
  101.  
  102.  
  103. ; Perform any necessary translation on line stored in
  104. ; 'input' buffer, leaving result in 'output' buffer.
  105. ;
  106. ; Call with:    AX = length of data in 'input' buffer
  107. ;
  108. ; Return:    AX = length to write to standard output
  109. ;
  110. ; Action of template routine is just to copy the line.
  111.  
  112. translate proc  near
  113.  
  114.                                 ; copy input to output...
  115.         mov     si,offset DGROUP:input
  116.         mov     di,offset DGROUP:output
  117.         mov     cx,ax
  118.         rep movsb
  119.         ret                     ; return AX = length unchanged
  120.  
  121. translate endp
  122.  
  123.  
  124. _TEXT   ends
  125.  
  126.         end     main            ; program entry point
  127.