The content comes from Intel assembly language programming (Fourth Edition) Chapter 11th -------- 32-bit windows programming.
We should first write the content into the file (if there is no file, a new file will be created), and then read the content from the file.
First, let's take a look at writing data to a file:
Title using writefile (writefile. ASM)
Include irvine32.inc
. Data
Buffer Byte "this text is written to an output file.", 0dh, 0ah
Bufsize = ($-buffer)
Errmsg Byte "cannot create file", 0dh, 0ah, 0
Filename Byte "output.txt", 0
Filehandle DWORD? ; Handle to output file
Byteswritten DWORD? ; Number of bytes written
. Code
Main proc
Invoke createfile,
ADDR filename, generic_write, do_not_share, null,
Create_always, file_attribute_normal, 0
MoV filehandle, eax; save file handle
. If eax = invalid_handle_value
MoV edX, offset errmsg; Display error message
Call writestring
JMP quitnow
. Endif
Invoke writefile,; write text to file
Filehandle,; file handle
ADDR buffer,; buffer pointer
Bufsize,; number of bytes to write
ADDR byteswritten,; number of bytes written
0; overlapped execution flag
Invoke closehandle, filehandle
Quitnow:
Invoke exitprocess, 0; End Program
Main endp
End main
The program first uses the createfile function to open a file:
Invoke createfile,
ADDR filename, generic_write, do_not_share, null,
Create_always, file_attribute_normal, 0
Because the create_always flag is used here, the existing files will be overwritten.
The following code:
MoV filehandle, eax; save file handle
. If eax = invalid_handle_value
MoV edX, offset errmsg; Display error message
Call writestring
JMP quitnow
. Endif
First, save the handle to filehandle, and then judge whether the file is successfully opened. If not, an error statement is output and then jump to quitnow to exit.
If a handle is obtained, the program will use writefile to write the string to the file:
Invoke writefile,; write text to file
Filehandle,; the handle value obtained earlier
ADDR buffer,; the first address of the string to be entered
Bufsize,; the length of the data to be written
ADDR byteswritten,; number of bytes of data actually written after the function is executed. byteswritten is the returned value.
0; asynchronous information pointer. this parameter is optional. 0 is input here.
After writing data to a file, we need to close the file:
Invoke closehandle, filehandle
In this case, we only need to pass in the handle of the opened file.
The code for writing data to the file ends here. The following code reads data from a file:
Title using readfile (readfile. ASM)
Include irvine32.inc
. Data
Buffer byte 500 DUP (?)
Bufsize = ($-buffer)
Errmsg Byte "cannot open file", 0dh, 0ah, 0
Filename Byte "output.txt", 0
Filehandle DWORD? ; Handle to output file
Bytecount DWORD? ; Number of bytes written
. Code
Main proc
Invoke createfile,; open file for Input
ADDR filename, generic_read,
Do_not_share, null, open_existing,
File_attribute_normal, 0
MoV filehandle, eax; save file handle
. If eax = invalid_handle_value
MoV edX, offset errmsg; Display error message
Call writestring
JMP quitnow
. Endif
Invoke readfile,; read file into Buffer
Filehandle, ADDR buffer,
Bufsize, ADDR bytecount, 0
Invoke closehandle,; close the file
Filehandle
MoV ESI, bytecount; insert null Terminator
MoV buffer [esi], 0; into Buffer
MoV edX, offset buffer; display the buffer
Call writestring
Quitnow:
Invoke exitprocess, 0; End Program
Main endp
End main
The program still uses createfile to open the file to be read. The open_existing parameter is used here. Then, save the handle in eax to the variable. Call the readfile function, pass in the handle parameter, read the data to the buffer, and close the file handle.
The subsequent program is as follows:
MoV ESI, bytecount; first, assign the bytecount value returned by calling readfile to ESI.
MoV buffer [esi], 0; in this way, the address of the last element of the buffer is obtained. Because it is the end of the string, 0 is assigned to the last element.
MoV edX, offset buffer; assign the buffer's offset address to edX
Call writestring; print the data content in the buffer.
The code for reading the file ends.