TITLE FILE I/O PAGE 60,132 .MODEL SMALL .CODE ;---------------- MAIN procedure ------------------------------- ;this program clears screen, reads keyboard input into buffer, writes buffer to file, closes file ;then opens file, reads file to buffer, writes buffer to monitor, closes file, exits when user is ready MAIN PROC FAR MOV AX,@DATA MOV DS,AX CALL CLR_SCR ;clear the screen MOV AH,02 ;set cursor position MOV BH,00 MOV DL,10 ;column position MOV DH,10 ;row position INT 10H CALL CREATE_F ;create an output file MOV AH,0AH ;read keyboard input MOV DX,OFFSET BUFFER_1 INT 21H CALL WRITE_F ;write buffer to file CALL CLOSE_F ;close file MOV AH,02 ;set cursor position MOV BH,00 MOV DL,10 ;column position MOV DH,20 ;row position INT 10H CALL OPEN_F ;open file for read CALL READ_F ;read file to buffer MOV AH,09 ;copy buffer to screen MOV DX,OFFSET BUFFER_2 INT 21H CALL CLOSE_F ;close file MOV AH,02 ;set cursor position MOV BH,00 MOV DL,10 ;column position MOV DH,24 ;row position INT 10H MOV DX,OFFSET EXIT_MSG ;print "ANY KEY TO EXIT" message CALL ERR_MSG MOV AH,07 ;exit when user hits any key INT 21H MOV AH,4CH ;set up to return INT 21H ;return to DOS MAIN ENDP ;--------------- this procedure creates an output file ------------------------ CREATE_F PROC NEAR MOV AH,3CH ;create file function MOV CX,0 ;normal file MOV DX,OFFSET FILE_1 ;DX points to ASCIIZ INT 21H ;invoke interrupt JC C_ERR MOV HANDLE_F1,AX ;save handle if OK JMP C_EXIT C_ERR: MOV DX,OFFSET ERR_CREATE ;display error message CALL ERR_MSG C_EXIT: RET CREATE_F ENDP ;--------------- this procedure reads file to buffer ------------------------ READ_F PROC NEAR MOV AH,3FH ;read from file MOV BX,HANDLE_F1 ;use handle MOV CX,25 ;number of bytes to read MOV DX,OFFSET BUFFER_2 ;DX points to buffer INT 21H ;invoke interrupt JNC R_EXIT MOV DX,OFFSET ERR_READ ;display error message CALL ERR_MSG R_EXIT: RET READ_F ENDP ;--------------- this procedure writes buffer to file ------------------------ WRITE_F PROC NEAR MOV AH,40H ;write to file MOV BX,HANDLE_F1 MOV CX,25 ;number of bytes to write MOV DX,OFFSET BUFFER_1+2 ;DX points to buffer INT 21H ;invoke interrupt JNC W_EXIT MOV DX,OFFSET ERR_WRITE ;display error message CALL ERR_MSG W_EXIT: RET WRITE_F ENDP ;--------------- this procedure closes a file ------------------------ CLOSE_F PROC NEAR MOV AH,3EH ;close file function MOV BX,HANDLE_F1 ;use handle INT 21H ;invoke interrupt JNC CL_EXIT MOV DX,OFFSET ERR_CLOSE ;display error message CALL ERR_MSG CL_EXIT: RET CLOSE_F ENDP ;--------------- this procedure opens a file ------------------------ OPEN_F PROC NEAR MOV AH,3DH ;open file function MOV AL,0 ;read only MOV DX,OFFSET FILE_1 ;DX points to ASCIIZ INT 21H ;invoke interrupt JC O_ERR MOV HANDLE_F1,AX ;save handle if OK JMP O_EXIT O_ERR: MOV DX,OFFSET ERR_OPEN ;display error message CALL ERR_MSG O_EXIT: RET OPEN_F ENDP ;--------------- this procedure displays a message --------------- ERR_MSG PROC NEAR ;DX points to msg before call MOV AH,09H ;output to monitor INT 21H ;invoke interrupt RET ERR_MSG ENDP ;--------------- this procedure clears the screen -------------------- CLR_SCR PROC NEAR MOV AX,0600H ;scroll screen MOV BH,07 MOV CX,0000 ;scroll the MOV DX,184FH ; entire screen INT 10H RET CLR_SCR ENDP ;--------------- data area -------------------------------------------------- .DATA HANDLE_F1 DW 0 ;file1 handle FILE_1 DB 'C:\FILE1.ASC',0 ;file1 ASCIIZ string BUFFER_1 DB 25,?,25 DUP (' ') BUFFER_2 DB 25 DUP (' '),'$' ERR_CREATE DB 0DH,0AH,'** Error creating file **$' ERR_OPEN DB 0DH,0AH,'** Error opening file **$' ERR_READ DB 0DH,0AH,'** Error reading file **$' ERR_WRITE DB 0DH,0AH,'** Error writing to file **$' ERR_CLOSE DB 0DH,0AH,'** Error closing file **$' EXIT_MSG DB 0DH,0AH,'PRESS ANY KEY TO EXIT$' ; .STACK 32 END MAIN