gpt4 book ai didi

linux - 如何在Linux-Assembler中发出提示音?

转载 作者:行者123 更新时间:2023-12-03 09:53:10 24 4
gpt4 key购买 nike

我想在汇编程序内部发出哔哔声。

喜欢

beep()

这可能吗?

我试图将sysCall与BELL-Symbol一起使用。
但是它什么也没做。

我使用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

但是我没有声音。即使我使用sudo运行它。

最佳答案

好的,首先是引用文档:http://www.cs.binghamton.edu/~reckert/220/8254_timer.html
以C实现为例:https://github.com/torvalds/linux/blob/master/drivers/input/misc/pcspkr.c#L49
这是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
该代码无法在用户空间程序中立即运行,因为内核禁止写入IO端口。为了允许该代码还需要 ioperm调用: ioperm(0x42, 32, 1)并使用sudo运行。请参阅此 gist中的完整工作示例。

关于linux - 如何在Linux-Assembler中发出提示音?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41165267/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com