The ScanFormatFile function
The ScanFormatFile function reads data from a file stream and stores the values in the corresponding variables. It works like ScanFormatString, but reads from a file by its handle.
Parameters: AX: pointer to the format string; BX: file handle. The other arguments (pointers to the variables) 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 (Example 1)
title .model small .stack 100h data segment public fName db "test.txt", 0 fmtstr db "%s %s %c %d" fmtout db "NAME: %.4s\nSURNAME: %s\nSEX: %.4c\nAGE: %d\0" mName db 20 dup(0) mSurname db 20 dup(0) mSex db ? mAge dw ? 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 lea ax, ds:[mAge] ; Copy the value of the variable mAge into the ds:ax register push ax ; 6th: Save the ax register value on the stack lea ax, ds:[mSex] ; Copy the value of the variable mSex into the ds:ax register push ax ; 5th: Save the ax register value on the stack lea ax, ds:[mSurname] ; Load the address of the variable mSurname into the ds:ax register push ax ; 4th: Save the ax register value on the stack lea ax, ds:[mName] ; Load the address of the variable mName into the ds:ax 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 string fmtstr into the ds:ax register call ScanFormatFile ; Call the ScanFormatFile procedure (ax:fmtstr, ...) add sp, 8 ; Align the stack push mAge ; 5th: Save the value of the variable mAge on the stack mov al, mSex ; Copy the value of the variable mSex into the al register push ax ; 4th: Save the ax register value on the stack lea ax, ds:[mSurname] ; Load the address of the variable mSurname into the ds:ax register push ax ; 3rd: Save the ax register value on the stack lea ax, ds:[mName] ; Load the address of the variable mName into the ds:ax register push ax ; 2nd: save the ax register value on the stack lea ax, ds:[fmtout] ; 1st: Load the address of the string fmtout into the ds:ax register call PrintFormatString ; Call the PrintFormatString procedure (ax:fmtout, ...) add sp, 8 ; Align the stack 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
Outputting the contents of the file test.txt to the console:
NAME: Evgeny
SURNAME: Dovgopolov
SEX: M
AGE: 36
Example taken from the textbook "HANDYFAR - MASM (Evgeny Sergeevich Dovgopolov)".
Program source code (Example 2: reading numbers)
title .model small .stack 100h data segment public fName db "test.txt", 0 fmtstr db "%d %d" fmtout db "%d + %d = %d\0" aValue dw ? bValue dw ? rValue dw ? 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 lea ax, ds:[bValue] ; Load the address of the variable bValue into the ds:ax register push ax ; 4th: Save the ax register value on the stack lea ax, ds:[aValue] ; Load the address of the variable aValue into the ds:ax 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 string fmtstr into the ds:ax register call ScanFormatFile ; Call the ScanFormatFile procedure (ax:fmtstr, aValue, bValue) add sp, 4 ; Align the stack mov ax, aValue ; Copy the value of the variable aValue into the ax register add ax, bValue ; Add the value of the variable bValue to the ax register mov rValue, ax ; Save the ax register value in the variable rValue push rValue ; 4th: Save the value of the variable rValue on the stack push bValue ; 3rd: Save the value of the variable bValue on the stack push aValue ; 2nd: Save the value of the variable aValue on the stack lea ax, ds:[fmtout] ; 1st: Load the address of the string fmtout into the ds:ax register call PrintFormatString ; Call the PrintFormatString procedure (ax:fmtout, ...) add sp, 6 ; 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
5 + 8 = 13
Example taken from the textbook "HANDYFAR - MASM (Evgeny Sergeevich Dovgopolov)".
Program source code (Example 3: reading numbers)
title .model small .stack 100h data segment public fName db "test.txt", 0 fmtstr db "%d + (%d * %d)" fmtout db "%d + (%d * %d) = %d\0" aValue dw ? bValue dw ? cValue dw ? rValue dw ? 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 lea ax, ds:[cValue] ; Load the address of the variable cValue into the ds:ax register push ax ; 5th: Save the ax register value on the stack lea ax, ds:[bValue] ; Load the address of the variable bValue into the ds:ax register push ax ; 4th: Save the ax register value on the stack lea ax, ds:[aValue] ; Load the address of the variable aValue into the ds:ax 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 string fmtstr into the ds:ax register call ScanFormatFile ; Call the ScanFormatFile procedure (ax:fmtstr, ...) add sp, 6 ; Align the stack mov ax, bValue ; Copy the value of the variable bValue into the ax register mov bx, cValue ; Copy the value of the variable cValue into the bx register mul bx ; Multiply the value in the ax register by the value in bx add ax, aValue ; Add the value of the variable aValue to the ax register mov rValue, ax ; Save the ax register value in the variable rValue push rValue ; 5th: Save the value of the variable rValue on the stack push cValue ; 4th: Save the value of the variable cValue on the stack push bValue ; 3rd: Save the value of the variable bValue on the stack push aValue ; 2nd: Save the value of the variable aValue on the stack lea ax, ds:[fmtout] ; 1st: Load the address of the string fmtout into the ds:ax register call PrintFormatString ; Call the PrintFormatString procedure (ax:fmtout, ...) add sp, 8 ; 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
2 + (2 * 2) = 6
Example taken from the textbook "HANDYFAR - MASM (Evgeny Sergeevich Dovgopolov)".