FileWrite.

Laszlo TataiLaszlo Tatai
1 min read

The following program creates and writes a file.

.386 
.model flat,stdcall 
option casemap:none 
include \masm32\include\windows.inc 
include \masm32\include\kernel32.inc 
includelib \masm32\lib\kernel32.lib 
include \masm32\include\user32.inc 
includelib \masm32\lib\user32.lib 

.data 
filename DB "output.txt", 0
buffer   DB "Hello, world!", 0

.code 
main PROC

INVOKE CreateFile,
            ADDR filename,  
            GENERIC_WRITE,
            0,
            NULL,
            CREATE_NEW,
            FILE_ATTRIBUTE_NORMAL,
            NULL

    ; Error check.
    .IF EAX == INVALID_HANDLE_VALUE
        INVOKE ExitProcess, 1
    .ENDIF

    ; Writing to file.
    INVOKE WriteFile,
            EAX,                   ; File handle
            ADDR buffer,           ; Address of data.
            LENGTHOF buffer,       ; Number of bytes to be written
            0,                     ; Number of bytes written (output parameter)
            NULL                   ; Overlap information (not used)

    ; Close file.
    INVOKE CloseHandle, EAX

    invoke ExitProcess,NULL
main ENDP
END main

Compile and run:
Link : How to compile our console assebmly file, using MASM32.

The output.txt file will be created as a result of the run, open it and check, that "Hello World!" does it include.

0
Subscribe to my newsletter

Read articles from Laszlo Tatai directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Laszlo Tatai
Laszlo Tatai

I am happy to help beginner programmers.