汇编语言系列Ⅳ 实现发出各种声音
Posted boboring
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了汇编语言系列Ⅳ 实现发出各种声音相关的知识,希望对你有一定的参考价值。
软件:emu8086
语言:汇编语言(Assembly)
注意:本文列出了响铃,机枪声音,音乐播放的代码,第一个代码为博主独自一人编写,所有代码都会有瑕疵,谨慎使用。
9.响铃设计
9.1设计要求:
要求掌握响铃的使用方法。设计程序,从键盘接收输入字符,如是数字N,则响铃N次,如输入为非数字,则不响铃。
9.2设计思路:
判断输入为0~9之间,将N值给BP,设置响铃延时次数CX输出N次响铃。
9.3程序清单:
DATA SEGMENT pkey DB 0AH,0DH,‘please input the N:‘,‘$‘ ERR DB 0AH,0DH,‘eero!‘,0AH,0DH,‘please input the N again:‘,‘$‘ DATA ENDS CODE SEGMENT ASSUME CS:CODE,DS:DATA STATE: MOV AX,DATA MOV DS,AX MOV ES,AX LEA DX,pkey MOV AH,09H INT 21H ;DOS功能调用,输出字符串 INPUT: MOV AH,01H INT 21H CMP AL,‘9‘ JA eero CMP AL,‘0‘ JB eero SUB AL,30H ;输入为0~9,则AL为响铃次数 SUB AH,AH MOV BP,AX ;BP为响铃次数N xiang: MOV CX,4 ;CX为响铃延时时间 MOV AH,02H MOV DL,07H INT 21H ;DOS功能调用,响铃 LP: LOOP LP ;响铃延时 DEC BP JNZ xiang ;响铃N次 JMP STATE eero: LEA DX,ERR MOV AH,9 INT 21H ;输出错误字符串 JMP INPUT CODE ENDS END STATE
9.4程序运行结果及分析:
设置CX次数为延时次数,BP次数为响铃次数,调用响铃功能通过延时实现响铃,起初会把延时次数和响铃次数混淆导致错误。
图9 响铃设计结果
10.需要外接扬声器的发声程序
10.1机枪声音
PROGRAM SEGMENT MAIN PROC FAR ASSUME CS:PROGRAM ORG 100H STRAT:MOV CX,20D NEW_SHOT:PUSH CX CALL SHOOT MOV CX,4000H SILENT:LOOP SILENT POP CX LOOP NEW_SHOT INT 20H MAIN ENDP SHOOT PROC NEAR MOV DX,140H MOV BX,20H IN AL,61H AND AL,11111100B SOUND:XOR AL,2 OUT 61H,AL ADD DX,9248H MOV CL,3 ROR DX,CL MOV CX,DX AND CX,1FFH OR CX,10 WAIT: LOOP WAIT DEC BX JNZ SOUND AND AL,11111100B OUT 61H,AL RET SHOOT ENDP PROGRAM ENDS END STRAT
10.2音乐播放
;定义数据段 data segment infor1 db 0Dh, 0AH, "welocom you to come here listeng! $" mus_freg dw 330,294,262,294,3 dup (330) ;频率表 dw 3 dup (294),330,392,392 dw 330,294,262,294,4 dup (330) dw 294,294,330,294,262,-1 mus_time dw 6 dup (25),50 ;节拍表 dw 2 dup (25,25,50) dw 12 dup (25),100 data ends ;栈段定义 stack segment stack db 200 dup(?) stack ends ;--------字符串输出宏---------- SHOWBM MACRO b LEA DX,b MOV AH,9 INT 21H ENDM ;----------音乐地址宏----------- ADDRESS MACRO A,B LEA SI,A LEA BP,DS:B ENDM ;代码段定义 code segment assume ds:data, ss:stack, cs:code start: mov ax, data mov ds, ax mov ax, stack mov ss, ax mov sp, 200 address mus_freg, mus_time call music exit: mov ah, 4cH int 21h ;------------发声------------- gensound proc near push ax push bx push cx push dx push di mov al, 0b6H out 43h, al mov dx, 12 mov ax, 348ch div di out 42h, al mov al, ah out 42h, al in al, 61h mov ah, al or al, 3 out 61h, al wait1: mov cx, 3314 call waitf delay1: dec bx jnz wait1 mov al, ah out 61h, al pop di pop dx pop cx pop bx pop ax ret gensound endp ;-------------------------- waitf proc near push ax waitf1: in al,61h and al,10h cmp al,ah je waitf1 mov ah,al loop waitf1 pop ax ret waitf endp ;--------------发声调用函数---------------- music proc near xor ax, ax freg: mov di, [si] cmp di, 0FFFFH je end_mus mov bx, ds:[bp] call gensound add si, 2 add bp, 2 jmp freg end_mus: ret music endp code ends end start
以上是关于汇编语言系列Ⅳ 实现发出各种声音的主要内容,如果未能解决你的问题,请参考以下文章