![]() |
#2
zklhp2012-08-14 11:33
|

;**************************************************************************
;program name: show_txt.asm
;function: show a text file
;author: frank
;date: 8-14-2012
;**************************************************************************
EOF = 1AH
DATA SEGMENT
FNAME DB 'TEST.TXT', 0 ;file's name
ERROR1 DB 'File not found', 07h, 0 ;message
ERROR2 DB 'Reading error', 07h, 0 ;message
BUFFER DB ? ;buffer zore for one byte
DATA ENDS
;**************************************************************************
CODE SEGMENT
ASSUME CS:CODE,DS:DATA
START: mov ax,data
mov ds,ax ;set data segment register
mov dx,offset FNAME
mov ax,3d00h ;open file for reading
int 21h
jnc OPEN_OK ;open success
mov si,offset ERROR1
call DMESS ;show message when operation failed
jmp OVER
OPEN_OK:mov bx,ax ;save file
CONT: call READCH ;read a character from file
jc READERR ;turning when read failed
cmp al,eof ;judge if read character for file end
jmp CONT ;continue
READERR:mov si,offset ERROR2
call DMESS ;show massage for failed reading
TYPE_OK:mov ah,3eh ;close file
int 21h
OVER: mov ah,4ch ;ending program
int 21h
;subroutine message: read character
READCH PROC
mov cx,1 ;set number for byte of reading
mov dx,offset BUFFER ;set burrer zero address
mov ah,3fh ;set function call's number
int 21h ;read
jc READCH2 ;error for reading
cmp ax,cx ;judge if file is ending
mov al,EOF ;if file ended, set file endiing character
jb READCH1 ;file ended, turning
mov al,BUFFER ;if file isn't end, reading next
READCH1:clc
READCH2:ret
READCH ENDP
;subroutine DMESS: show a string whose ending character is "0"
;entrance parameter: si=string's start address
;export parameter: none
DMESS PROC
DMESS1: mov dl,[si]
inc si
or dl,dl
jz DMESS2
mov ah,2
int 21h
jmp DMESS1
DMESS2: ret
DMESS ENDP
;subroutine: PUTCH:
PUTCH PROC
push dx
mov dl,al
mov ah,2
int 21h
pop dx
ret
PUTCH ENDP
CODE ENDS
END START
;**************************************************************************