【问题标题】:I don't understand why my program isn't working我不明白为什么我的程序不工作
【发布时间】:2018-04-15 15:57:25
【问题描述】:

我正在尝试编写一个使用中断 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

为什么它不起作用??

【问题讨论】:

  • 我不知道汇编,所以我不确定IDEAL 是否需要缩进,甚至我是否修复了格式。
  • 您可能想在帮助中心阅读stackoverflow.com/help/how-to-ask
  • notepad++ 在这里绝对无关紧要,所以这对您来说很紧迫。而“不工作”并不是一条有用的信息
  • 在什么情况下它不起作用?您期望的行为是什么?它会做什么?
  • “它不起作用”不是错误描述。告诉我们您预计会发生什么以及会发生什么。

标签: assembly dos x86-16 tasm


【解决方案1】:
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 过程中,DOS 函数 3Dh 返回并设置了进位标志。发生这种情况很可能是因为找不到该文件,仅仅是因为它不存在!
为了让您开始,更改程序以包含 CreateFile 过程。记得把call OpenFile改成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 继续程序是不够的。
您需要放弃该程序,因为后续操作无论如何都不会成功。


DOS 函数 40h (WriteToFile) 和 3Eh (CloseFile) 也通过 CF 报告可能的错误。确保以类似的方式抓住那个进位。

【讨论】:

    猜你喜欢
    • 2018-11-20
    • 2019-06-10
    • 1970-01-01
    • 1970-01-01
    • 2023-02-05
    • 1970-01-01
    • 1970-01-01
    • 2020-08-27
    • 2014-04-20
    相关资源
    最近更新 更多