In this simple example, I will show how to write a file using the assembly programming language with FASM, known as flat assembler.
You can use the FASM editor with the FASMW.EXE executable.
Open this editor and use this commented source code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | ;define format of exe file format pe console 4.0 ;include file WIN32AX ;http://flatassembler.net/docs.php?article=win32 include 'INCLUDE/WIN32AX.INC' ; define data for code .data ;this will be the filename of file filename db 'file.txt',0 ; this string will be write on the file buffer_text db 'This is a test by Catalin!' ; this is the size for WriteFile based on buffer_text buffer_size = $ - buffer_text ; define a space bytes_to_write dd ? ; the code for running .code main: invoke CreateFile,filename, GENERIC_WRITE, 0, 0, 4, FILE_ATTRIBUTE_NORMAL, 0 ;https://learn.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-writefile invoke WriteFile, eax, buffer_text, buffer_size, bytes_to_write, 0 ;https://learn.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-exitprocess invoke ExitProcess, 0 .end main |
Each step is describe in the source code.
Run and test with these commands:
1 2 3 4 5 6 7 8 | fasm write_file_001.asm flat assembler version 1.73.30 (1048576 kilobytes memory) 3 passes, 0.1 seconds, 2048 bytes. write_file_001.exe type file.txt This is a test by Catalin! |