title: Playing sound with the PC Speaker in x86 Assembly
body: <p>So I'm trying to play a single tone using the pc speaker in x86 assembly, It play a sound, but when I try to turn it off again... the tone begins do what I can only describe as shaking.</p>
  
  <p>Also I'm making this for a 16 bit OS if that means anything.</p>
  
  <p>Here's my sound.asm file</p>
  
  <pre><code>; ------------------------------------------------------------------
  ; os_play_sound -- Play a single tone using the pc speaker
  ; IN: CX = tone, BX = duration
  
  os_play_sound:
      mov     al, 182
      out     0x43, al
      mov     ax, cx
  
      out     0x42, al
      mov     al, ah
      out     0x42, al
      in      al, 0x61
  
      or      al, 00000011b
      out     0x61, al
  
      .pause1:
          mov cx, 65535
  
      .pause2:
          dec cx
          jne .pause2
          dec bx
          jne .pause1
  
          in  al, 0x61
          and al, 11111100b
          out 0x61, al
  
          ret
  </code></pre>
  
  <p>And here's the part in main.asm where I'm calling the sound.asm label from</p>
  
  <pre><code>mov cx, 9121
  mov bx, 25
  call os_play_sound
  </code></pre>