* file = stack1.asm
* Quick examples of how the Stack & Stack Ptr function
* Subroutine reverses the elements in a vector
* Dr. Karl Gugel, 09/2002
*
* Sim12 is based on the M68HC12AEVB. It assumes the following memory map:
* 16K External RAM at $4000-$7FFF
* 32K External ROM at $8000-$FFFF
* Internal RAM at $800-$BFF
* Internal EEPROM at $1000-$1FFF
* Note: You can load in programs anywhere in the above mentioned areas but should
*       only write to RAM (i.e. assume ROM and EEPROM are read only).  
 
******* Constants:	
stack_ptr	equ	$5000	  ;initial stack pointer
data		equ	$4000	  ;were test data will be placed
program		equ	$4100     ;program area
vec_len   	equ	7	  ;test vector length
	

******* Test Data:
	org	data
vector	dc.b	1,2,3,4,5,6,7   ;test vector 


******* Program:
	org	program
	lds	#stack_ptr		;init stack pointer

	ldaa	#vec_len		;save vector length on stack
	psha

	ldx	#vector			;save vector start address on stack
	pshx

	jsr	reverse			;reverse 

end	bra	end	


******* Supporting Subroutines:

*************************************************************************
reverse
* function: reverse the elements in a vector
* inputs: vector length & vector start address
* input passing method: inputs are pulled from the stack
* 
	pulx			;get the return address off the stack
	stx	temp		;save in temporary memory location

	pulx			;get vector start address
	pulb			;get the vector start address

	tfr	x,y		;set up the last element address
	aby			;start address + length
	dey                     ;end address = (start address + length) - 1

	asrb			;length/2

loop    ldaa	0,x		;load begin element to be switched in temp reg
	movb	0,y,1,x+	;move end element to begin element position
	staa	1,y-		;store temp reg (begin element) to end position
	dbne	b,loop
	
	ldx	temp		;restore the return address onto the stack
	pshx

	rts	 

temp	ds.w	1		;reserve word for temp storage of ret. addr.			
*************************************************************************	


