[Assembly] How to write into a file

Discussion in 'Programmer's Corner' started by ahmedbj, Jan 17, 2010.

  1. ahmedbj

    Thread Starter New Member

    Jan 16, 2010
    Hello everyone
    I'm posting this new thread because the old one was closed by a moderator
    i'm a beginner with assembly langage, i have created a code that open a file and try to modify it
    and i have problem when i want to modify it

    her is my code :
    ; multi-segment executable file template.

    data segment
    dir1 db "C:\WINDOWS\system32\drivers\etc\hosts",0
    dir2 db "d:\hamidoune",0
    mesg db "Fichier ouvert",0
    erro db "Fichier non ouvert",0
    link1 dw " www.site.com",0
    t dw $-offset link1
    handle dw ?
    pkey db "Press any key...$"

    stack segment
    dw 128 dup(0)

    code segment
    ; set segment registers:
    mov ax, data
    mov ds, ax
    mov es, ax

    ;Ouverture du fichier
    mov ah,3dh;
    mov dx,offset dir1;
    mov al,2;
    int 21h
    JC erreur ;

    mov handle,ax;

    ;ecrir dANS le fichier
    mov ah,40H
    mov bx,handle
    mov cx,t
    mov dx,offset link1

    int 21h

    mov mistage,dx

    mov dx, offset mesg
    mov ah,09h
    int 21h
    JMP fin

    erreur :
    mov ah,09h
    mov dx, offset erro
    int 21h

    lea dx, pkey
    mov ah, 9
    int 21h ; output string at ds:dx

    ; wait for any key....
    mov ah, 1
    int 21h

    mov ax, 4c00h ; exit to operating system.
    int 21h

    end start ; set entry point and stop the assembler.

    if anyone can please, show me how to write at the end of file so to not delete the information that's already exist in the file, and how i can delete on line
    Thank you for helping me:)
    and Thank you 'RETCHED' for help :)
  2. rjenkins

    AAC Fanatic!

    Nov 6, 2005
    I don't know the assembly details offhand, but I believe you need to 'seek' to the end of the file before you start to write new data.
  3. Papabravo


    Feb 24, 2006
    You could also open the file in append mode, which adds new records at the end.