#	Chapter 2 example
#	Reverse all the characters in a string
#	We could have just used a loop that iterates in reverse order, but stacks are more fun
#	Demonstrates recrusive procedures and stack

	.data
str:	.space	1024

	.text
main:	li	$v0,8
	la	$a0,str
	li	$a1,1024
	syscall			# read string from user
	li	$a1,0
	jal	rev		# first call to recursion
	li	$v0,10
	syscall			# print reversed string to user

rev:	addi	$sp,$sp,-16	# push four registers onto stack
	sw	$a0,0($sp)
	sw	$a1,4($sp)
	sw	$a2,8($sp)
	sw	$ra,12($sp)

	add	$t0,$a0,$a1	# find byte location
	lb	$a2,0($t0)	# load byte
	beq	$a2,$0,exit	# check to see if we've reached the end of the string
	addi	$a1,$a1,1	# recurse on next byte

	jal	rev		# recurse
	li	$v0,11		# system call 11:  print character
	move	$a0,$a2		# character to print in $a0 (didn't want to change this until recursive call returns)
	syscall
	
exit:	lw	$a0,0($sp)	# pop stack
	lw	$a1,4($sp)
	lw	$a2,8($sp)
	lw	$ra,12($sp)
	addi	$sp,$sp,16
	
	jr	$ra		# return to caller
