我写了一个简单的程序,其中一个按
这三个按钮工作得非常好,现在我想要的是这样的:当我按下“4”时,程序读取该文本文件(我之前按下“1”)。现在我还编写了打开文本文件的代码,它作为一个独立的工作正常。这是:
org 100h ; .com memory layout
mov dx, offset file ; address of file to dx
mov al,0 ; open file (read-only)
mov ah,3dh
int 21h ; call the interupt
jc terminate ; if error occurs, terminate program
mov bx,ax ; put handler to file in bx
mov cx,1 ; read one character at a time
print:
lea dx, BUF
mov ah,3fh ; read from the opened file (its handler in bx)
int 21h
CMP AX, 0 ; how many bytes transfered?
JZ terminate
mov al, BUF
mov ah,0eh ; print character (teletype).
int 10h
jmp print ; repeat if not end of file.
terminate:
mov ah, 0 ; wait for any key...
int 16h
ret
file db "c:\finaltest.txt", 0
BUF db ?
END
但是当我将这些代码导入我的主程序时,它无法打开该文件。这是完整的程序,我希望程序在按“4”时读取该文本文件,但是程序只是重新加载:
.model small
.stack 100h
.data
msg1 db 10, 13, 10, 13, "Please select an item:",0Dh,0Ah,0Dh,0Ah,09h
db "1- Create File",0Dh,0Ah,09h
db "2- About",0Dh,0Ah,09h
db "3- Exit",0Dh,0Ah,09h
db "4- Open File",0dh,0ah,09h
db "Enter item number: "
db '$'
About db 10, 13, 10, 13, "Blank Text About the Program$"
handle dw ?
file1 db "c:\finaltest.txt", 0
text db "Contains Message",0
text_size equ $ - text
.code
main proc
mov ax,@data
mov ds,ax
ShowMenu:
lea dx, msg1
mov ah, 09h
int 21h
getnum:
mov ah, 1
int 21h
cmp al, '1'
jl ShowMenu
cmp al, '3'
jg ShowMenu
cmp al, "1"
je CreateFile
cmp al, "2"
je ShowAbout
cmp al, "3"
jmp Quit
cmp al, "4"
jmp OpenFile
Quit:
mov ah,4ch
int 21h
Showabout:
lea dx, About
mov ah, 09h
int 21h
jmp ShowMenu
CreateFile:
jmp new
text_size = $ - offset text
new:
mov ah, 3ch
mov dx, offset file1
int 21h
mov handle, ax
mov ah, 40h
mov bx, handle
mov dx, offset text
mov cx, text_size
int 21h
int 21h
ret
OpenFile:
jmp print
mov dx, offset file
mov al,0
mov ah,3dh
int 21h
jc terminate
mov bx,ax
mov cx,1
print:
lea dx, BUF
mov ah,3fh
int 21h
CMP AX, 0
JZ terminate
mov al, BUF
mov ah,0eh
int 10h
jmp print
terminate:
mov ah, 0
int 16h
ret
file db "c:\finaltest.txt", 0
BUF db ?
END
jmp ShowMenu
main endp
end main
答案 0 :(得分:1)
查看您的代码,我发现了多个问题:
OpenFile
无法打开文件;有一个奇怪的jmp print
跳过了函数的第一部分。CreateFile
和OpenFile
都不会关闭文件。我不确定模拟环境,但在生产中,你将创建一个空文件。int 21h
末尾的CreateFile
重复;复制/粘贴错误?ret
没有任何call
的使用。file
和BUF
位于.code
段,应该在.data
中(尽管这可能不是您正在使用的模拟器中的问题)。解决方案:
jmp print
开头的OpenFile
。int 21h
。ret
替换为jmp ShowMenu
。.data
和file
。BUF
醇>
之后,您可能需要进行更多调试才能使一切正常运行。
关闭文件的代码示例:
mov ah, 3Eh
mov bx, handle
int 21h