MASM - 为什么此代码不会导致计算机发出蜂鸣声?

发布于 2024-12-15 16:37:45 字数 767 浏览 8 评论 0原文

我已经阅读了有关使计算机发出蜂鸣声的代码,但我无法让它工作。以下代码要求用户输入数字。数字显示在屏幕上,但如果用户键入非数字字符,它应该发出蜂鸣声。当我运行程序并输入非数字字符时,它就会崩溃。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

柒七 2024-12-22 16:37:46

如果您在此处查看 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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文