简体   繁体   English

如何在 Linux-Assembler 中发出哔哔声?

[英]How to beep in Linux-Assembler?

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.我曾尝试将 sysCall 写入与 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.我使用 Linux-64-Nasm 汇编器,而且,在构建编译器时,我不想使用 C 库。

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.即使我用 sudo 运行它。

OK, first here is a reference doc: http://www.cs.binghamton.edu/~reckert/220/8254_timer.html好的,首先这里是一个参考文档: 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以 C 实现为例: https : //github.com/torvalds/linux/blob/master/drivers/input/misc/pcspkr.c#L49

And here is the beeping code in GNU asm:这是 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.这段代码不会立即在用户空间程序中工作,因为内核禁止写入 IO 端口。 In order to allow that the code also requires ioperm call: ioperm(0x42, 32, 1) and running with sudo.为了允许代码还需要ioperm调用: ioperm(0x42, 32, 1)并使用 sudo 运行。 See the full working example in this gist .请参阅此要点中的完整工作示例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM