Sign in to follow this  
Paint Chugin Pete

help with SPIM

Recommended Posts

Hey ya'll, tryin to learn SPIM and got a couple things working, decided to try something tough but its not being very nice. Essentially I'm trying to input 2 strings and compare which letters are found in common between the two. I know its not the most efficient piece of code but hey, got to start somewhere:
	.data

prompt1:	.asciiz "Enter a string\n"
prompt2:	.asciiz "Enter another string\n"
output:		.asciiz "The following letters are found in common:\n"
array:		.space 27
string1:	.space 128
string2:	.space 128


	.text

main:
	# Initialization
	li $s3,0			# Size counter for array.
	li $s4,0			# Size counter for string1.
	li $s5,0			# Size counter for string2.
	li $s6,0			# Dummy counter for $s3
	li $s7,0			# Dummy counter for $s5

	# Print first prompt for string.
	li $v0,4
	la $a0,prompt1
	syscall

	# Obtain first string.
	li $v0,8
	la $a0,string1
	la $a1,126
	syscall
	
	la $s1,string1
	
	# Print second prompt for string.
	li $v0,4
	la $a0,prompt2
	syscall

	# Obtain second string.
	li $v0,8
	la $a0,string2
	la $a1,126
	syscall
	
	la $s2,string2
	
	
string1Size:
	lbu $t0,0($s1)
	beqz $t0,string2Size
	addi $s4,$s4,1
	addi $s1,$s1,1
	j string1Size
	
	
string2Size:
	lbu $t1,0($s2)
	beq $t1,$zero,change
	addi $s5,$s5,1				# Increment size counter for string2.
	addi $s7,$s7,1				# Increment size counter for string2 dummy.
	addi $s2,$s2,1				# Increment to next character index.
	j string2Size

	
# Must decrement position of first string before entering loop
change:
	sub $s1,$s1,1
	addi $s4,$s4,1
	j loop1
	
	
# Outer Loop
loop1:
	beqz $s4,message			# Go to 'message' once traversed through all the characters in string1.
	addi $s1,$s1,1				# Increment to next letter of string1.
	sub $s4,$s4,1				# Decrement size counter of string1.
	j loop2						# Jump to inner loop.


# Inner Loop
loop2:
	move $s6,$s3				# Reset counter of array.
	la $s0,array				# Reset index to start of array.

	lbu $t0,0($s1)
	lbu $t1,0($s2)

	addi $s2,$s2,1				# Increment to next letter of string2.
	sub $s7,$s7,1				# Decrement size counter of string2.
	beq $t0,$t1,check			# Go to 'check' if the current character of string1 matches the current character of string2.
	bnez $s7,loop2				# Loop back to start of 'loop2' when the size counter of string2 is not equal to zero.
	
	la $s2,string2				# Reset index to start of string2.
	move $s7,$s5				# Reset counter for string2.
	j loop1						# Return to outer loop once completely traversed through the inner loop.


# Check to see if character is already stored in the array to avoid repetition.
check:
	beq $s0,$s1,loop1			# Return to outer loop if character is already present in the array.
	beqz $s3,insert				# Go to 'insert' if size counter of array is zero.
	addi $s0,$s0,1				# Increment index of array
	sub $s6,$s6,1				# Decrement array counter.
	j check						# Jump back to the start of 'loop3'.
	
	
insert:
	addi $s0,$s0,1				# Increment index of array.
	sw $s0,0($s1)				# Add the current letter into the end of the array.
	j loop1						# Return to outer loop.


message:
	# Print 'output'.
	li $v0,4
	la $a0,output
	syscall
	
	la $s0,array


print:
	beqz $s3,close

	# Print a character at the current index of the array
	li $v0,4
	la $a0,0($s0)
	syscall
	
	addi $s0,$s0,1				# Decrement array to next index.
	sub $s3,$s3,1				# Decrement array counter.
	j print


close:	
	# Close program.
	li $v0,10
	syscall


But this is the error that I get after tryin to run it (x3): Exception 5 [address error in store] occured and ignored Edit: Also, how do you put your code in a box? I'll edit that ASAP as soon as I figure it out :) [Edited by - Paint Chugin Pete on January 29, 2008 9:03:21 PM]

Share this post


Link to post
Share on other sites
Quote:
Original post by Paint Chugin Pete
Edit: Also, how do you put your code in a box? I'll edit that ASAP as soon as I figure it out :)


You know, I bet that gets asked a lot. They really need to make that link (at the top of every page) more prominent :/

Share this post


Link to post
Share on other sites
Look in the spim console, you'll see the address of the instruction that caused the exception and the reason for the exception. From there it should be easy to spot your error.

Hint: You got the load right.

Share this post


Link to post
Share on other sites
lol, I've been trying to analyze it but its still a cipher to me. Keep in mind I've only been using SPIM for a few days so the concept of programming in assembly language is still new to me. Should I be comparing the error at the PC address to the report? If so then the error is occuring on line 109 where I'm (attempting) to store the character?

Share this post


Link to post
Share on other sites
Quote:
Original post by Paint Chugin Pete
lol, I've been trying to analyze it but its still a cipher to me. Keep in mind I've only been using SPIM for a few days so the concept of programming in assembly language is still new to me. Should I be comparing the error at the PC address to the report? If so then the error is occuring on line 109 where I'm (attempting) to store the character?


Yes, 109 would be it. You manage to load characters just fine in a bunch of other places, but you're not actually attempting to store characters at 109. Consider the difference between lbu and sw, besides the fact that one is a load and the other is a store.

Share this post


Link to post
Share on other sites
Well if I understand it correctly, I can see where you're gettin at. This will only store the address of the array and not the ascii value itself. But I forgot to mention that I've tried loading the character of string1 at the current index into a temporary register and doing the 'sw' on the temp register instead but that only yields more exceptions at the same line. It also brings up the fact that I forgot to increment the size of the array every time I'm inserting but that doesn't totally eliminate the problem lol.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this