Search code examples
assemblydos

Writing to a file in Assembly x86


I need to write data to a txt file in assembly, without erasing the current data (that is, to add data, not to replace it). Right now whenever I write I replace everything currently in the file.

Here is the code I am using:

writeFile Proc  
            push ax bx cx dx

            ;open file
            mov dx, offset fileName
            mov cx, 0
            mov ah,3ch
            int 21h
            mov filePointer, ax

            ;write to file
            mov dx, offset dataToWrite ;data
            mov cx, DATA_TO_WRITE_LEN  ;size of data
            mov bx, filePointer
            mov ah, 40h
            int 21h

            ;close file
            mov bx, filePointer
            mov ah, 3eh
            int 21h 
            pop dx cx bx ax
            ret
writeFile endp

Is there any way to do this? Thanks:)


Solution

  • You are calling

            mov ah,3ch
            int 21h
    

    Ie, create a file. If you use this call on an existing file that file will be truncated. You should use

            mov ah,3dh
            int 21h
    

    with the appropriate flags in AL to open the file.