The ReadFile function

The ReadFile function reads data from a file.

Parameters: AX: pointer to the buffer; BX: file handle; CX: number of bytes to read.

Return value: On success (CF=0) AX contains the number of bytes successfully read; on error (CF=1) AX contains the error code.

Flag state: The function affects all flags.

Program source code

An example program for the Intel 8086 assembler demonstrating the use of the ReadFile function to read data from a file.

title Author: Evgeny Sergeevich Dovgopolov

.model small
.stack 100h

data segment public
    fName   db "test.txt", 0
    fBuffer db 255 dup(0)
    rError  db "An error occurred while reading from the file '%s'. Error code: %d\0"
    fHandle dw ?
data ends

code segment public                ; Start of the code segment
    assume  ds:data, cs:code
    include Handyfar.inc           ; Include the file with functions

    main proc                      ; Start of the main procedure
        call    SetDataSegment     ; Call the SetDataSegment procedure

        mov     bl, READ_WRITE     ; 2nd: Set the read-write attribute in the bl register
        lea     ax, ds:[fName]     ; 1st: Load the address of the string fName into the ds:ax register
        call    OpenFile           ; Call the OpenFile procedure (ds:ax, READ_WRITE) := AX and CF
        mov     fHandle, ax        ; Save the file handle in the variable fHandle

        mov     cx, 0FFh           ; 3rd: Copy the maximum number of bytes to read into the cx register
        mov     bx, fHandle        ; 2nd: Copy the file handle fHandle into the bx register
        lea     ax, ds:[fBuffer]   ; 1st: Load the address of the buffer fBuffer into the ds:ax register
        call    ReadFile           ; Call the ReadFile procedure (ax:fBuffer, bx:fHandle, cx:0FFh)

        jnc     NoReadFileError    ; If CF=0, jump to the label NoReadFileError

        push    ax                 ; 3rd: Save the ax register value (error code) on the stack
        lea     ax, ds:[fName]     ; Load the address of the variable fName into the ds:ax register
        push    ax                 ; 2nd: save the ax register value on the stack
        lea     ax, ds:[rError]    ; 1st: Load the address of the format string rError into the ds:ax register
        call    PrintFormatString  ; Call the PrintFormatString procedure (ax:rError, fName, ax)
        add     sp, 4              ; Align the stack
        jmp     Done               ; Perform an unconditional jump to the label Done

    NoReadFileError:
        lea     ax, ds:[fBuffer]   ; Load the address of the buffer fBuffer into the ds:ax register
        call    WriteString        ; Call the WriteString procedure (ax:fBuffer)

    Done:
        xor     al, al             ; Clear the al register
        call    ExitProcess        ; Call the ExitProcess procedure
    main endp                      ; End of the main procedure

code ends                          ; End of the code segment
end main	                       ; End of file. The main procedure is the entry point

This text is from the file!

Example taken from the textbook "HANDYFAR - MASM (Evgeny Sergeevich Dovgopolov)".