The PrintFormatFile function
The PrintFormatFile function is used to write a formatted string to a stream. It works like PrintFormatString, but writes the data to a file by its handle.
Parameters: AX: pointer to the format string; BX: file handle. The other arguments are passed through the stack.
Return value: The function does not return a value.
Flag state: The function does not modify the flags.
Program source code
An example program for the Intel 8086 assembler demonstrating the use of the PrintFormatFile function for formatted writing to a file.
title .model small .stack 100h data segment public fName db "test.txt", 0 fmtstr db "Character: %c\nString: %s\nDEC: %.3d\nHEX: %.3h\nOCT: %.3o\nBIN: %.3b\nFPU: %.3f\n\0" string db "Dovgopolov\0" fpunum dq 41.56 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 (ax:fName, bl:READ_WRITE) := AX and CF mov fHandle, ax ; Save the file handle in the variable fHandle fld qword ptr[fpunum] ; 9th: Load fpunum onto the top of the math coprocessor stack mov ax, 35 ; Copy the value 35 into the ax register push ax ; 8th: Save the ax register value on the stack push ax ; 7th: Save the ax register value on the stack push ax ; 6th: Save the ax register value on the stack push ax ; 5th: Save the ax register value on the stack lea ax, ds:[string] ; Load the address of the variable string into the ds:ax register push ax ; 4th: Save the ax register value on the stack mov al, 'A' ; Copy the character 'A' into the al register push ax ; 3rd: Save the ax register value on the stack mov bx, fHandle ; 2nd: Copy the value of the file handle fHandle into the bx register lea ax, ds:[fmtstr] ; 1st: Load the address of the format string fmtstr into the ds:ax register call PrintFormatFile ; Call the PrintFormatFile procedure (ax:fmtstr, ...) add sp, 12 ; Align the stack mov ax, fHandle ; Copy the file handle fHandle into the ax register call CloseHandle ; Call the CloseHandle procedure (ax:fHandle) 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
Contents of the file test.txt:
Character: A
String: Dovgopolov
DEC: 35
HEX: 0023
OCT: 43
BIN: 0010 0011
FPU: 41.56
Example taken from the textbook "HANDYFAR - MASM (Evgeny Sergeevich Dovgopolov)".