home *** CD-ROM | disk | FTP | other *** search
/ Microsoft Programmer's Library 1.3 / Microsoft-Programers-Library-v1.3.iso / sampcode / dos_ency / 14 / protol.asm < prev    next >
Encoding:
Assembly Source File  |  1988-08-11  |  3.3 KB  |  104 lines

  1.         name    protol
  2.         title   'PROTOL.ASM --- template line filter'
  3. ;
  4. ; PROTOL.ASM:  a template for a line-oriented filter.
  5. ;
  6. ; Ray Duncan, June 1987
  7. ;
  8.  
  9. stdin   equ     0               ; standard input
  10. stdout  equ     1               ; standard output
  11. stderr  equ     2               ; standard error
  12.  
  13. cr      equ     0dh             ; ASCII carriage return
  14. lf      equ     0ah             ; ASCII linefeed
  15.  
  16.  
  17. DGROUP  group   _DATA,STACK     ; 'automatic data group'
  18.  
  19.  
  20. _TEXT   segment byte public 'CODE'
  21.  
  22.         assume  cs:_TEXT,ds:DGROUP,es:DGROUP,ss:STACK
  23.  
  24. main    proc    far             ; entry point from MS-DOS
  25.  
  26.         mov     ax,DGROUP       ; set DS = ES = our data segment
  27.         mov     ds,ax
  28.         mov     es,ax
  29.  
  30. main1:                          ; read a line from standard input
  31.         mov     dx,offset DGROUP:input  ; address to place data
  32.         mov     cx,256          ; max length to read = 256
  33.         mov     bx,stdin        ; handle for standard input
  34.         mov     ah,3fh          ; function 3FH = read from file or device
  35.         int     21h             ; transfer to MS-DOS
  36.         jc      main3           ; if error, terminate
  37.         or      ax,ax           ; any characters read?
  38.         jz      main2           ; end of file, terminate program
  39.  
  40.         call    translt         ; translate line if necessary
  41.         or      ax,ax           ; anything to output after translation?
  42.         jz      main1           ; no, get next line
  43.  
  44.                                 ; now write line to standard output
  45.         mov     dx,offset DGROUP:output ; address of data
  46.         mov     cx,ax           ; length to write
  47.         mov     bx,stdout       ; handle for standard output
  48.         mov     ah,40h          ; function 40H = write to file or device
  49.         int     21h             ; transfer to MS-DOS
  50.         jc      main3           ; if error, terminate
  51.         cmp     ax,cx           ; was entire line written?
  52.         jne     main3           ; disk full, terminate program
  53.         jmp     main1           ; go process another line
  54.  
  55. main2:  mov     ax,4c00h        ; end of file reached, terminate 
  56.         int     21h             ; program with return code = 0
  57.  
  58. main3:  mov     ax,4c01h        ; error or disk full, terminate 
  59.         int     21h             ; program with return code = 1 
  60.  
  61. main    endp                    ; end of main procedure
  62.  
  63. ;
  64. ; Perform any necessary translation on line stored in
  65. ; 'input' buffer, leaving result in 'output' buffer.
  66. ;
  67. ; Call with:    AX = length of data in 'input' buffer.
  68. ;
  69. ; Return:       AX = length to write to standard output.
  70. ;
  71. ; Action of template routine is just to copy the line.
  72. ;
  73. translt proc    near
  74.  
  75.                                 ; just copy line from input to output
  76.         mov     si,offset DGROUP:input
  77.         mov     di,offset DGROUP:output
  78.         mov     cx,ax
  79.         rep movsb
  80.         ret                     ; return length in AX unchanged
  81.  
  82. translt endp
  83.  
  84.  
  85. _TEXT   ends
  86.  
  87.  
  88. _DATA   segment word public 'DATA'
  89.  
  90. input   db      256 dup (?)     ; storage for input line 
  91. output  db      256 dup (?)     ; storage for output line
  92.  
  93. _DATA   ends
  94.  
  95.  
  96. STACK   segment para stack 'STACK'
  97.  
  98.         dw      64 dup (?)
  99.  
  100. STACK   ends
  101.  
  102.  
  103.         end     main            ; defines program entry point
  104.