
TITLE PIANO -- Use timer 2 to run speaker
;Keyboard number keys play notes of the scale
;assemble with:masm piano
;link with:link piano+gensound
;
      extrn gensound:far
stack segment para stack 'stack'
	db 64 dup('stack...')
stack ends
;--------------------------------------------------
dseg segment para 'data'
table	dw	262	; C
	dw	294	; D
	dw	330	; E
	dw	349	; F
	dw	392	; G
	dw	440	; A
	dw	494	; B
	dw	523	; C
dseg ends
;--------------------------------------------------
cseg segment para 'code'
main proc far
     assume cs:cseg,ds:dseg,ss:stack
	push	ds	      ;set up stack for return
	sub	ax,ax
	push	ax
	mov	ax,dseg	      ;set DS to current
	mov	ds,ax	      ;  data segment
new_note:
	mov	ah,0	      ;read the keyboard
	int	16h
	cmp	al,0dh	      ;is it return?
	je	exit	      ;is so, exit
	mov	bx,offset table  ;freq table address
	cmp	al,'1'	         ;is'1'--'8'?
	jb	new_note      ;no, read the next key
	cmp	al,'8'
	ja	new_note
	and	al,0fh	      ;mask off high 4 bits
	shl	ax,1	      ;  * by 2
	sub	ax,2	      ;convert the ASCII to
	mov	si,ax	      ;  an index in SI

	mov	di,[bx][si]   ;look_up a frequency
	mov	bx,10	      ;for 0.01 second
	call	gensound      ;play the note
	jmp	new_note      ;go get another note
exit:	ret		      ;return to DOS
main	endp
cseg	ends
;--------------------------------------------------
	end	main


 