# 3827 Homework 3, problem 1, "rindex"
#
# <your name here>, <your uni here>
#
# Invoke with, e.g., spim -file rindex.s
#	
	.text
	# $a0 : s
	# $a1 : c
rindex:
	# REPLACE WITH YOUR CODE
	move $v0, $0
	
	jr $ra

######################################################################
#
# Test harness: extend to add tests
#	
	      
	.data
hello:
	.asciiz "Hello World!"

fox:	.asciiz "The quick brown fox jumps over the lazy dog"

	# Add more test strings here
	
looking: .ascii "Looking for '"
lookingchar:
	.asciiz "X' in \""

found:	.asciiz "\"\nFound at position "
notfound: .asciiz "\"\nNot found"
nl:	.asciiz "\n"
	
	.text

	# $a0 : s
	# $a1 : c
test:
	addi $sp, $sp, -16
	sw $ra, 0($sp)
	sw $s0, 4($sp)
	sw $s1, 8($sp)

	move	$s0, $a0
	move	$s1, $a1

	# Print what character we're looking for
	la	$t0, lookingchar
	sb	$s1, 0($t0)
	la	$a0, looking
	li	$v0, 4
	syscall

	# Print the string being searched
	move	$a0, $s0
	li	$v0, 4
	syscall

	# Find the character
	move	$a0, $s0
	move	$a1, $s1
	jal	rindex

	beq	$v0, $zero, didnotfind

	move	$s2, $v0

	# Found: report the character offset where it was found
	la 	$a0, found
	li	$v0, 4
	syscall

	subu	$a0, $s2, $s0
	li	$v0, 1
	syscall	

	b	testexit
didnotfind:
	# Not found: report that
	la 	$a0, notfound
	li	$v0, 4
	syscall

testexit:
	la	$a0, nl
	li	$v0, 4
	syscall
	
	lw $ra, 0($sp)
	lw $s0, 4($sp)
	lw $s1, 8($sp)
	addi $sp, $sp, 16
	jr $ra
	
	.globl main

main:
	move	$s0, $ra

	la	$a0, hello
	li	$a1, 'e'
	jal	test

	la	$a0, hello
	li	$a1, 'l'
	jal	test

	la	$a0, hello
	li	$a1, 'z'
	jal	test

	la	$a0, hello
	li	$a1, 0
	jal	test	

	la	$a0, fox
	li	$a1, 'z'
	jal	test

	# Add tests here

	move	$ra, $s0
	jr	$ra
