Presentation is loading. Please wait.

Presentation is loading. Please wait.

File Operations. FILE PROCESSING For the purposes of the following discussion, reading means copying all or part of an existing file into memory Writing.

Similar presentations


Presentation on theme: "File Operations. FILE PROCESSING For the purposes of the following discussion, reading means copying all or part of an existing file into memory Writing."— Presentation transcript:

1 File Operations

2 FILE PROCESSING For the purposes of the following discussion, reading means copying all or part of an existing file into memory Writing a file means copying data from memory to a file Rewriting a file means replacing a file's content with other data

3 File Handle When a file is created or opened in a program, DOS assigns it a unique number called the file handle This number is used to identify the file, so the program must save that information Note: a file stream in C/C++/Java is the same thing as a file handle in Assembler

4 File Handle (Cont.) There are 5 predefined file handles: 0 keyboard 1 screen 2 error output - usually the screen 3 COM1 4 printer Additional user-defined files are assigned file handles starting with 5, 6, 7, etc.

5 File Errors There can be many types of errors in using INT 21h file handling functions. Each error type is identified by a code number In the following functions, if an error occurs the CF is set and the code number is stored in AX

6 A list of common file- handling errors Error Code: Meaning: 1 invalid function number 2 file not found 3 path not found 5 access denied (file may already be open by another process) 6 invalid file handle F invalid drive specified

7 DOS FILE HANDLE INSTRUCTIONS As with the DOS I/O functions, put the function number into the AH Register and execute INT 21h.

8 OPENING A FILE A file must be created before it can be used To create a new file, or to rewrite an existing file, the user provides a filename and an attribute DOS returns a file handle

9 OPENING A FILE (Cont.1) INT 21h, Function 3Ch DOS function: Open a new file/rewrite a file Input: AH = 3Ch DS:DX = address of filename (an ASCIIZ string ending with a zero byte) CL = attribute (= 0 for write only, = 1 for read only, =2 for read & write) Output: If successful, AX = file handle Error: if CF = 1, AX = error code (3, 4, or 5)

10 OPENING A FILE (Cont.2) The filename may include a path: A:\PROGS\PROG1.ASM Possible errors: 1.Path doesn't exist 2.Access denied --- directory is full or file is read-only

11 OPENING A FILE (Cont.3) Example: Write instructions to open a new read-only file called "FILE1" FNAME DB 'FILE1', 0 HANDLE DW ?.CODE MOV AX,@DATA MOV DS, AX ; initialize DS MOV AH, 3CH ; open file function LEA DX, FNAME ; copy address to DX MOV CL, 1 ; read-only attribute INT 21H ; open the file JC OPEN_ERROR ; jump if error MOV HANDLE, AX ; handle or err code

12 OPENING A FILE (Cont.4) INT 21h, function 3Dh DOS Function: Open an existing file Input: AH = 3Dh DS:DX = address of filename (an ASCII string ending with a zero byte) AL = access code: 0 = open for reading (where attribute is 1 or 2) 1 = open for writing (where attribute is 0 or 2) 2 = open for read/write (where attribute is 2) Output: If successful, AX = file handle If CF = 1, AX = error code (2, 4, 5, 12)

13 CLOSING FILES A file should be closed after it has been processed. This frees the file handle for use with another file If the file is being written, closing the file causes any data remaining in memory to be written to the file, and the file's time, date, and size will be updated in the directory entry

14 CLOSING FILES (Cont.1) INT 21h, function 3Eh DOS Function: Close a file Input: AH = 3Eh BX = file handle Output: Error if CF = 1, AX = error code (6)

15 CLOSING FILES (Cont.2) Example: Write instructions to close a file. Assume the file handle is stored in a variable called HANDLE MOV AH, 3EH ; close file function MOV BX, HANDLE ; copy handle to bx INT 21H ; execute closing JC CLOSE_ERROR ; jump if error The only error here would be if there is no file using the file handle (e.g., if the file had already been closed or an incorrect file handle had been stored.)

16 READING A FILE The read file function reads a specified number of bytes from a file and stores them in memory

17 READING A FILE (Cont.1) INT 21h, function 3Fh DOS Function: Read a file Input: AH = 3Fh BX = file handle CX = number of bytes to read DS:DX = memory buffer address Output: AX = count of bytes actually read. If AX = 0 or AX < CX, EOF If CF = 1, AX = error code (5, 6)

18 READING A FILE (Cont.2) Example: Write instructions to read a 512-byte sector from a file..DATA HANDLE DW ? BUFFER DB 512 DUP (0).CODE MOV AX, @DATA MOV DS, AX ; initialize DS MOV AH, 3FH ; read file MOV BX, HANDLE ; copy handle into BX MOV CX, 512 ; set count to read LEA DX, BUFFER INT 21H ; AX = bytes read JC READ_ERROR ; jump if error

19 READING A FILE (Cont.3) In some cases, we may want to read the entire file (i.e., until EOF). Compare AX and CX to check for EOF: CMP AX, CX ; EOF? JL EXIT ; yes, stop reading JMP READ_LOOP ; no, keep reading

20 WRITING A FILE INT 21h, function 40h DOS Function: Write a file Input: AH = 40h BX = file handle CX = number of bytes to write DS:DX = data address Output: AX = count of bytes written. If AX < CX, error (disk full). If CF = 1, AX = error code (5, 6)

21 WRITING A FILE (Cont.1) It is possible there is not enough room on the disk to accept the data. This is NOT an error, so the program must check for it by comparing AX and CX Function 40h writes data to a file, but file handles 1 or 4 can be used to send data to the screen or printer

22 WRITING A FILE (Cont.2) Example: Use function 40h to display a message on the screen..DATA MSG DB 'DISPLAY THIS MESSAGE‘.CODE MOV AX, @DATA MOV DS, AX MOV AH, 40H ; write file MOV BX, 1 ; file handle for screen MOV CX, 20 ; len of message (bytes) LEA DX, MSG ; copy address to DX INT 21H ; display MSG executed

23 THE FILE POINTER The file pointer is used to locate a position in a file When the file is opened, the file pointer is positioned at the beginning of the file After a read operation, the file pointer indicates the next byte to be read; after writing a new file, the file pointer is at EOF The following function can be used to move the file pointer

24 THE FILE POINTER (Cont.1) DOS Function: Move File Pointer INT 21h, function 42h Input: AH = 42h AL = movement code: 0 = relative to beginning of file 1 = relative to current file pos 2 = move relative to end of file BX = file handle CX:DX = num bytes to move (signed) Output: DX:AX = new location in bytes from the beginning of the file If CF = 1, AX = error code (1, 6)

25 THE FILE POINTER (Cont.2) In the case where we are moving the file from its current position (AL = 1), if CX:DX is: move pointer backward > 0 => move pointer forward If CX:DX is too large, the pointer could be moved past the beginning or end of the file. This is not an error, but will cause an error when the next read or write is executed If AL = 0 => move pointer from beginning of file (forward) If AL = 1 => move pointer from current position (forward or backward) If AL = 2 => move pointer from end of file (backward)

26 THE FILE POINTER (Cont.3) Code to move pointer to end of file and determine file size: MOV AH, 42H ; move file ptr MOV BX, HANDLE ; file handle MOV CX, 0 ; clear CX MOV DX, 0 ; 0 bytes to move MOV AL, 2 ; relative to end INT 21H ; move pointer to end. DX:AX = file size JC MOVE_ERROR ; error if CF = 1


Download ppt "File Operations. FILE PROCESSING For the purposes of the following discussion, reading means copying all or part of an existing file into memory Writing."

Similar presentations


Ads by Google