汇编实现乐曲演奏《两只老虎》

1.原理

(1)定义音符和节拍:音符根据表格。

节拍根据12,25,50存入,根据公式计算脉冲和持续时间。

(2)通过不断变换端口61h第一位的值,控制开关电路发出一串脉冲。

2.代码

datas segment

;定义音频表

freq  dw    2 dup(262,294,330,262)

      dw      2 dup(330,349,392)

      dw      2 dup(392,440,392,349,330,262)

      dw      2 dup(294,196,262)

;定义节拍表    一个数字为一拍(25),56加下划线为每一个数字半个节拍(12)  5 ——为两个节拍(50)

beat    dw      10 dup(25),50

        dw      2 dup(25),50,4 dup(12),25,25,4 dup(12),25,25

      dw    25,25,50,25,25,50

datas ends

codes segment

main proc far

   assume cs:codes,ds:datas

   org 100h

   start:

         mov ax,datas

       mov ds,ax

       mov cx,32d

       lea si,freq

       lea di,beat

   play:

        push cx

       call player

       add si,2d

       add di,2d

       pop cx

       loop play

       mov al,48h

       out 61h,al

       

       mov ah,4ch

       int 21h

main endp

;播放一个字符

player  proc  near

      ;将音符的频率转化为控制脉冲宽度的计数值   75页 脉宽 = 2801 * 50 / freq

     mov ax,2801

     mov bx,50

     mul bx  

     mov bx,[si] ;取出在freq中的对应的音符的频率

     div bx;计算脉宽

     mov dx,ax  ;存放脉宽

    

     push dx  ;16位的乘法,结果放在ax  dx中

     ;计算节拍  节拍决定了各音频持续的时间

     mov bx,[di]

     mov ax,120  ;  8太快

     mul bx   ;16位的乘法,结果放在ax bx中

     mov bx,ax

     ;位触发模式发声  

     in al,61h

     mov ah,al

     and al,11111100b

    

     pop dx

   silent:

     xor al,2

     out 61h,al

     mov cx,dx

   wait1:

     loop wait1

     dec bx

     jnz silent

    

     mov al,ah

     out 61h,al

     ret

    

player endp

 

codes ends

     end start

  • 1
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
以下是8086汇编语言的程序,可以让微机演奏一段两只老虎乐曲: ``` ; 两只老虎乐曲演奏程序 ; 使用PC Speaker发声 ORG 100h start: mov ax, 34h ; 设置定时器频率 out 43h, al mov ax, 2864h ; 设置定时器初值 out 40h, al mov al, ah out 40h, al ; 第一只老虎 call delay ; 延迟一段时间 call note_c call delay call note_c call delay call note_g call delay call note_g call delay call note_a call delay call note_a call delay call note_g call delay call note_f call delay call note_f call delay call note_e call delay call note_e call delay call note_d call delay call note_d call delay call note_c call delay ; 第二只老虎 call delay call note_c call delay call note_c call delay call note_g call delay call note_g call delay call note_a call delay call note_a call delay call note_g call delay call note_f call delay call note_f call delay call note_e call delay call note_e call delay call note_d call delay call note_d call delay call note_c call delay jmp start ; 循环演奏 ; 播放一个音符 note: push ax ; 保存寄存器状态 mov bx, ax ; 备份ax ; 计算频率 mov ax, 1193180 cwd ; 扩展ax到dx:ax div bx ; dx:ax / note_number mov bx, ax ; bx = freq ; 发送频率到PC Speaker mov al, bl out 42h, al mov al, bh out 42h, al ; 发出声音 in al, 61h ; 读取端口61h or al, 3 ; 置位speaker引脚和gate2引脚 out 61h, al ; 发送到端口61h mov cx, 1000 ; 延迟一段时间 delay_loop: loop delay_loop in al, 61h ; 关闭speaker引脚和gate2引脚 and al, 0feh out 61h, al pop ax ; 恢复寄存器状态 ret ; 返回 note_c: mov ax, 262 ; C4 call note ret note_d: mov ax, 294 ; D4 call note ret note_e: mov ax, 330 ; E4 call note ret note_f: mov ax, 349 ; F4 call note ret note_g: mov ax, 392 ; G4 call note ret note_a: mov ax, 440 ; A4 call note ret delay: mov cx, 100 ; 延迟一段时间 delay_loop: loop delay_loop ret END start ``` 这个程序使用PC Speaker发声,通过设置定时器频率和初值来控制音符的频率。具体实现过程中,程序会将频率计算出来,然后将频率发送到PC Speaker,再通过控制speaker引脚和gate2引脚来发出声音。在每个音符之间,程序还会延迟一段时间,以控制音符的持续时间。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值