2

I wanna have a beep inside of assembler.

like

beep()

Is this possible?

I have tried to use the sysCall write with the BELL-Symbol. But it doesn't do anything.

I use the Linux-64-Nasm Assembler, And, as I am building a compiler, I don't want to use the C-libraries.

section .data
cmp_BLANK: db 0x0a
cmp_interr: db "error, You have typed in a non-Integer Character!", 0x0a
cmp_interrlen: equ $-cmp_interr
cmp_buffer: times 9 db 0x00
cmp_beep: db 0x07

section .bss

section .text
global _start
_start:
call func_main
mov eax, 1
mov ebx, 0
int 80h
func_main:
mov eax, 4
mov ebx, 1
mov ecx, cmp_beep
mov edx, 1
int 80h
ret        

But I don't get a Sound. Even when I run it with sudo.

Jester
  • 56,577
  • 4
  • 81
  • 125
Linuxer4Fun
  • 85
  • 1
  • 6
  • FYI, that's 32 bit code. As to whether you get a beep depends on circumstances such as terminal used, bell being enabled in X11 (if you are running under it), your sound modules, etc. – Jester Dec 15 '16 at 13:42
  • 1
    As beeping require hardware access, your question is basically a duplicate of [this one](https://stackoverflow.com/questions/10072909/beep-on-linux-in-c). – Margaret Bloom Dec 15 '16 at 13:54
  • no It's not, as I didn't get a good answer on it. And in Assembler I can't use the \a sequence – Linuxer4Fun Dec 15 '16 at 17:15
  • @Linuxer4Fun Not an exact duplicate, of course. [Here](http://pkgs.fedoraproject.org/repo/pkgs/beep/beep-1.3.tar.gz/49c340ceb95dbda3f97b2daafac7892a/beep-1.3.tar.gz) you can find the source of the `beep` program. The program is very simple, you can use it to create your assembly program. As I said, you have to use to OS to do a beep as the 8254 is not accessible from user space. Alternative: do LKM. – Margaret Bloom Dec 15 '16 at 18:30
  • 1
    If your terminal is configured to make an audible beep when a program prints a `\a`, then that's all you need to do. (See http://unix.stackexchange.com/questions/1974/how-do-i-make-my-pc-speaker-beep for more about making that happen with the PC Speaker. In a modern terminal emulator like konsole or gnome-terminal, check the config options for playing a sound over your normal speakers). Note that in NASM, you can use C-style backslash-escapes inside backquotes, but not in single or double quotes. – Peter Cordes Dec 15 '16 at 19:10
  • Your program seems correct. Now, is your terminal able to beep? Do you hear a beep if you for example try to backspace at the beginning of a shell input line? – Armali Oct 18 '18 at 07:27

1 Answers1

0

OK, first here is a reference doc: http://www.cs.binghamton.edu/~reckert/220/8254_timer.html

Look at the C implementation as example: https://github.com/torvalds/linux/blob/master/drivers/input/misc/pcspkr.c#L49

And here is the beeping code in GNU asm:

    # send "set tune" command
    movb    $0xB6, %al
    outb    %al, $0x43

    # nanosleep to let the IO complete                                                                                                                                  
    movl    $0x1000, %eax
1:  subl    $1, %eax
    cmpl    $0, %eax
    jne     1b

    # set 220Hz, 0x152F == 1193180 / 220
    movb    $0x2F, %al
    outb    %al, $0x42

    # nanosleep
    movl    $0x1000, %eax
1:  subl    $1, %eax
    cmpl    $0, %eax
    jne     1b

    movb    $0x15, %al
    outb    %al, $0x42

    # nanosleep
    movl    $0x1000, %eax
1:  subl    $1, %eax
    cmpl    $0, %eax
    jne     1b

    # turn on the speaker
    inb     $0x61, %al
    movb    %al, %ah
    orb     $0x3, %al
    outb    %al, $0x61

    # sleep about 1 sec
    movl    $0x30000000, %eax
1:  subl    $1, %eax
    cmpl    $0, %eax
    jne     1b

    # turn off the speaker
    movb    %ah, %al
    andb    $0xfc, %al
    outb    %al, $0x61

This code won't work immediately in userspace program, because kernel forbids writing into IO ports. In order to allow that the code also requires ioperm call: ioperm(0x42, 32, 1) and running with sudo. See the full working example in this gist.

void
  • 2,759
  • 12
  • 28