1
我在這裏有一個TASM代碼,並且在我再次運行程序時它不會追加新的字符串。我想感謝us2012幫助我達到這個「國家」。
在程序集中的文本文件末尾追加
; This example program creates a file and then writes to it.
.model small
.stack
.data
CR equ 13
LF equ 10
StartMessage DB "This program creates a file called NEW.TXT"
DB ,"on the C drive.$"
EndMessage DB CR,LF,"File create OK, look at file to"
DB ,"be sure.$"
WriteMessage DB "An error has occurred (WRITING)$"
OpenMessage DB "An error has occurred (OPENING)$"
CreateMessage DB "An error has occurred (CREATING)$"
WriteMe DB "HELLO, THIS IS A TEST, HAS IT WORKED?",0
FileName DB "new.txt",0 ; name of file to open
Handle DW ? ; to store file handle
.code
START:
mov ax,@data ; base address of data segment
mov ds,ax ; put it in ds
mov dx,offset StartMessage
mov ah,09h
int 21h
mov si,offset FileName ; put offset of filename in dx
xor cx,cx ; clear cx - make ordinary file
mov ah,6Ch ; function 3Ch - create a file
mov al, 0
int 21h ; call DOS service
mov bx, Handle
xor cx, cx
xor dx, dx
mov ah, 42h
mov al, 02h
int 21h
jc CreateError ; jump if there is an error
mov si,offset FileName ; put offset of filename in dx
mov al,2 ; access mode -read and write
mov ah,3Dh ; function 3Dh - open the file
int 21h ; call dos service
jc OpenError ; jump if there is an error
mov Handle,ax ; save value of handle
mov bx, Handle
xor cx, cx
xor dx, dx
mov ah, 42h
mov al, 02h
int 21h
mov dx,offset WriteMe ; address of information to write
mov bx,Handle ; file handle for file
mov cx,38 ; 38 bytes to be written
mov ah,40h ; function 40h - write to file
int 21h ; call dos service
jc WriteError ; jump if there is an error
cmp ax,cx ; was all the data written?
jne WriteError ; no it wasn't - error!
mov bx,Handle ; put file handle in bx
mov ah,3Eh ; function 3Eh - close a file
int 21h ; call dos service
mov dx,offset EndMessage
mov ah,09h
int 21h
ReturnToDOS:
mov ax,4C00h ; terminate program
int 21h
WriteError:
mov dx,offset WriteMessage
jmp EndError
OpenError:
mov dx,offset OpenMessage
jmp EndError
CreateError:
mov dx,offset CreateMessage
EndError:
mov ah,09h
int 21h
mov ax,4C01h
int 21h
END START
如果我使用3Ch
,該程序的作品,但它不附加到文件末尾。通過使用6Ch
代替3Ch
,this說,它不會截斷現有的文件,以零個字節,但每當我運行的代碼,在那裏創建該文件是一個錯誤(沒有文件被創建)。請幫我修復代碼。非常感謝你!
編輯2:
; This example program creates a file and then writes to it.
.model small
.stack
.data
CR equ 13
LF equ 10
StartMessage DB "This program creates a file called NEW.TXT"
DB ,"on the C drive.$"
EndMessage DB CR,LF,"File create OK, look at file to"
DB ,"be sure.$"
WriteMessage DB "An error has occurred (WRITING)$"
OpenMessage DB "An error has occurred (OPENING)$"
CreateMessage DB "An error has occurred (CREATING)$"
WriteMe DB "HELLO, THIS IS A TEST, HAS IT WORKED?",0
FileName DB "new3.txt",0 ; name of file to open
Handle DW ? ; to store file handle
.code
START:
mov ax,@data ; base address of data segment
mov ds,ax ; put it in ds
mov dx,offset StartMessage
mov ah,09h
int 21h
mov si, offset FileName ; put offset of filename in dx
xor cx,cx ; clear cx - make ordinary file
mov ah,6Ch ; function 3Ch - create a file
mov bx, 3
mov dx, 12h
int 21h ; call DOS service
jc CreateError ; jump if there is an error
mov ah,3Eh ; function 3Eh - close a file
int 21h ; call dos service
mov dx, offset FileName ; put offset of filename in dx
mov al,2 ; access mode -read and write
mov ah,3Dh ; function 3Dh - open the file
int 21h ; call dos service
jc OpenError ; jump if there is an error
mov Handle,ax ; save value of handle
mov bx, Handle
xor cx, cx
xor dx, dx
mov ah, 42h
mov al, 02h
int 21h
mov dx,offset WriteMe ; address of information to write
mov bx,Handle ; file handle for file
mov cx,38 ; 38 bytes to be written
mov ah,40h ; function 40h - write to file
int 21h ; call dos service
jc WriteError ; jump if there is an error
cmp ax,cx ; was all the data written?
jne WriteError ; no it wasn't - error!
mov bx,Handle ; put file handle in bx
mov ah,3Eh ; function 3Eh - close a file
int 21h ; call dos service
mov dx,offset EndMessage
mov ah,09h
int 21h
ReturnToDOS:
mov ax,4C00h ; terminate program
int 21h
WriteError:
mov dx,offset WriteMessage
jmp EndError
OpenError:
mov dx,offset OpenMessage
jmp EndError
CreateError:
mov dx,offset CreateMessage
EndError:
mov ah,09h
int 21h
mov ax,4C01h
int 21h
END START
謝謝,但老實說,我不明白這一點。我現在將BX設置爲2(讀/寫訪問模式)並將DX設置爲3(文件存在行爲)(http://stanislavs.org/helppc/int_21-6c.html)我用'DX'替換了'DX' 'SI'(在偏移量文件名中),但打開文件時出錯。關於未被初始化的'Handle',我在'mov bx,Handle'之前放了一個'mov ax,Handle'。但是,仍然是錯誤。該程序實際上工作,如果我使用'3Ch'和問題是它不會追加。請幫助我,謝謝你。 –
請參閱我的問題中的編輯。這就是我所做的,但是由於代碼中的'jc'條件,它說創建時發生錯誤。 –
我會設置'BX = 3'(讀取+寫入)而不是'BX = 2'(只讀)。另外,'DX = 3'無效,它在表中沒有列爲有效值。試試'12H'。而且您還沒有先關閉文件即可重新打開文件。 –