You are on page 1of 8

File Processing

Assembly Language Programming


File Handle
• An integer assigned to a file by DOS
• Used to refer to the file in programs
• DOS provides file handle functions to
manipulate files
› These are available through the DOS interrupt
(21h)

04/10/20 Ali Saleh - Assembly Language Pr 2


ogramming
Predefined File Handles
• DOS manages standard input output devices
through these handles
› 0 keyboard (stdin)
› 1 screen (stdout)
› 2 error output (stderr)
› 3 auxiliary device
› 4 printer

04/10/20 Ali Saleh - Assembly Language Pr 3


ogramming
File Errors
• Many file handling • 1 invalid function number
• 2 file not found
routines return an
• 3 path not found
error code • 4 no free handles
› carry clear indicates no • 5 access denied
error • 6 invalid handle
› carry set indicates an • C invalid access code
error • F invalid drive
› AX will contain an • 10 can't remove current dir
error number • 11 not the same device
• 12 no more files to be found
04/10/20 Ali Saleh - Assembly Language Pr 4
ogramming
Opening a File
• Create new file • Open Existing
› AH is 3Ch › AH is 3Dh
› DS:DX is the address › DS:DX is the address
of filename (ASCIIZ)
of filename (ASCIIZ)
› AL is access code
› CL is attribute • 0 read
• Returns file handle in • 1 write
• 2 read/write
AX or error code 3, 4,
or 5 • Returns file handle in
AX or error code
2,4,5,12
04/10/20 Ali Saleh - Assembly Language Pr 5
ogramming
Closing a File
• Closing a file frees the file handle and
updates file info (size, timestamp) in
directory
› AH is 3Eh
› BX is the file handle
• Returns error code 6 in AX if unsuccessful

04/10/20 Ali Saleh - Assembly Language Pr 6


ogramming
Reading a File (or keyboard)
• Read a File • If a file is open for
› AH is 3Fh reading, this function
› BX is file handle will copy a specified
› CX is number of bytes to number of bytes from
read
the file to a memory
› DS:DX is address of
buffer
buffer
• Returns number of bytes • EOF is detected by
read in AX or error code comparing CX and
5 or 6 AX after the read
04/10/20 Ali Saleh - Assembly Language Pr 7
ogramming
Writing to a File (or screen or
printer)
• Write File • This function sends
› AH is 40h one or more bytes to a
› BX is file handle file that is open for
› CX is number of bytes to output
write
• A full disk will result
› DS:DX is address of
bytes to be written in less than the
• Returns number of bytes specified number of
written in AX or error bytes being written
code 5 or 6
04/10/20 Ali Saleh - Assembly Language Pr 8
ogramming

You might also like