CS273 HW5 Solution

From the Book

For these problems, you only need to provide us with the subroutine (procedure) code itself (the assembly code, not the .s19 file). We'll hook up a main program that calls your procedure. Also, note that these subroutines won't require the full stack manipulation stuff we've talked about in class.

Not Quite From the Book (30 points)

This problem is really 5-40, modified to push parameters on the stack. Write a procedure that compares the 8-bit two's complement numbers in two tables. It should determine whether all the numbers in the first table are equal to or greater than the corresponding numbers in the second table. The procedure should return 00 in the A accumulator if the condition is true, or ff if it is false. The references to the tables will be passed on the stack; the first item pushed will be the number of elements in the two tables (this is a one-byte value); the second will be the address in memory of the first table (this is a two-byte value); the third will be the address in memory of the second table (this will also be a two-byte value).


	org	$f800
tab1	fcb	1,2,3,4,5
tab2	fcb	2,1,2,3,4
count	fcb	5
	
main	lds	#$00ff 	*initialize stack
	ldaa	count	*pass the number of elements in each table through stack
	psha 		
	ldx	#tab1 	*pass the start address of the first table through stack
	pshx 		
	ldx	#tab2 	*pass the start address of the second table through stack
	pshx 		
	jsr	ALLGE	*call the subroutine
	ins		*restore the stack
	ins
	ins
	ins
	ins

eloop	bra	eloop


*============== subroutine ALLGE ===============
ALLGE	pshx		*push X
	pshy		*push Y
	pshb		*push B
	tsx		*get the address of the top element of the stack into X
	ldaa 11,x	*get the number of elements in each table into A (as a counter)
	ldy 9,x		*get the start address of the first table		
	ldx 7,x		*get the start address of the second table		
loop	tsta		*test whether we have looked all the pairs of numbers from the tables
	beq	ret00	*if so, return 00
	ldab    0,y	*get the current number from the first table
	cmpb    0,x	*compare this number to the paired number in the second table
	blt	retff	*if "less than", then return $ff 
	deca		*decrement the counter
	iny		*move on to the next number of the first table
	inx		*move on to the next number of the second table
	bra	loop	*keep looping

ret00	ldaa	#0	*prepare to return 0 through A
	bra	restore
retff	ldaa	#$ff	*prepare to return #$ff  through A

restore	pulb		*restore the stack
	puly
	pulx
	rts


	end	main