我不明白为什么我的程序无法正常工作 [英] I don't understand why my program isn't working

查看:80
本文介绍了我不明白为什么我的程序无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个在21h中断时读写文本文件的代码.

I am trying to write a code that read and write text file with interrupt 21h.

这是我的代码:

IDEAL
MODEL small
STACK 100h
DATASEG
filename db 'testfile.txt',0
filehandle dw ?
Message db 'Hello world!'
ErrorMsg db 'Error', 10, 13,'$'
CODESEG
proc OpenFile
; Open file for reading and writing
mov ah, 3Dh
mov al, 2
mov dx, offset filename
int 21h
jc openerror
mov [filehandle], ax
ret
openerror:
mov dx, offset ErrorMsg
mov ah, 9h
int 21h
ret
endp OpenFile
proc WriteToFile
; Write message to file
mov ah,40h
mov bx, [filehandle]
mov cx,12
mov dx,offset Message
int 21h
ret
endp WriteToFile
proc CloseFile
push ax
push bx
; Close file
mov ah,3Eh
mov bx, [filehandle]
int 21h
pop bx
pop ax
ret
endp CloseFile
start:
mov ax, @data
mov ds, ax
; Process file
call OpenFile
call WriteToFile
call CloseFile
quit:
mov ax, 4c00h
int 21h
END start

为什么不起作用?

推荐答案

proc OpenFile
; Open file for reading and writing
mov ah, 3Dh
mov al, 2
mov dx, offset filename
int 21h
jc openerror
mov [filehandle], ax
ret
openerror:
mov dx, offset ErrorMsg
mov ah, 9h
int 21h
ret               <--- This is an extra problem!
endp OpenFile

是您的程序在屏幕上显示消息错误".
这样做是因为在 OpenFile proc中,DOS函数3Dh返回了带有进位标志的设置.发生这种情况很可能是因为找不到该文件,仅仅是因为该文件尚不存在!
要开始使用,请更改程序以包含 CreateFile proc.请记住将call OpenFile更改为call CreateFile.

It's your program that displays the message 'Error' on screen.
It does so because in the OpenFile proc, the DOS function 3Dh returned with the carry flag set. This happens most probably because the file wasn't found, simply because it didn't exist already!
To get you started, change the program to include a CreateFile proc. Remember to change the call OpenFile into call CreateFile.

proc CreateFile
  mov dx, offset filename
  xor cx, cx
  mov ah, 3Ch
  int 21h
  jc  CreateError
  mov [filehandle], ax
  ret
 CreateError:
  mov dx, offset ErrorMsg
  mov ah, 9h
  int 21h
  jmp Quit          <--- Solution to the extra problem!
endp CreateFile

请注意,当DOS报告错误时,仅显示一条消息并不足以使用ret继续运行该程序是不够的.
您需要放弃该程序,因为无论如何后续操作都不会成功.

Do notice that when an error is reported by DOS, it's not enough to just display a message and then happily continu with the program using ret.
You need to abandon the program because subsequent actions would not be successful anyway.

DOS功能40h(WriteToFile)和3Eh(CloseFile)也通过CF报告可能的错误.确保以类似的方式抓住那些随身携带的东西.

The DOS functions 40h (WriteToFile) and 3Eh (CloseFile) also report possible errors via the CF. Make sure to catch that carry in a similar fashion.

这篇关于我不明白为什么我的程序无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆