	* 1BA3 Lab #6
	* 23rd of January 2002 
	* Written by Edsko de Vries

	* 1.
	* Write an assembly language program that will compare the zero
	* terminated strings starting at $4200 and $4300 and return in d0
	* the value 0 if the strings are different and 1 if they are the same.
	* (Note: The strings must be of the same length to be the same.)

	* 2.
	* Write an assembly language program that will determine the largest
	* signed longword in memory between the addresses of $1000 and
	* $2000 and return this in d0

	* NOTE: It will take a while to execute (2) in the simulator. You might
	* want to decrease the length of the interval

***
* Some simulator specific definitions
****
CPUHALT	EQU	$F000

*** 
* The main program
****
	org	$1000

	* Work around buggy MOVEA/LEA
	move.l	#STR1,d0
	move.l	d0,a0
	move.l	#STR2,d0
	move.l	d0,a1
	bsr	STRCMP

	* Again, work around buggy MOVEA/LEA
	move.l	#$1000,d0
	move.l	d0,a0
	move.l	#$2000,d0
	move.l	d0,a1
	bsr	SRCHINT

	dc.w	CPUHALT

***
* Compare two strings A and B 
*
* PARAMETERS
* a0.l	Offset of A	
* a1.l	Offset of B	
*
* RETURN VALUE
* d0.b	1 if the A = B, 0 otherwise
****
STRCMP	move.l	a0,-(a7)	* Push a0-a1 
	move.l	a1,-(a7)	* MOVEM doesn't work yet :)

	move.b	#1,d0		* Assume strings equal
	
SCMP_L1	tst.b	(a0)		* End of A?
	beq	SCMP_J1
	tst.b	(a1)		* End of B?
	beq	SCMP_NE		* Not end of A, strings are unequal
	cmpm.b	(a0)+,(a1)+	* [A] == [B] ?
	bne	SCMP_NE		* Nope, strings unequal
	bra	SCMP_L1		* Loop

SCMP_J1	tst.b	(a1)		* End of A, also end of B?
	bne	SCMP_NE		* No, strings unequal	
	bra	SCMP_E1		* Yes, strings are equal

SCMP_NE	eor.b	d0,d0		* Clear d0 

SCMP_E1	move.l	(a7)+,a1	* Pop a0-a1
	move.l	(a7)+,a0

	rts			* And return from subroutine

***
* Find the largest signed longword in the interval [a0,a1)
*
* PARAMETERS
* a0	Begin of the interval
* a1	End of the interval
*
* RETURN VALUE
* d0	The largest signed longword
*	Will be zero for an empty interval
****
SRCHINT	move.l	a0,-(a7)	* Push a0

	eor.l	d0,d0		* Clear d0
	
SRCI_L1	cmp.l	a0,a1		* End of interval?
	beq	SRCI_E1	
	cmp.l	(a0)+,d0	* Larger longword?
	bgt	SRCI_L1		* Nope
	move.l	-4(a0),d0	* Yes, replace d0
	bra	SRCI_L1		

SRCI_E1	move.l	(a7)+,a0	* Pop a0

	rts

***
* Some sample data
****
	org	$4200
STR1	dc.w	'This is the first string',0

	org	$4300
STR2	dc.w	'This is the second string',0

