In the last article tutorial, I wrote about how to write a message to a text file named file.txt.
Now, I will show you how to read that message.
You must use the ReadFile function defined after fileapi.h, in WIN32AX.INC.
The text is shown in modal dialog – has some limitations in terms of mode or the number of lines.
In the source code that writes the file, I don’t use CreateFile, but you can use it.
There are many exceptions and peculiarities in assembly language programming.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | format pe console 4.0 include 'INCLUDE/WIN32AX.INC' ; define data for code .data filename db 'file.txt',0 header_file dd ? size dd ? nr_of_bytes_to_read dd ? pointer_to_the_buffer rb 76 MessageBoxCaption db 'Output:',0 ; the code for running .code main: invoke CreateFile, filename, GENERIC_READ, 0, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0 mov [header_file], eax invoke GetFileSize, [header_file], 0 mov [size], eax ; https://learn.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-readfile invoke ReadFile, [header_file], pointer_to_the_buffer, [size], nr_of_bytes_to_read, 0 invoke CloseHandle, [header_file] ; will show a modal dialog with the content of the file ; NOTE: Modal dialog has some limitations in terms of mode or number of lines invoke MessageBox, NULL, addr pointer_to_the_buffer, addr MessageBoxCaption, MB_OK invoke ExitProcess, 0 .end main |
You can use the same commands to create the EXE file and run it:
1 2 3 4 5 | fasm read_file_001.asm flat assembler version 1.73.30 (1048576 kilobytes memory) 4 passes, 0.2 seconds, 2048 bytes. C:\fasmw17330>read_file_001.exe |