Assembly 在程序集中的文本文件末尾追加
我这里有一个TASM代码,当我再次运行程序时,它不会附加新字符串。我要感谢你帮助我达到这个“状态”。Assembly 在程序集中的文本文件末尾追加,assembly,file-io,append,tasm,dosbox,Assembly,File Io,Append,Tasm,Dosbox,我这里有一个TASM代码,当我再次运行程序时,它不会附加新字符串。我要感谢你帮助我达到这个“状态”。 如果我使用3Ch,程序可以工作,但不会附加到文件末尾。通过使用6Ch而不是3Ch,表示它不会将现有文件截断为零字节,但每当我运行代码时,创建文件时就会出现错误(未创建任何文件)。请帮我修改代码。多谢各位 编辑2: ; This example program creates a file and then writes to it. .model small .stack .data CR
如果我使用
3Ch
,程序可以工作,但不会附加到文件末尾。通过使用6Ch
而不是3Ch
,表示它不会将现有文件截断为零字节,但每当我运行代码时,创建文件时就会出现错误(未创建任何文件)。请帮我修改代码。多谢各位编辑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
看看这部分:
mov dx,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
及
函数期望si
而不是dx
具有文件名的偏移量
您不需要设置bx
您不需要设置dx
您不会检查错误
您不保存文件句柄
看看这部分:
mov dx,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
句柄
此时未初始化,因为您从未将句柄保存到此变量中
现在,这里:
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
您正在尝试再次打开打开的文件。你需要先把它关上再打开。或者,如果您有句柄,只需继续处理该文件,不要关闭并重新打开它。看看这部分:
mov dx,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
及
函数期望si
而不是dx
具有文件名的偏移量
您不需要设置bx
您不需要设置dx
您不会检查错误
您不保存文件句柄
看看这部分:
mov dx,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
句柄
此时未初始化,因为您从未将句柄保存到此变量中
现在,这里:
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
您正在尝试再次打开打开的文件。你需要先把它关上再打开。或者,如果你有句柄,就继续处理文件,不要关闭然后重新打开它。谢谢你,但老实说,我没那么了解它。我现在将BX设置为2(读/写访问模式),DX设置为3(文件存在行为)()我将
DX
替换为SI
(在偏移文件名中),但打开文件时出错。关于Handle
未初始化,我在mov-bx,Handle
之前放置了一个mov-ax,Handle
。但仍然是错误。如果我使用3Ch
,程序实际上可以工作,问题是它没有附加。请帮我谢谢。请看我问题中的编辑。这就是我所做的,但由于代码中的jc
条件,它表示在创建时发生了错误。我设置了BX=3
(读+写),而不是BX=2
(只读)。另外,DX=3
无效,未在表中作为有效值列出。请尝试12H
。你还没有先关闭文件就重新打开了。非常感谢!应该使用si
,而不是dx
,因为每当我使用si
时,它不会追加。很抱歉BX=3
,看起来BX=2
是正确的。然而,我知道的3个引用都说函数6Ch
在SI
中取名称偏移量。谢谢你,但老实说,我没有得到那么多。我现在将BX设置为2(读/写访问模式),DX设置为3(文件存在行为)()我将DX
替换为SI
(在偏移文件名中),但打开文件时出错。关于Handle
未初始化,我在mov-bx,Handle
之前放置了一个mov-ax,Handle
。但仍然是错误。如果我使用3Ch
,程序实际上可以工作,问题是它没有附加。请帮我谢谢。请看我问题中的编辑。这就是我所做的,但由于代码中的jc
条件,它表示在创建时发生了错误。我设置了BX=3
(读+写),而不是BX=2
(只读)。另外,DX=3
无效,未在表中作为有效值列出。请尝试12H
。你还没有先关闭文件就重新打开了。非常感谢!应该使用si
,而不是dx
,因为每当我使用si
时,它不会追加。很抱歉BX=3
,看起来BX=2
是正确的。然而,我知道的3个引用都说函数6Ch
在SI
中取名称偏移量。