CreateFileEx function
The CreateFileEx function creates a new file with the specified name and attributes. The parameters are passed through the stack. The function automatically aligns the stack to 4 bytes.
Parameters (stack): 1. Pointer to the file name. 2. Creation attributes (READ_ONLY, HIDDEN, etc.).
Return value: On success (CF=0) AX contains the file handle; 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 CreateFileEx function to create a new file with attributes passed through the stack.
title .286C .model small .stack 100h data segment public fName db "test.txt", 0 cSuccess db "File '%s' was created successfully!\0" cError db "An error occurred while creating '%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 push READ_ONLY ; 2nd: Push the READ_ONLY attribute onto the top of the stack pushs ds:[fName] ; 1st: Push the address of the fName variable onto the top of the stack call CreateFileEx ; Call the CreateFileEx(fName, READ_ONLY) procedure := AX and CF jnc @NoCreateFileError ; If CF=0, jump to the label @NoCreateFileError 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:[cError] ; 1st: Load the address of the cError format string into the ds:ax register call PrintFormatString ; Call the PrintFormatString(ax:cError, fName, ax) procedure add sp, 4 ; Align the stack jmp @Done ; Perform an unconditional jump to the label @Done @NoCreateFileError: pushs ds:[fName] ; 2nd: Push the address of the fName variable onto the top of the stack leaax ds:[cSuccess] ; 1st: Load the address of the cSuccess format string into the ds:ax register call PrintFormatString ; Call the PrintFormatString(ax:cSuccess, fName) procedure add sp, 2 ; Align the stack @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
File 'test.txt' was created successfully!
Example taken from the textbook "HANDYFAR - MASM (Evgeny Sergeevich Dovgopolov)".