Int 21H [2.0] Function 3FH (63) Read file or device

Given a valid file handle from a previous open or create operation, a buffer address, and a length in bytes, transfers data at the current file-pointer position from the file into the buffer and then updates the file pointer position.

Call with:

AH = 3FH

BX = handle

CX = number of bytes to read

DS:DX = segment:offset of buffer

Returns:

If function successful

Carry flag = clear

AX = bytes transferred

If function unsuccessful

Carry flag = set

AX = error code

Notes:

If reading from a character device (such as the standard input) in cooked mode, at most one line of input will be read (i.e., up to a carriage return character or the specified length, whichever comes first).

If the carry flag is returned clear but AX = 0, then the file pointer was already at end of file when the program requested the read.

If the carry flag is returned clear but AX < CX, then a partial record was read at end of file or there is an error.

[3.0+] If the program is running on a network, the user must have Read access rights to the directory and file.

Example:

Using the file handle from a previous open or create operation, read 1024 bytes at the current file pointer into the buffer named buff.

buff db 1024 dup (?) ; buffer for read

fhandle dw ? ; contains file handle

.

.

.

mov ah,3fh ; function number

mov dx,seg buff ; buffer address

mov ds,dx

mov dx,offset buff

mov bx,fhandle ; file handle

mov cx,1024 ; length to read

int 21h ; transfer to MS-DOS

jc error ; jump, read failed

cmp ax,cx ; check length of read

jl done ; jump, end of file

.

.

.