#	This is a demonstration program to test the string procedures that must
#	be written for Project #4. 
#	This program prompts for two lines, allowing up to 85 characters in each
#	line. It is intended that the user enters two lines shorter than 85 
#	characters so that a newline is stored with each input string.  The 
#	first string must contain at least two n's; the second must contain at
#	least two r's.  The comments below indicate what calls should be made to 
#	the procedures to examine and manipulate the two input strings.

	.text
	.globl	main
main:
	addi	$sp, $sp, -8		# Standard save of registers
	sw	$fp, 4($sp)
	sw	$ra, 0($sp)
	addi	$fp, $sp, 4

	la	$a0, prompt1		# Ask for the first line (with 2 n's)
	li	$v0, 4
	syscall
	la	$a0, first		# Read it in
	li	$v0, 8
	li	$a1, 85
	syscall
	la	$a0, prompt2		# Ask for the second line (with 2 r's)
	li	$v0, 4
	syscall
	la	$a0, second		# Read it in
	li	$v0, 8
	li	$a1, 85
	syscall

# Eliminate the newline at the end of first, use findnext to help
	la	$a0, first		# Locate newline
	li	$a1, 10
	jal	findnext
	sb	$0, 0($v0)		# Replace it with a null

# Eliminate the newline at the end of second, use findnext to help
	la	$a0, second		# Locate newline in second
	li	$a1, 10
	jal	findnext
	sb	$0, 0($v0)		# Replace with a null

# Determine length of first line
	la	$a0, first		
	jal	length
	la	$a0, first
	addi	$t0, $v0, -10		# Make pointer to last 10 characters
	add	$a0, $a0, $t0

# Display the last 10 characters of first
	li	$v0, 4
	syscall				# Display last 10 characters

# Determine length of second line
	la	$a0, second		
	jal	length
	la	$a0, second
	addi	$t0, $v0, -10		# Make pointer to last 10 characters
	add	$a0, $a0, $t0

# Display the last 10 characters of second on the same line
	li	$v0, 4
	syscall				# Display last 10 characters
	la	$a0, annot1
	syscall				# Display label for 20 characters

# Locate the first and second occurrence of 'n' in first, use findnext to help
	la	$a0, first
	li	$a1, 0x6e		# Find first n in first line
	jal	findnext
	move	$s0, $v0		# Save location
	addi	$a0, $v0, 1
	li	$a1, 0x6e		# Find second n in first line
	jal	findnext
	sub	$s1, $v0, $s0
	addi	$s1, $s1, 1		# Get number of characters n to n

# Extract the characters between the n's (and the n's themselves) into a string 
# at part1, use substr to help
	la	$a0, first		# Set up substr
	sub	$a1, $s0, $a0		# Get starting position
	move	$a2, $s1
	la	$a3, part1
	jal	substr

# Locate the first and second occurrence of r in second, use findnext to help
	la	$a0, second
	li	$a1, 0x72		# Find first r in second line
	jal	findnext
	move	$s0, $v0		# Save location
	addi	$a0, $v0, 1
	li	$a1, 0x72		# Find second r in second line
	jal	findnext
	sub	$s1, $v0, $s0
	addi	$s1, $s1, 1		# Get number of characters r to r

# Extract the characters between the r's (and the r's themselves) into a string 
# at part2, use substr to help
	la	$a0, second		# Set up substr
	sub	$a1, $s0, $a0		# Get starting position
	move	$a2, $s1
	la	$a3, part2
	jal	substr

# Concatenate the strings at part1 and part into a string at join, 
# use concatenate to help
	la	$a0, part1		# Join the two substrings
	la	$a1, part2
	la	$a2, join
	jal	concatenate

# Display the concatenated string with some annotation
	la	$a0, join
	li	$v0, 4
	syscall
	la	$a0, annot2
	syscall

# Insert string at plugin at invalid location - test error
	la	$a0, plugin
	la	$a1, second
	li	$a2, 200
	jal	insert
# Insert string at plugin at character 12 of first
	la	$a0, plugin
	la	$a1, first
	li	$a2, 12
	jal	insert

# Display the new version of first
	li	$v0, 4
	la	$a0, first
	syscall
	lw	$fp, 4($sp)	# Restore the stack
	lw	$ra, 0($sp)
	addi	$sp, $sp, 8
	jr	$ra

	.data
first:	.space		85
second:	.space		85
prompt1:	.asciiz		"Enter a line of text with at least 2 n's in it\n"
prompt2:	.asciiz		"Enter a second line with at least 2 r's in it\n"
annot1:		.asciiz		"   Last 10 characters of each line\n\n"
annot2:		.asciiz		"   n to n and r to r\n\n"
plugin:		.asciiz		"*-=%$%=-*"
part1:	.space		80
part2:	.space		80
join:	.space		160

#	Search a string (address in $a0) for a character (in $a1)
#	Return in $v0 address where char is found (first occurrence)
#	or a value of 0 if character is not found
#	The string is assumed to be null terminated.

	.text
findnext:
	addi	$sp, $sp, -12	# Make space for procedure frame
	sw	$s0, 8($sp)	# Save register used in procedure
	sw	$fp, 4($sp)	# Save $fp (std)
	sw	$ra, 0($sp)	# Save $ra (std)
	addi	$fp, $sp, 8	# Set $fp to beginning

	move	$s0, $a0	# Get address to start
	move	$t0, $a1	# Get character to compare
top:
	lb	$t1, 0($s0)	# Get a byte of the string
	sub	$t2, $t1, $t0
	beqz	$t2, found	# If equal to char, found
	addi	$s0, $s0, 1	# On to next byte
	beqz	$t1, notfound	# If string byte is null, end it
	j	top
found:
	move	$v0, $s0	# Set up return address
	j	leave
notfound:
	li	$v0, 0		# Return zero
leave:
	lw	$s0, 8($sp)	# Restore registers
	lw	$fp, 4($sp)
	lw	$ra, 0($sp)
	addi	$sp, $sp, 12
	jr	$ra

length:				# Placeholder functions
	li	$v0, 0
	jr	$ra

concatenate:
	jr	$ra

substr:
	jr	$ra

insert:
	jr	$ra
