键盘控制发声的程序,发出6,7,1,2,3,4,5,6,7,i,十个音调

根据《IBM—PC汇编语言程序设计》的八度音程的钢琴程序,整理,
能发出6,7,1,2,3,4,5,6,7,i,十个音调
DATAS SEGMENT para 'data'
table dw 494  ;0,7.
      dw 524  ;1
      dw 588  ;2
      dw 660  ;3
      dw 698  ;4
      dw 784  ;5
      dw 880  ;6
      dw 988  ;7
      dw 1048 ;8,i
      dw 440  ;9,6.
message db 'Paly the piano with number key(1-9) ,exit with space bar!',13,10,'$'
DATAS ENDS
STACKS SEGMENT  para  'stack'
      db 64 dup('stack...')
STACKS ENDS
CODES SEGMENT
main proc far
     ASSUME CS:CODES,DS:DATAS,SS:STACKS
START:
     MOV   AX,DATAS
     MOV   DS,AX
     mov   ah,9
     mov   dx,offset message
     int   21h
new_note:
     mov   ah,0
     int   16h
     cmp   al,20h     ;space bar is the exit key
     je    exit
     mov   bx, offset table
     cmp   al,'0'
     jb    new_note
     cmp   al,'9'
     ja    new_note
     and   ax,000fh   ;convert the ascii to an index in si ,start with zero
     shl   ax,1
     sub   ax,2
     mov   si,ax
     mov   di,[bx][si+2]
     call  soundf
     jmp   new_note
   
;produces a tone of a specified frequency
soundf proc near
     push ax
     push bx
     push cx
     push dx
     push di
     mov  al,0b6h     ;write timer mode reg
     out  43h,al
     mov  dx,12h      ;timer divisor
     mov  ax,348ch    ;1193100hz/frzq
     div  di          ;value of frzq
     out  42h,al      ;write timer2 count low byte
     mov  al,ah
     out  42h,al      ;write timer2 count low byte
     in   al,61h
     mov  ah,al
     or   al,3
     out  61h,al
     mov  bx,100
wait1:
     mov  cx,633
     call waitf
     dec  bx
     jnz  wait1
     mov  al,ah
     out  61h,al
     pop  di
     pop  dx
     pop  cx
     pop  bx
     pop  ax
     ret
soundf 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
exit:
    MOV   ah,4ch
    INT   21h
main  endp
CODES ENDS
    END START
 

你可能感兴趣的:(汇编语言)