汇编源代码之MAKE SOUNDS(发声)
2009-01-28 09:35:40 来源:WEB开发网 闂傚倸鍊搁崐鎼佸磹閹间礁纾归柟闂寸绾惧綊鏌熼梻瀵割槮缁炬儳缍婇弻鐔兼⒒鐎靛壊妲紒鐐劤缂嶅﹪寮婚悢鍏尖拻閻庨潧澹婂Σ顔剧磼閻愵剙鍔ょ紓宥咃躬瀵鎮㈤崗灏栨嫽闁诲酣娼ф竟濠偽i鍓х<闁绘劦鍓欓崝銈囩磽瀹ュ拑韬€殿喖顭烽幃銏ゅ礂鐏忔牗瀚介梺璇查叄濞佳勭珶婵犲伣锝夘敊閸撗咃紲闂佺粯鍔﹂崜娆撳礉閵堝洨纾界€广儱鎷戦煬顒傗偓娈垮枛椤兘骞冮姀銈呯閻忓繑鐗楃€氫粙姊虹拠鏌ュ弰婵炰匠鍕彾濠电姴浼i敐澶樻晩闁告挆鍜冪床闂備浇顕栭崹搴ㄥ礃閿濆棗鐦遍梻鍌欒兌椤㈠﹤鈻嶉弴銏犵闁搞儺鍓欓悘鎶芥煛閸愩劎澧曠紒鈧崘鈹夸簻闊洤娴烽ˇ锕€霉濠婂牏鐣洪柡灞诲妼閳规垿宕卞▎蹇撴瘓缂傚倷闄嶉崝搴e垝椤栫偛桅闁告洦鍨扮粻鎶芥倵閿濆簼绨藉ù鐘荤畺濮婃椽妫冨☉娆愭倷闁诲孩鐭崡鎶芥偘椤曗偓瀹曞爼顢楁径瀣珫婵犳鍣徊鍓р偓绗涘洤绠查柛銉墮閽冪喖鏌i弬鎸庢喐闁荤喎缍婇弻娑⑩€﹂幋婵囩亪濡炪値鍓欓悧鍡涒€旈崘顔嘉ч幖绮光偓鑼嚬缂傚倷绶¢崰妤呭箰閹间焦鍋╅柣鎴f绾偓闂佺粯鍔曠粔闈浳涢崘顔兼槬闁逞屽墯閵囧嫰骞掗幋婵愪紑閻庤鎸风粈渚€鍩為幋锔藉亹闁圭粯甯╂导鈧紓浣瑰劤瑜扮偟鍒掑▎鎾宠摕婵炴垶鐭▽顏堟煙鐟欏嫬濮囨い銉︾箞濮婃椽鏌呴悙鑼跺濠⒀傚嵆閺岀喖鎼归锝呯3闂佹寧绻勯崑娑㈠煘閹寸姭鍋撻敐搴樺亾椤撴稒娅婇柡灞界У濞碱亪骞忕仦钘夊腐闂備焦鐪归崐鏇㈠箠閹邦喗顫曢柟鎯х摠婵挳鏌涢幘鏉戠祷闁告挸宕—鍐Χ閸℃浠搁梺鑽ゅ暱閺呮盯鎮鹃悜钘壩ㄧ憸澶愬磻閹剧粯鏅查幖绮瑰墲閻忓秹姊虹紒妯诲鞍婵炲弶锕㈡俊鐢稿礋椤栨氨鐤€闂傚倸鐗婄粙鎰姳閼测晝纾藉ù锝堟閻撴劖鎱ㄥΟ绋垮婵″弶鍔欓獮妯兼嫚閼碱剦妲伴梻浣稿暱閹碱偊宕愭繝姣稿洭寮舵惔鎾存杸濡炪倖姊婚妴瀣啅閵夛负浜滄い鎾跺仜濡插鏌i敐鍥у幋妤犵偞甯¢獮瀣籍閳ь剟鎮楁繝姘拺閻熸瑥瀚崕妤呮煕濡 鍋撻悢鎻掑緧婵犵數濮烽弫鍛婃叏閻戣棄鏋侀柛娑橈攻閸欏繑銇勯幘鍗炵仼缁炬儳顭烽弻鐔煎礈瑜忕敮娑㈡煃闁垮鐏﹂柕鍥у楠炴帡宕卞鎯ь棜缂傚倸鍊风粈渚€藝闁秴鏋佸┑鐘虫皑瀹撲線鏌涢埄鍐姇闁稿﹦鍏橀弻娑樷攽閸℃浼€濡炪倖姊归崝鏇㈠煘閹达附鍊婚柛銉㈡櫇鏍¢梻浣告啞閹稿鎮烽敂鐣屸攳濠电姴娲﹂崵鍐煃閸濆嫬鏆熼柨娑欑矒濮婇缚銇愰幒鎴滃枈闂佸憡鐟ユ鎼佸煝閹炬枼鍫柛顐ゅ枔閸樻悂鏌h箛鏇炰户缁绢厼鐖煎畷鎴﹀箻鐠囪尙鐤€婵炶揪绲介幉锟犲磹椤栫偞鈷戠痪顓炴噹娴滃綊鎮跺☉鏍у姦闁糕斁鍋撳銈嗗笒閸燁偊鎯冨ú顏呯厸濞达絽婀辨晶顏堟煃鐟欏嫬鐏撮柟顔界懇瀵爼骞嬮悩杈敇闂傚倷绀佸﹢杈ㄧ仚闂佺濮ょ划搴ㄥ礆閹烘绫嶉柛顐ゅ枎娴犺櫣绱撴担鍓插創妞ゆ洘濞婇弫鍐磼濞戞艾骞堥梻浣告惈濞层垽宕濆畝鍕€堕柣妯肩帛閻撴洟鏌熼懜顒€濡煎ù婊勫劤閳规垿鏁嶉崟顐℃澀闂佺ǹ锕ラ悧鐘茬暦濠靛鏅濋柍褜鍓熼垾锕傚锤濡も偓閻掑灚銇勯幒宥堝厡缂佺姴澧介埀顒€鍘滈崑鎾斥攽閻樿京绐旈柛瀣殔閳规垿顢欑涵鐑界反濠电偛鎷戠徊鍨i幇鏉跨闁瑰啿纾崰鎾诲箯閻樼粯鍤戦柤绋跨仛濮f劙姊婚崒姘偓鐑芥嚄閼哥數浠氭繝鐢靛仜椤曨參宕楀Ο渚殨妞ゆ劑鍊栫€氭氨鈧懓澹婇崰鏍р枔閵婏妇绡€闁汇垽娼ф牎缂佺偓婢樼粔鐟邦嚕閺屻儱绠甸柟鐑樼箘閸炵敻鏌i悩鐑橆仩閻忓繈鍔岄蹇涘Ψ瑜夐崑鎾舵喆閸曨剙纰嶅┑鈽嗗亝缁诲倿锝炶箛娑欐優闁革富鍘鹃敍婊冣攽閳藉棗鐏犻柟纰卞亰閿濈偛顓奸崶鈺冿紳婵炶揪缍侀ˉ鎾诲礉瀹ュ鐓欑紒瀣仢閺嗛亶鏌i敐鍥у幋妤犵偛顑夐弫鍐焵椤掑倻涓嶅┑鐘崇閸嬶綁鏌涢妷鎴濆暟妤犲洭鎮楃憴鍕碍缂佸鎸抽垾鏃堝礃椤斿槈褔鏌涢埄鍏狀亪妫勫鍥╃=濞达絽澹婇崕鎰版煕閵娿儱顣崇紒顔碱儏椤撳吋寰勭€n亖鍋撻柨瀣ㄤ簻闁瑰搫绉堕ˇ锔锯偓娈垮枛閻忔繈鍩為幋锕€鐓¢柛鈩冾殘娴狀垶姊洪崨濠庣劶闁告洦鍙庡ú鍛婁繆閵堝繒鍒伴柛鐕佸灦瀹曟劙宕归锝呭伎濠碘槅鍨抽崢褎绂嶆ィ鍐╁€垫慨妯煎亾鐎氾拷

INTRODUCTION
This code example provides a set of keyboard routines to control sound output while waiting for a user to enter a keyboard character. The advantage to this method is that a main routine can call these sound routines to play a sound sequence, and the sound routines will return control back to the main routine whenever the user enters keyboard data so that the main routine can continue computing while the sound plays in the background. The code example has two different code entry points for getting keyboard data. One code entry point is a standard get_keyinput call which will wait for a key and update the sound data until a key code is found. The other code entry point is the get_keyinput_to call, which will wait a set amount of time for a key code and if none is found, return with a no key code found condition. The calling routine puts a timeout counter value in register AX on entry. The counter value is based on the system clock which ticks at 18.2 times per second. The entry point start_table_sound is used to begin a background sound sequence. On entry, the register BX indexes a table of sound data. The table has a format of four byte entries and is terminated by a data word of zero. The four bytes are used as two words: the first is a duration count and the second is a tone value. There are two code entry points for turning the background sound off and on. There is also a utility to flush out the keyboard buffer that can be executed with a call to flush_keyboard.
;Set of keyboard routines with sound outputs
.MODEL small
.STACK 500
.DATA
;define table for sound output
;sample_sounds dw 8,45000 ;long low sound
; dw 2,2000 ;short high sound
; dw 0 ;end of sample sound table
sound_table dw 0
sound_time_m dw 0
sound_time_l dw 0
sound_flag db 0
sound_on_flag db 0,0
key_time_out_m dw 0
key_time_out_l dw 0
.CODE
;************ ^^^^^^^^^^ *************
;### code entry point #####
get_keyinput proc near
;this routine checks for keyboard data in BIOS buffer
; and returns with data if there
;else it updates sound output data and loops to check for
; keyboard data again until keyboard data found
;on exit AX has keyboard data
public get_keyinput
push bx
push cx
push dx
get_keyinput_loop:
mov ah,1 ;set AH for scan
int 16H ;BIOS Call
;branch if no keyboard data
jz sound_update
mov ah,0 ;set AH for get key
int 16H ;BIOS Call
pop dx
pop cx
pop bx
ret
;******* -------- *******
sound_update:
cmp sound_flag,0 ;check for sound on????
jz get_keyinput_loop ;branch out if sound off
mov cx,sound_time_m ;else check for sound update
mov ax,sound_time_l
call test_current_time ;is it time for update ??
jc get_keyinput_loop ;branch if not time
mov bx,sound_table
mov ax,[bx] ;get next sound update value
or ax,ax ;?? end of sound ??
jz turn_sound_off ;branch if end sound
call get_time_plus_ax ;reset sound duration
mov sound_time_m,cx
mov sound_time_l,ax
inc bx
inc bx
mov ax,[bx]
inc bx
inc bx
mov sound_table,bx
call sound_out_ax ;go set sound frequency
jmp get_keyinput_loop ;branch to keyboard loop
turn_sound_off:
call sound_off
mov sound_flag,0
jmp get_keyinput_loop ;branch to keyboard loop
get_keyinput endp
;^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
;************ ########## *************
;### code entry point #####
get_keyinput_to proc near
;get keyboard data with timeout if no data available
;on entry AX has time duration in 18 ticks per second
;on exit if carry clear then AX has keyboard data
public get_keyinput_to
push bx
push cx
push dx
call get_time_plus_ax ;add duration to current time
mov key_time_out_m,cx ;set timeout value
mov key_time_out_l,ax
get_keyinput_to_loop:
mov ah,1 ;ready to scan keyboard data
int 16H ;BIOS Call
jz sound_update_to ;branch if no keyboard data
mov ah,0 ;ready to get key data
int 16H ;BIOS Call
pop dx
pop cx
pop bx
clc ;set keyboard data flag
ret
get_keyinput_to_1:
mov cx,key_time_out_m ;check for timeout
mov ax,key_time_out_l
call test_current_time
jc get_keyinput_to_loop ;branch if no timeout
xor ax,ax ;else timeout return condition
pop dx
pop cx
pop bx
stc ;set no keyboard data flag
ret
; ******** %%%%%%% ********
sound_update_to:
cmp sound_flag,0 ;check for sound on????
jz get_keyinput_to_1 ;branch if sound off
mov cx,sound_time_m ;else check for sound update
mov ax,sound_time_l
call test_current_time
jc get_keyinput_to_1 ;branch if not ready for update
mov bx,sound_table
mov ax,[bx]
or ax,ax ;test for end of table
jz turn_sound_off_to ;branch if end of table data
call get_time_plus_ax
mov sound_time_m,cx
mov sound_time_l,ax
inc bx
inc bx
mov ax,[bx]
inc bx
inc bx
mov sound_table,bx
call sound_out_ax
jmp get_keyinput_to_1
turn_sound_off_to:
call sound_off
mov sound_flag,0
jmp get_keyinput_to_1
get_keyinput_to endp
;^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
;************ @@@@@@@@@@ ************
;### code entry point #####
start_table_sound proc near
;subroutine to start background sound output
;on entry BX indexes sound data table
public start_table_sound
push ax
push bx
mov ax,[bx]
call get_time_plus_ax
mov sound_time_m,cx
mov sound_time_l,ax
inc bx
inc bx
mov ax,[bx]
inc bx
inc bx
mov sound_table,bx
call sound_out_ax
mov sound_flag,0FFH
pop bx
pop ax
ret
start_table_sound endp
;************ ========== *************
;### code entry point #####
flush_keyboard proc near
;utility to flush contents of keyboard buffer
public flush_keyboard
mov ah,1
int 16H ;BIOS Call ;scan for keyboard data
jz flush_keyboard_x ;branch if no keyboard data
mov ah,0 ;else get keyboard data
int 16H ;BIOS Call
jmp flush_keyboard
flush_keyboard_x:
ret
flush_keyboard endp
;************* ----------- **************
sound_out_ax proc near
;set sound out frequency to data value in AX
push ax
push ax
cmp sound_on_flag,0
jne sound_out_1
in al,61H ;input port 61h
or al,3
out 61H,al ;output port 61h
sound_out_1:
mov al,0B6H
out 43H,al ;output port 43h
pop ax
out 42H,al ;output port 42h
xchg al,ah
out 42H,al ;output port 42h
mov sound_on_flag,0FFH
pop ax
ret
sound_out_ax endp
;*********** $$$$$$$$$$ ************
;###### code entry point #######
sound_off proc near
;turn sound port off
public sound_off
push ax
cmp sound_on_flag,0
je sound_off_exit
in al,61H ;input port 61h
and al,0FCH
out 61H,al ;output port 61h
mov sound_on_flag,0
sound_off_exit:
pop ax
ret
sound_off endp
;************** %%%%%%%%%% ***************
;with all CX:AX time values, CX is most significant
; and AX is least significant
get_current_time proc near
;on exit CX:AX has 32 bit day clock value
; in 18.2 ticks per second
push dx
xor ax,ax ;set AH to zero
int 1AH ;BIOS Call get time
mov ax,dx
pop dx
ret
get_current_time endp
;****************************
get_time_plus_ax proc near
;on entry AX has 16 bit value to add to current clock time
;on exit CX:AX has new 32 bit clock value
push dx
push ax
xor ax,ax
int 1AH ;BIOS Call
pop ax
add ax,dx
adc cx,0
pop dx
ret
get_time_plus_ax endp
;************ ######## ************
test_current_time proc near
;on entry CX:AX has time value
; to be subtracted from the current time
;on exit if carry set then current time
; is less than CX:AX time
push dx
push cx
push ax
xor ax,ax
int 1AH ;BIOS Call
cmp dx,18
jb test_current_time_2
test_current_time_1:
pop ax
sub dx,ax
pop dx
sbb cx,dx
mov cx,dx
pop dx
ret
test_current_time_2:
or cx,cx
jnz test_current_time_1
pop ax ;this is fix code for midnight factor
pop dx
pop dx
clc ;clear carry condition
ret
test_current_time endp
;*****************************************
end
更多精彩
赞助商链接