2

I'm trying to generate a sound with a specific frequency using 8086 assembly. I have searched and searched and found lots of codes regarding connecting to the speaker (not the PC speaker). Yet, none of them seem to work. The following is my code:

    mov     al, 182         ; meaning that we're about to load
    out     43h, al         ; a new countdown value

    mov     ax, 2153        ; countdown value is stored in ax. It is calculated by 
                            ; dividing 1193180 by the desired frequency (with the
                            ; number being the frequency at which the main system
                            ; oscillator runs
    out     42h, al         ; Output low byte.
    mov     al, ah          ; Output high byte.
    out     42h, al               

    in      al, 61h         
                            ; to connect the speaker to timer 2
    or      al, 00000011b  
    out     61h, al         ; Send the new value

I think this is supposed to produce a sound until we somehow manage to tell the speaker to turn off. Nonetheless, there's no sound to be heard. Can you please tell me what is wrong with the code?

tshepang
  • 12,111
  • 21
  • 91
  • 136
Farnaz Jahanbakhsh
  • 493
  • 2
  • 5
  • 15
  • That code does attempt to access the PC speaker. How are you running it? –  Jun 22 '13 at 15:49
  • In emu8086 emulator. Is there anything wrong with it? – Farnaz Jahanbakhsh Jun 22 '13 at 15:52
  • Based on what I'm reading [on the Emu8086 hardware page](http://emu86.com/virtual_devices.html), it does not emulate PC hardware, so there is no speaker at the I/O port you're trying to access. Use something like [DOSBox](http://www.dosbox.com/) if you want that. –  Jun 22 '13 at 16:08
  • Oh I see! And if I try to access an external speaker can I do it with assembly? – Farnaz Jahanbakhsh Jun 22 '13 at 16:16
  • Yes, but it's considerably more difficult. See the documentation for the [AdLib](http://bochs.sourceforge.net/techspec/adlib_sb.txt) and [Sound Blaster 16](http://homepages.cae.wisc.edu/~brodskye/sb16doc/sb16doc.html) hardware. –  Jun 22 '13 at 16:24

2 Answers2

3

To sum this one up: Your code looks fine. The problem was that you were running it on an 8086 emulator that was not for the PC platform (and hence didn't have a speaker attached to that I/O port).

-1

It might be easier to generate a sine wave starting and ending at a zero-crossing, then repeatedly play the file using the built-in OS features.