The DeleteFile function

The DeleteFile function deletes the file with the specified name.

Parameters: AX: pointer to the string with the file name.

Return value: On success (CF=0) the file is deleted; 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 DeleteFile function to delete a file.

title Author: Evgeny Sergeevich Dovgopolov

.model small
.stack 100h

data segment public
    fName    db "test.txt", 0
    dSuccess db "The file was deleted successfully!\0"
    dError   db "An error occurred while deleting '%s'. Error code: %d\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

        lea     ax, ds:[fName]     ; Load the address of the string fName into the ds:ax register
        call    DeleteFile         ; Call the DeleteFile procedure (ax:fName) := AX and CF

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

        push    ax                 ; 3rd: Save the ax register value 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:[dError]    ; 1st: Load the dError format string into the ds:ax register
        call    PrintFormatString  ; Call the PrintFormatString procedure (ax:dError, fName)
        add     sp, 4              ; Align the stack
        jmp     Done               ; Perform an unconditional jump to the Done label

    NoDeleteFileError:
        lea     ax, ds:[dSuccess]  ; Load the address of the string dSuccess into the ds:ax register
        call    WriteString        ; Call the WriteString procedure (ax:dSuccess)

    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

The file was deleted successfully!

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