-1

When computer starts to boot, It makes a beep sound from the BIOS Speaker.

How do i can do this in Assembly or C++ ? Clearly I want to make Beep Sound by BIOS Speaker.
Remember i mean BIOS Speakers

Does it have any interrupt for that ? I searched about that but nothing found.. I used some interrupt But the didn't do this. the following code :

int main(){
   cout<<"\a";
}

Produced the sound from the Speaker, Not Bios

How can i do this ? with any interrupt ?

Alireza378n A
  • 135
  • 1
  • 2
  • 7

3 Answers3

2

Try to add this code, too.

.pause1:
    mov     cx, 65535
.pause2:
    dec     cx
    jne     .pause2
    dec     bx
    jne     .pause1
    in      al, 61h         ; Turn off note (get value from
                            ;  port 61h).
    and     al, 11111100b   ; Reset bits 1 and 0.
    out     61h, al         ; Send new value.

So, the result is:

void beep(){

    __asm{

      MOV al, 182         ; Prepare the speaker for the
      out     43h, al     ;  note.
      mov     ax, 2280    ; Frequency number (in decimal)
                          ;  for C.
      out     42h, al     ; Output low byte.
      mov     al, ah      ; Output high byte.
      out     42h, al 
      in      al, 61h     ; Turn on note (get value from
                          ;  port 61h).
      or      al, 00000011b   ; Set bits 1 and 0.
      out     61h, al         ; Send new value.
      mov     bx, 4       ; Pause for duration of note.


    .pause1:
       mov     cx, 65535
    .pause2:
       dec     cx
       jne     .pause2
       dec     bx
       jne     .pause1
       in      al, 61h         ; Turn off note (get value from
                               ;  port 61h).
       and     al, 11111100b   ; Reset bits 1 and 0.
       out     61h, al         ; Send new value.

   };
}
  • `in` and `out` are privileged instructions; you can't use them in user-space under most OSes. (Under Linux, there's `ioperm` and `iopl` system calls to let a process request IO privilege, but that could interfere with a kernel driver using the PC speaker). This code might work in a DOS program with an ancient 16-bit C++ compiler, though. – Peter Cordes Aug 07 '21 at 05:03
1

The only way you can implement this in any modern Windows OS is, I guess, writing Kernel Mode driver. The reason is that in or out instructions are not available in user mode, and there is no API for beeper available.

However, if you're just willing to dig into low-level programming, consider writing own bootloader or even own BIOS (using virtual machine).

Max Malysh
  • 29,384
  • 19
  • 111
  • 115
-1

Try to include this procedure in your C++ program.

void beep(){

    __asm{

      MOV al, 182         ; Prepare the speaker for the
      out     43h, al     ;  note.
      mov     ax, 2280    ; Frequency number (in decimal)
                          ;  for C.
      out     42h, al     ; Output low byte.
      mov     al, ah      ; Output high byte.
      out     42h, al 
      in      al, 61h     ; Turn on note (get value from
                          ;  port 61h).
      or      al, 00000011b   ; Set bits 1 and 0.
      out     61h, al         ; Send new value.
      mov     bx, 4       ; Pause for duration of note.
   };
}
  • 1
    `in` and `out` are privileged instructions. They won't work in an user mode application. – Daniel Kamil Kozar Apr 18 '15 at 08:27
  • Probably, your beep cames out from PC speakers or earphones; Not from PC BIOS Speaker. So are you sure that your device don't produce any sound? – Nicolò Favaretto Apr 19 '15 at 11:36
  • Don't post multiple versions of the same answer; in future edit a single answer. Since you already have one answer with your full code, you should delete this one. – Peter Cordes Aug 07 '21 at 05:01