匯編語言之實現發出各種聲音

軟件: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

到此這篇關於匯編語言之實現發出各種聲音的文章就介紹到這瞭,更多相關匯編發出各種聲音內容請搜索WalkonNet以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支持WalkonNet!

推薦閱讀: