TITLE MUSEX -- tone of do _ re _ mi
;--------------------------------------------------
dseg segment para 'data'
	 dw 0
mus_f    dw 262,294,330,349,392,440,494,523,0
mus_t    dw 7 dup(50),100
flag	 dw -1
dseg ends
;--------------------------------------------------
cseg    segment  para 'code'
assume  cs:cseg, ds:dseg
tone    proc    far
	push    ds            ;put return addr.
	sub     ax,ax	      ;  on stack
	push    ax
	mov     ax,dseg       ;initialize DS
	mov     ds,ax

	lea     si,mus_f   ;freq table offset in SI
next:	lea	bp,mus_t   ;time table offset in BP

freq:	mov	di,[si]	      ;read next frequency
	cmp	di,0	      ;end of tone ?
	je	end_f	      ;yes,one times is end
	mov	bx,ds:[bp]    ;no,fetch the duration

	mov	al,0b6h	      ;put magic number
	out	43h,al        ;    into timer2
	mov	dx,12h        
	mov	ax,533h*896
	div	di	      ;( DX,AX ) / ( DI )
	out	42h,al        ;LSB into timer2
	mov	al,ah
	out	42h,al	      ;MSB into timer2
	in	al,61h
	mov	ah,al
	or	al,3	      ;turn speaker on
	out	61h,al
long:	mov	cx,2801       ;length of tone       
delay:	loop	delay
	dec	bx
	jnz	long
	mov	al,ah         ;recover the portb
	out	61h,al

	test	flag,1        ;raise or lower ?
	jz	lower
	add	si,2	      ;inc the freq pointer
	jmp	cont
lower:	sub	si,2          ;dec the freq pointer 
cont:	add	bp,2          ;update mus_t pointer
	jmp	freq	      ;go process next note
end_f:	inc	flag
	jnz	exit          ;two times end
	sub	si,2
	jmp	next          ;second times
exit:	mov	al,48h        ;reset portb
	out	61h,al
	ret		      ;return to DOS
tone    endp
cseg    ends
;----------------------------------------------------
	end     tone
 