VOGONS

Common searches


First post, by tigrou

User metadata
Rank Newbie
Rank
Newbie

I wrote the following code, which is supposed to output a 60 Hz square wave to the PC speaker :

org 100h
loop:

in al, 61h
or al, 00000010b ;turn pc speaker on
out 61h, al

call _wait

in al, 61h
and al, 11111101b ;turn pc speaker off
out 61h, al

call _wait

jmp loop

_wait:
mov cx, 00h
mov dx, 411ah ;wait 16.666ms
mov ah, 86h
int 15h
ret

It can be compiled under NASM (http://www.nasm.us/) using the following command :

nasm sound.asm -o sound.com

The PC speaker is turned on/off using bit 1, as described here : http://wiki.osdev.org/PC_Speaker#The_Raw_Hardware

However, when run under DOSBOX it does not works as expected (I did not try on real hardware) :
- It only output sound for a short time, then silence. Why ?
- The sound is heavily distorted. It does not sounds at all like a 100% perfect square wave.

How can I fix this ?

Important : as stated in the OSDev page, the PC Speaker can be controlled using the PIT. However this is not what i want. I want to control the PC speaker directly by sending one and zeroes (in order to output something different than a square wave).

Last edited by tigrou on 2016-11-11, 23:33. Edited 2 times in total.

Reply 3 of 3, by tigrou

User metadata
Rank Newbie
Rank
Newbie

OK the problem came from the _wait procedure. Replacing it by something simpler works perfectly :

_wait:
mov cx, 10000
delay_loop:
loop delay_loop

This is of course CPU frequency dependent, but it's not a problem for what I want.