Assembly 程序集8086:在文件末尾追加

Assembly 程序集8086:在文件末尾追加,assembly,Assembly,我知道这是一个简单的问题,但我真的不知道如何解决这个问题。 我正在使用这个代码 mov bx, handle mov dx, offset data mov cx, 100 mov ah, 40h int 21h 写入文件。但我需要“更新”它;有点像在它的末尾追加。可能吗?如果是,我会怎么做?有具体的说明吗?谢谢 这是我的代码: .model small .data filename db "test1.txt", 0 handle dw ? data db "writ

我知道这是一个简单的问题,但我真的不知道如何解决这个问题。 我正在使用这个代码

mov bx, handle
mov dx, offset data
mov cx, 100
mov ah, 40h
int 21h
写入文件。但我需要“更新”它;有点像在它的末尾追加。可能吗?如果是,我会怎么做?有具体的说明吗?谢谢

这是我的代码:

.model small
.data
    filename db "test1.txt", 0
    handle dw ?
    data db "write me! "
    buffer db 200 dup(' ') 
    errormess db "Error in opening file!$"
.stack 100h
.code

    main proc

    mov ax, @data
    mov ds, ax

    mov AH,3dh
    mov AL,2
    lea dx,filename
    int 21h

    mov handle,AX
    jc erroropen
    jmp noerror
    erroropen:
        lea dx, errormess
        mov ah, 09h
        int 21h
        jmp exit
    noerror:

    mov bx, handle
    mov ah, 42h  ; "lseek"
    mov al, 2    ; position relative to end of file
    mov cx, 0    ; offset MSW
    mov dx, 0    ; offset LSW
    int 21h

    ;mov bx, handle
    mov dx, offset data
    mov cx, 100
    mov ah, 40h
    int 21h ; write to file...

    mov bx, handle
    mov ah, 3eh
    int 21h ; close file... 

    exit:
    mov ax, 4c00h
    int 21h

    main endp
    end main
可能使用(
lseek
)并在写入之前定位到文件末尾:

; assuming "bx" holds the file handle

mov ah, 42h  ; "lseek"
mov al, 2    ; position relative to end of file
mov cx, 0    ; offset MSW
mov dx, 0    ; offset LSW
int 21h

; current position (= file length) now in dx:ax
; write here...
(哇,我从没想过我会再摆弄DOS中断;-)

更新:组装为(您必须调整语法),测试为:

这可能比我今晚想做的更多:-)


刚刚用我的代码版本编辑了我的文章,它与nasm和dosemu“对我有用”;我试图坚持传统的英特尔语法,但您可能需要在这里或那里进行一些小的调整(
lea
mov-ax,@data;mov-ds,ax
,等等)。干杯
   org 100h

section .code

start:
   mov ah, 3dh
   mov al, 2
   mov dx, filename
   int 21h
   jc err_open

   mov [handle], ax

   mov bx, ax
   mov ah, 42h  ; "lseek"
   mov al, 2    ; position relative to end of file
   mov cx, 0    ; offset MSW
   mov dx, 0    ; offset LSW
   int 21h
   jc err_seek

   mov bx, [handle]
   mov dx, usermsg
   mov cx, 100
   mov ah, 40h
   int 21h ; write to file...
   jc err_write

   mov bx, [handle]
   mov ah, 3eh
   int 21h ; close file...
   jc err_close

exit:
   mov ax, 4c00h
   int 21h

err_open:
   mov dx, msg_open
   jmp error

err_seek:   
   mov dx, msg_seek
   jmp error

err_write:
   mov dx, msg_write
   jmp error

err_close:
   mov dx, msg_close
   ; fallthrough

error:
   mov ah, 09h
   int 21h

   mov ax, 4c01h
   int 21h


section .data

filename:  db "test1.txt", 0
handle:    dw 0
usermsg:   db "write me", 0
buffer:    times 200 db 0
msg_open:  db "Error opening file!$"
msg_seek:  db "Error seeking file!$"
msg_write: db "Error writing file!$"
msg_close: db "Error closing file!$"
% echo "bla" > ~/.dosemu/drive_c/test1.txt 
% dosemu