Issue with Displaying Random Characters and Sound Playback Interruption in Assembly Code

41 views Asked by At
IDEAL
MODEL small
stack 100h
DATASEG
freqs dw 165, 165, 196, 196, 220, 220, 196, 174, 165, 147, 147, 131, 131, 123, 123, 110, 110, 98, 98, 87, 87, 73, 73
times db  2,   2,   2,   2,   2,   2,   4,   2,   2,   2,   2,   2,   2,   4,   2,   2,   2,   2,   2,   2,   2,   4,   0
multi db 200

CODESEG
proc K_value  
    mov di, ax      ; frequency in DI
    mov dx, 12h     ; decimal number 1190000 corresponds to 
    mov ax, 2870h   ; hexadecimal number 122870h
    div di          ; DX:AX / DI = AX
    ret
endp K_value 

beats equ [bp+4]

proc pauses
    push bp
    mov bp, sp
    in al, 61h
    and al, 11111100b
    out 61h, al

    mov cx, bx      ; save the duration address in CX
    pop bx          ; retrieve the duration value from the stack
    mov al, [bx]    ; retrieve the duration value
    mov ah, 0
    inc bx          ; increment the duration address by 1
    push bx         ; push it back onto the stack 

    add cx, 2       ; increment the frequency address by 2
    push cx    

    mul [multi]     ; AX = AL * MULTI 
    call suspension

    pop bp
    ret 2
endp pauses 

freq equ [bp+4]

proc NoteItself
    push bp
    mov bp, sp
    in al, 61h
    or al, 00000011b
    out 61h, al

    mov al, 0B6h
    out 43h, al

mov ax, freq
out 42h, al ; Sending lower byte
mov al, ah
out 42h, al ; Sending upper byte

pop bp
    ret 4
endp NoteItself

proc suspension
    mov di, 1000    ; Delay multiplier
    mul di          ; DX:AX = CX * AX (in AX: duration of the note * multi)
    mov cx, dx      ; Load high byte into CX
    mov dx, ax      ; Load low byte into DX
    mov ax, 8600h
    int 15h         ; Wait for microseconds

    in al, 61h      
    and al, 11111100b
    out 61h, al     ; Turn off speaker

    mov cx, 0       ; Pause between notes
    mov dx, 10000   
    mov ax, 8600h
    int 15h         ; Wait for microseconds

    ret
endp suspension

start:
    mov ax, @data
    mov ds, ax

    myloop:
    lea bx, [times] ; Load effective address of durations array
    push bx         ; Save it on the stack
    lea bx, [freqs] ; Load effective address of frequencies array
    push bx     
    pop bx      
    push [bx]
    call NoteItself
    pop bx
    push [bx+1]
    call pauses
    pop bx
    push [bx+2]
    call NoteItself
    pop bx
    push [bx+3]
    call pauses
    pop bx
    push [bx+4]
    call NoteItself
    pop bx
    push [bx+5]
    call pauses
    pop bx
    push [bx+6]
    call NoteItself
    pop bx
    push [bx+7]
    call NoteItself
    pop bx
    push [bx+8]
    call pauses
    pop bx
    push [bx+9]
    call NoteItself
    pop bx
    push [bx+10]
    call pauses
    pop bx
    push [bx+11]
    call NoteItself
    pop bx
    push [bx+12]
    call pauses
    pop bx
    push [bx+13]
    call NoteItself
    pop bx
    push [bx+14]
    call pauses
    pop bx
    push [bx+15]
    call NoteItself
    pop bx
    push [bx+16]
    call pauses
    pop bx
    push [bx+17]
    call NoteItself
    pop bx
    push [bx+18]
    call pauses
    pop bx
    push [bx+19]
    call NoteItself
    pop bx
    push [bx+20]
    call pauses
    pop bx
    push [bx+21]
    call NoteItself
    pop bx
    push [bx+22]
    call pauses
    pop bx
    push [bx+23]
    call NoteItself

    jmp myloop

stop:
    in al, 61h
    and al, 11111100b
    out 61h, al      ; Disable speaker
    ret

exit:
     in al,61h
                and al,11111100b
                out 61h,al  ;disable speaker

                mov ah, 8
                int 21h
                mov ah,4ch      ;exit to DOS
                int 21h
END start

I'm encountering a problem with my assembly code, where it displays random characters on the console and plays only a split second of sound before stopping. I'm using a code snippet that's supposed to play a sequence of notes with pauses in between. However, when I run the code, it produces unexpected output on the console and the sound playback is abruptly interrupted.

Here's a summary of the issues:

Random Characters: The program outputs random characters on the console instead of the expected behavior. Sound Playback Interruption: The sound playback stops abruptly after playing only a split second of sound. I've verified the correctness of the code, and it seems to be structured properly for playing the notes with pauses. However, I suspect there might be issues related to system configuration, hardware compatibility, or a bug in the code itself.

I've already tried the following troubleshooting steps:

Checked sound hardware to ensure it's functional. Confirmed the program is running in a pure DOS environment. Verified the compatibility of hardware and the DOS environment. Checked sound volume to ensure it's not muted. Added debugging statements to track the flow of the program. Despite these efforts, the issue persists. I'm seeking advice on how to further diagnose and resolve the problem to achieve the intended behavior of the code. Any insights or suggestions would be greatly appreciated. Thank you.

0

There are 0 answers