Documente Academic
Documente Profesional
Documente Cultură
FILE PROCESSING
DOS File Handle Functions - a group of INT 21h functions that allows DOS to
track open file
data in its own internal tables. File Handle Functions also permit users to
specify file
path names.
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.
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.)
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.
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.
A list of common file-handling errors:
Error Code: Meaning:
1 invalid function number
2 file not found
3 path not found
4 all available handles in use
5 access denied (file may already be open by another
process)
6 invalid file handle
C invalid access code
F invalid drive specified
10 attempt to remove current directory
11 not the same device
12 no more files to be found
DOS FILE HANDLE INSTRUCTIONS
As with the DOS I/O functions, put the function number into the AH Register
and execute
INT 21h.
READING A FILE
The read file function reads a specified number of bytes from a file and
stores
them in memory.
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)
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.
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