Search code examples
assemblymasmirvine32

MASM - Why doesn't this code cause the computer to beep?


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.


Solution

  • 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).