MASM - 为什么此代码不会导致计算机发出蜂鸣声?
我已经阅读了有关使计算机发出蜂鸣声的代码,但我无法让它工作。以下代码要求用户输入数字。数字显示在屏幕上,但如果用户键入非数字字符,它应该发出蜂鸣声。当我运行程序并输入非数字字符时,它就会崩溃。
INCLUDE Irvine32.inc
.data
enterDigits BYTE 'Please type some digits: ', 0
.code
main PROC
mov EDX, OFFSET enterDigits
call WriteString
L1:</b>
call ReadChar
cmp AL, 0Dh
je FINISHED
cmp AL, 30h
jl BEEP
cmp AL, 39h
jg BEEP
call WriteChar
loop L1
BEEP:</b>
mov AH, 02h
mov DL, 07h
int 21h
jmp L1
FINISHED:</b>
call CRLF
call CRLF
exit</b>
main ENDP</b>
END main
If I replace BEEP with:
BEEP:</b>
mov AL, 33h
call WriteChar
jmp L1
当您键入非数字字符时,它将打印 3。不知道这是否有什么区别。
I've read about the code to make the computer beep but I can't get it to work. The following code asks the user to enter digits. The digits are displayed on the screen but if the user types a non-digit character it's supposed to beep. When I run the program and type a non-digit character, it just crashes.
INCLUDE Irvine32.inc
.data
enterDigits BYTE 'Please type some digits: ', 0
.code
main PROC
mov EDX, OFFSET enterDigits
call WriteString
L1:</b>
call ReadChar
cmp AL, 0Dh
je FINISHED
cmp AL, 30h
jl BEEP
cmp AL, 39h
jg BEEP
call WriteChar
loop L1
BEEP:</b>
mov AH, 02h
mov DL, 07h
int 21h
jmp L1
FINISHED:</b>
call CRLF
call CRLF
exit</b>
main ENDP</b>
END main
If I replace BEEP with:
BEEP:</b>
mov AL, 33h
call WriteChar
jmp L1
It will print a 3 when you type a non-digit character. Don't know if that makes any difference or not.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您在此处查看 WriteChar 的源代码,您可以看到它正在调用 Windows API WriteConsole 方法而不是使用 MSDOS API。我相信您将无法调用 DOS API 方法,因为您运行的模式与管理硬件和 I/O 功能的内核不兼容。
您可以尝试调整 exe 的兼容性模式以使用 Windows 95(右键单击 EXE,转到“兼容性”选项卡)。
If you look at the source for WriteChar here, you can see that it is invoking the Windows API WriteConsole method rather than use an MSDOS API. I believe you aren't going to be able to call a DOS API method because you're running in a mode incompatible with the kernel managing the hardware and I/O functions.
You might try adjusting the compatibility mode of your exe to use Windows 95 (right click on EXE, go to Compatibility tab).