	* 1BA3 Lab #5, 9th January 2002 
	* Written by Edsko de Vries
	
	* Write an assembly language program that calculates the even parity over the 31 least
	* significant bits in d0 and sets the most significant bit (MSB) in d0 as even-parity bit
	* accordingly. You must use a WHILE or DO-WHILE loop for this task.
	*
	* Your program should use an if-construct to write `odd' into the memory locations starting
	* at $2000 if the number of bits was odd and `even' if the number of bits was even.
	
	* Note, this program would probably be neater with the use of subroutines (see next sample prog)

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

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

	* Move some value into d0
	move.l	#$12345678,d0

	* Calculate the number of 1's in d0
	andi.l	#$7FFFFFFF,d0	* Get rid of MSB
	move.b	#0,d2		* Assume no 1's in d0 
	move.b	#32,d1		* lp1 = 31;
				* do {
MAIN_L1	ror.l	#1,d0		*   shift right
	bcc	MAIN_C1		*   if( carry )  
	add.b	#1,d2		*      One more 1
MAIN_C1	sub.b	#1,d1		*   lp1--;
	bne	MAIN_L1		* } while( lp1 )

	* Now d2 contains the number of 1's. The LSB of d2 
	* is the required parity bit
	ror.l	#1,d2		* Get parity bit in the right place
	andi.l	#$80000000,d2	* Mask out all bits except the parity bit
	beq	MAIN_J1		* If zero, number of bits was even
	bra	MAIN_J2
MAIN_J4	or.l	d2,d0		* And OR it with d0

	* By now $2000 points to the string `even' or `old'
	* We can use the simulator to print this for us
	move.l	#$2000,d0	* Buggy LEA/MOVEA
	move.l	d0,a0
	dc.w	PRNTSTR
	dc.w	CPUHALT
	
MAIN_J1	move.l	#ODD,d0		* Buggy LEA/MOVEA
	move.l	d0,a1		* Select the required string
	bra	MAIN_J3
MAIN_J2	move.l	#EVEN,d0
	move.l	d0,a1
MAIN_J3	move.l	#$2000,d0	* Destination
	move.l	d0,a0
	
MAIN_L2	tst.b	(a1)		* End of the string?
	beq	MAIN_J4		* Yes, exit loop
	move.b	(a1)+,d0	* Get next byte
	move.b	d0,(a0)+	* And copy it
	bra	MAIN_L2

*** 
* The strings `odd' and `even'
****
ODD	dc.w	'odd',0
EVEN	dc.w	'even',0
