The FindFirstFile function

The FindFirstFile function searches for the first file matching the specified name pattern and stores its name in the provided buffer.

Parameters: DS:AX - pointer to the pattern string; ES:BX - pointer to the buffer; CX - the file attributes for the search.

Return value: On success (CF=0) the file is found; 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 FindFirstFile function to find the first file matching a pattern.

title Author: Evgeny Sergeevich Dovgopolov

.model small
.stack 100h

data segment public
    pattern db "*.txt", 0
    fmtout  db "Found file: %s\n\0"
    fError  db "No files were found!\n\0"
    buffer  db 13 dup(0)
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

        xor     cx, cx             ; Set the attribute to 0
        lea     bx, es:[buffer]    ; Load the address of the buffer string into the es:bx register
        lea     ax, ds:[pattern]   ; Load the address of the pattern string into the ds:ax register
        call    FindFirstFile      ; Call the FindFirstFile procedure (ds:ax, es:bx, 0) := AX and CF

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

        lea     ax, ds:[fError]    ; Load the address of the fError string into the ds:ax register
        call    WriteString        ; Call the WriteString procedure (ax:fError)
        jmp     Done               ; Perform an unconditional jump to the label Done

    FileWasFound:
        lea     ax, ds:[buffer]    ; Load the address of the buffer variable into the ds:ax register
        push    ax                 ; 2nd: Save the ax register value on the stack
        lea     ax, ds:[fmtout]    ; 1st: Load the fmtout format string into the ds:ax register
        call    PrintFormatString  ; Call the PrintFormatString procedure (ax:fmtout, buffer)
        add     sp, 2              ; Align the stack

    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

Found file: file.txt

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