DeleteFileEx function
The DeleteFileEx function deletes a file. The pointer to the file name is passed through the stack. The function automatically aligns the stack to 2 bytes.
Parameters (stack): 1. Pointer to 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
Intel 8086 assembly program example demonstrating the use of the DeleteFileEx function to delete a file. The file name is passed through the stack.
title .286C .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 assume ds:data, cs:code include Handyfar.inc ; Include the file with functions include Handymac.inc ; Include the file with macros main proc call SetDataSegment ; Call the SetDataSegment procedure pushs ds:[fName] ; Push the address of the fName variable onto the top of the stack call DeleteFileEx ; Call the DeleteFileEx(fName) procedure := AX and CF jnc @NoDeleteFileError ; If CF=0, jump to the label @NoDeleteFileError push ax ; 3rd: Push ax onto the top of the stack (error code) pushs ds:[fName] ; 2nd: Push the address of the fName variable onto the top of the stack leaax ds:[dError] ; 1st: Load the address of the dError format string into the ds:ax register call PrintFormatString ; Call the PrintFormatString(ax:dError, fName, ax) procedure add sp, 4 ; Align the stack jmp @Done ; Perform an unconditional jump to the label @Done @NoDeleteFileError: pushs ds:[dSuccess] ; Push the address of the dSuccess variable onto the top of the stack call WriteStringEx ; Call the WriteStringEx(dSuccess) procedure @Done: push 0 ; Push the byte return code onto the top of the stack call ExitProcessEx ; Call the ExitProcessEx(0) procedure main endp code ends end main
The file was deleted successfully!
Example taken from the textbook "HANDYFAR - MASM (Evgeny Sergeevich Dovgopolov)".