Writing and reading files
Creating a file on a disk Get a file handle from system Use INT 21H function 3C to create a directory entry for the file Use INT 21H function 40H to write records to the file Use INT 21H function 3EH to close the file
Create a file filename DB 'data.dat',0 handle DW ? prompterror DB 'Error while creating this file ','$'
INT 21H function 3CH mov cx, 0 ; File attribute LEA dx, filename mov ah, 3ch int 21h jc err mov filehandle, ax jmp exit err: MOV AH, 09H LEA DX, prompterror INT 21H
File attribute File attribute defines the type of file. One file may have more than one attribute 00H: normal file 01H: read only file 02H:hidden file 04H: system file (more information, please see page 301)
INT 21H Function 40H MOV Ah, 40H MOV BX, filehandle MOV CX, 256 ; number of bytes to write to disk LEA DX, DISKAREA ; the area to write to disk INT 21H JC error CMP AX, 256 JNE error
INT 21H function 3EH: close file MOV AH, 3EH MOV BX, filehandle INT 21H JC error
Reading a file Use INT 21H function 3Dh to open a file Use INT 21H function 3FH to read records from the file Use INT 21H function 3EH to close a file
Open a file using INT 21H function 3DH 000: read only 001: write only 010: read/write
Open file for reading MOV AH, 3DH MOV AL, 00H ; Access code = 00 = read only LEA DX, FILENAME INT 21H JC error MOV filehandle, AX Data: filehandle DW ?
INT 21H function 3F – Read record Data: Filehandle DW ? INAREA DB 512 DUP(‘ ‘)
Code MOV AH, 3FH MOV BX, filehandle MOV CX, 512 LEA DX, inarea INT 21H JC error CMP AX, 00 JE exit
Final exam Date: Tuesday May 9, 10am-12pm Open book Format (multiple choices, problem solving, determine the errors from code, 2 programming problems) Focus: everything from Week 1 to Week 14