231a-ad playMary

From CSclasswiki
Jump to: navigation, search
;;; ; ; playMary.asm
;;; ; ; Amy Gray 
;;; ; ; December 2008
;;; ; ; 
;;; ; ;
;;; ; ; playMary.asm
;;; ; ;
;;; ; ; A test version of playNotes.asm
;;; ; ; This program contains an array with the notes
;;; ; ; for one verse of 'Mary Had a Little Lamb.'
;;; ; ;
;;; ; ; to assemble and run:
;;; ; ;
;;; ; ;     nasm -f elf -F  stabs playMary.asm
;;; ; ;     gcc -o playMary driver.c playMary.o
;;; ; ;     ./playMary
;;; ; ; =========================================================

;; %include "asm_io.inc"

;;; External Labels =============================================
extern serialport_writedword 	;int function
extern serialport_write 	;int function
extern serialport_read_until	;int function
extern displayBuffer		;int function

extern buf
extern dword
	

                EXIT    equ             1
                WRITE   equ             4
                STDOUT  equ             1

        
;;; ;  ===========================================================
;;; ;  data areas
;;; ;  ===========================================================
;;; ;  note: 78 is dec for N and is essentially arbitrary.  When
;;; ;  playing the array, the program will check to see if the
;;; ;  "note" equals 78, and if so, knows there are no more notes
;;; ;  contained in the array and will end the loop.  I chose N
;;; ;  for "null" or "no note."
;;; ;  ===========================================================

;; TONES  ========================================== 
;; Start by defining the relationship between 
;;      note, period, &  frequency. 
;; #define  c     3830    // 261 Hz 
;; #define  d     3400    // 294 Hz 
;; #define  e     3038    // 329 Hz 
;; #define  f     2864    // 349 Hz 
;; #define  g     2550    // 392 Hz 
;; #define  a     2272    // 440 Hz 
;; #define  b     2028    // 493 Hz 
;; #define  C     1912    // 523 Hz 
;; #define  p     0	  // silence
;; ==================================================
;; MARY HAD A LITTLE LAMB ==================================
;; B,A,G,A,B,B,B,A,A,A,B,D,D,P,B,A,G,A,B,B,B,B,B,A,A,B,A,G,P
;; =========================================================
                section .data
array		dd	2028,2272,2550,2272,2028,2028,2028,2272,2272,2272,2028,3400,3400
		dd	0,2028,2272,2550,2272,2028,2028,2028,2028,2028,2272,2272,2028,2272
		dd	2550,0,78,78,78,78
arrayLen	equ	$-array
message		db	"Mary Had a Little Lamb"
messageLen	equ	$-message
nextIndex	dd	0

        
;;; ;  =============================================================
;;; ;  code area
;;; ;  =============================================================

                section .text
                global asm_main

asm_main:
	mov	eax, WRITE
	mov	ebx, STDOUT
	mov	ecx, message
	mov	edx, messageLen
	int	0x80
	
	;; skip keyboard input
	;; skip writing to array
	;; no catch needed for running out of room
	;; this version does not check pots

	mov	edx, 0
checkArray:
	mov	eax, [array + edx]
	add	edx, 4
	cmp	eax, 78
	jne	checkArray

	sub	edx, 4
	mov	[nextIndex], edx

	mov	ecx, [nextIndex] ; #of notes (*4)
	mov	edx, 0		 ; which note to play
;; run through the array
runArray:
 	push 	ecx
 	mov	eax, [array + edx]
	;; pass eax to C program
delay:
 	mov	ecx, 500000000
.for:
 	;; do nothing for 1/2 a second
 	loop 	.for
	
 	add	edx, 4
 	pop	ecx
	sub	ecx, 3
 	loop	runArray
 	jmp	exit
	
exit:
	ret