Assembly source code make SOUNDS (audible)

Source: Internet
Author: User
Tags exit current time flush

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 the "This" is "a main routine can call" sound routines to play a sound sequence, and the sound Routines'll return control the main routine whenever the user enters keyboard the main routine can Continue computing While the sound is plays in the background. The code example has two different code entry points for getting of data. One code entry point are a standard get_keyinput call which'll wait for a key and update the sound data until a key code is found. The other code entry-Get_keyinput_to call, which'll wait a set amount of time for a key code and if none 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 in 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 is a duration count and the second is a tone value. There are two code entry points for turning the background. There is also a utility to flush out the keyboard buffer so can be executed with a call to Flush_keyboard.

; Set of keyboard routines with sound outputs
. MODEL Small
. STACK 500
. DATA
;d efine 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 to 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-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 at 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
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 the Cx:ax time values, the CX is most significant
; And AX is least significant
Get_current_time proc Near
; On exit Cx:ax has the 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 bit value to add to current clock time
; On exit Cx:ax has new 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 is subtracted from the
; 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-is fix code for Midnight factor
Pop DX
Pop DX
CLC; Clear Carry condition
Ret
Test_current_time ENDP
;*****************************************
End

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.