	* A small tutorial program
	* This program asks the user for a string, then reserves the string,
	* and outputs the new string to the user

***
* A few definitions specific to the simulator
****
HALT	EQU	$F000
PRNTSTR	EQU	$F002
READSTR	EQU	$F003

***
* The main program
****

	org	$400

	* Input the string from the user
	move.l	#INPUT,d0	* Work around buggy MOVEA
	move.l	d0,a1
	dc.w	READSTR

	* Reserse the string
	bsr	REVERSE

	* And output the string again
	dc.w	PRNTSTR

	* Halt the processor
	dc.w	HALT

***
* Reserse a string
*
* PARAMETERS
* a0	Start of the string
*
* RETURN VALUE
* a0	String of the new string (old string will be overwritten)
****

REVERSE	move.l	a0,-(a7)	
	
	* Look for the end of the string
	move.l	a0,a1		* Get the beginning of the string
REV_L1	tst.b	(a1)+		* Did we reach the end?
	beq	REV_E1		* Yes, Exit loop
	bra	REV_L1		* No, continue loop

	* We want the last character in the string,
	* not the zero, so we have added 2 too many to a1
REV_E1	sub.l	#2,a1

	* Start swapping until a1 <= a0
REV_L2	cmp	a0,a1		* Compare a0 to a1
	ble	REV_E2		* If a1 <= a0, exit loop
	move.b	(a0),d0		* Save the first
	move.b	(a1),d1		* Save the second
	move.b	d1,(a0)		* And store them swapped
	move.b	d0,(a1)
	adda.l	#1,a0		* Update a0 and a1
	suba.l	#1,a1
	bra	REV_L2		* Continue loop

REV_E2	move.l	(a7)+,a0	

	rts

***
* Some variables
****
INPUT	dc.w	'This program will reverse a string.<BR>Please enter a string:',0

