home *** CD-ROM | disk | FTP | other *** search
- name protol
- title 'PROTOL.ASM --- template line filter'
- ;
- ; PROTOL.ASM: a template for a line-oriented filter.
- ;
- ; Ray Duncan, June 1987
- ;
-
- stdin equ 0 ; standard input
- stdout equ 1 ; standard output
- stderr equ 2 ; standard error
-
- cr equ 0dh ; ASCII carriage return
- lf equ 0ah ; ASCII linefeed
-
-
- DGROUP group _DATA,STACK ; 'automatic data group'
-
-
- _TEXT segment byte public 'CODE'
-
- assume cs:_TEXT,ds:DGROUP,es:DGROUP,ss:STACK
-
- main proc far ; entry point from MS-DOS
-
- mov ax,DGROUP ; set DS = ES = our data segment
- mov ds,ax
- mov es,ax
-
- main1: ; read a line from standard input
- mov dx,offset DGROUP:input ; address to place data
- mov cx,256 ; max length to read = 256
- mov bx,stdin ; handle for standard input
- mov ah,3fh ; function 3FH = read from file or device
- int 21h ; transfer to MS-DOS
- jc main3 ; if error, terminate
- or ax,ax ; any characters read?
- jz main2 ; end of file, terminate program
-
- call translt ; translate line if necessary
- or ax,ax ; anything to output after translation?
- jz main1 ; no, get next line
-
- ; now write line to standard output
- mov dx,offset DGROUP:output ; address of data
- mov cx,ax ; length to write
- mov bx,stdout ; handle for standard output
- mov ah,40h ; function 40H = write to file or device
- int 21h ; transfer to MS-DOS
- jc main3 ; if error, terminate
- cmp ax,cx ; was entire line written?
- jne main3 ; disk full, terminate program
- jmp main1 ; go process another line
-
- main2: mov ax,4c00h ; end of file reached, terminate
- int 21h ; program with return code = 0
-
- main3: mov ax,4c01h ; error or disk full, terminate
- int 21h ; program with return code = 1
-
- main endp ; end of main procedure
-
- ;
- ; Perform any necessary translation on line stored in
- ; 'input' buffer, leaving result in 'output' buffer.
- ;
- ; Call with: AX = length of data in 'input' buffer.
- ;
- ; Return: AX = length to write to standard output.
- ;
- ; Action of template routine is just to copy the line.
- ;
- translt proc near
-
- ; just copy line from input to output
- mov si,offset DGROUP:input
- mov di,offset DGROUP:output
- mov cx,ax
- rep movsb
- ret ; return length in AX unchanged
-
- translt endp
-
-
- _TEXT ends
-
-
- _DATA segment word public 'DATA'
-
- input db 256 dup (?) ; storage for input line
- output db 256 dup (?) ; storage for output line
-
- _DATA ends
-
-
- STACK segment para stack 'STACK'
-
- dw 64 dup (?)
-
- STACK ends
-
-
- end main ; defines program entry point
-