【发布时间】:2016-03-14 17:36:03
【问题描述】:
我正在编写一个 DOS 程序来创建一个文件并写入它。我必须在两条不同的行中写入数据。这是我的代码:
.model tiny
.486
.data
fname db 'file5.txt',0
handle dw ?
msg db 'Prashant Pandey 2014A7PS100G'
.code
.startup
mov ah,3eh
mov bx,handle
int 21h
mov ah,3ch ;the 4 lines from now open an new non-existing.3ch is used to open a new file
lea dx,fname
mov cl,2h
int 21h
mov handle,ax
mov ah,40h ;these 4 lines write to the already opened file
mov bx,handle
mov cx,28h
lea dx,msg
int 21h
; new line
mov dx,13
mov ah,2
int 21h
mov dx,10
mov ah,2
int 21h
mov ah,40h ;these 4 lines write to the already opened file
mov bx,handle
mov cx,28h
lea dx,msg
int 21h
mov ah,3eh
mov bx,handle
int 21h
.exit
end
我正在使用 MASM 和 DOSBOX。问题是程序虽然打印了两行不同的数据,但是在第2行之前,它还打印了一些奇怪的字符,然后打印了字符串msg。这个程序有什么问题,我该如何解决?
【问题讨论】:
-
似乎您想要的字符串长度为 28 字节(十进制),并且您请求使用
mov cx,28h写入 28 小时(十进制 40)字节。也许你的意思是mov cx,28? -
我的错。我试试看。
-
或者更好的是,在定义
msg之后立即使用msglength equ $-msg指令让汇编器计算字符串的长度。 -
当我在上面输入这条评论时,彼得提出了类似的建议。您可以让 TASM 为您计算字符串长度。将
msglen equ $-msg放在msg的定义之后。这将计算msg的大小(长度),然后您可以通过将mov cx, 28h替换为mov cx, msglen来利用该值。 -
另一个特点。您似乎没有将换行符写入文件,而是将它们显示到控制台(int 21h/ah=2 显示到标准输出)。您不应该将它们写入文件吗?您可以使用
msg db 'Prashant Pandey 2014A7PS100G', 13, 10之类的东西向msg本身添加换行符
标签: assembly dos masm dosbox x86-16