Programming
FASM: Write File
by admin on Feb.20, 2011, under Programming
The example code below will create and write text into a new file. In order to use WriteFile in flat assembler you either have to include ‘WIN32AX.INC’ or handle the imports from kernel32.dll manually. I’ve used the shorter version and included win32ax.in. This sample code should work on all operating systems using the Win32 API:
format pe console 4.0 include 'WIN32AX.INC' .data buf db 'TEST' bufsize = $ - buf byteswritten dd ? .code main: invoke CreateFile, 'test.txt', GENERIC_WRITE, 0, 0, 4, FILE_ATTRIBUTE_NORMAL, 0 invoke WriteFile, eax, buf, bufsize, byteswritten, 0 invoke ExitProcess, 0 .end main
If the file already exists, it won’t get deleted, but it will be simply overwritten.