7 Replies - 1592 Views - Last Post: 28 October 2014 - 03:21 PM

#1 Sa1ntest   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 23-October 14

Need help with a piece of my simple MIPS code

Posted 23 October 2014 - 11:05 PM

I say simple, but truth be told this is very difficult for me since I have a very shaky understanding of the MIPS syntax. But anyways, I am writing a simple program that prompts the user to enter a value for the number of integers that will be entered, then prompt for each of those integer values. The program will then print the smallest of the integers entered. My trouble lies with figuring out how to update the register holding the variable for the lowest value.


I have uploaded two files, FindLow is my current code, and addem is an example summing code that mimics exactly how I want my interface to look like.


The main piece of code where my problem is in is in the section following "#get integer from user"

I don't have the code of all my different approaches unfortunately, I only have my current code.

The two codes for convenience
# This program takes an array of numbers as input
#   and finds the lowerst value


	.data
	array: .space 80
	
# Constant strings to be output to the terminal


prompt1:    	.asciiz "How many numbers?(Max 20): "
prompt2:    	.asciiz "Number["
prompt3:	.asciiz	"]: "

 result:	.asciiz "Lowest Integer is: "

   endl:	.asciiz	"\n"

	.text

   main:  
   	la $s4, array			# initialize address of array to $s4
	
# Prompt for number of items to be entered
	li	$v0,4			# code for print_string
	la	$a0,prompt1		# point $a0 to prompt string
	syscall				# print the prompt

# get an integer from the user
	li	$v0,5			# code for read_int
	syscall				# get an int from user --> returned in $v0
	move	$s0,$v0			# move the resulting int to $s0
	
	move 	$s5,$0			#initial min value


getnum:   
# Prompt for number of items to be entered
	li	$v0,4			# code for print_string
	la	$a0,prompt2		# point $a0 to prompt string
	syscall				# print the prompt
	
# print out the array index
	li	$v0,1			# code for print_int
	move 	$s1,$0			#intialize i = 0
	move	$a0,$s1			# put array index in $a0
	syscall				# print out array index

# Prompt for number of items to be entered
	li	$v0,4			# code for print_string
	la	$a0,prompt3		# point $a0 to prompt string
	syscall				# print the prompt
# Get integer values from user
	li 	$v0,5			# code for read_int
	syscall				# get an int from user --> returned in $v0
	move	$s2,$v0			# move the resulting int to $s2

	beq   	$s0,$s1,done		# if all elements examined, quit

	addi  	$s1,$s1,4	 	# increment index i by 1
	 
	bge	$s2, ,getnum		# check min
	
	add	$s5, $s2, $s5		#Sets min=$s2
	
	j getnum			NEED HELP WITH THESE 2
done:
# output minimum

# print line feed
	li	$v0,4			# code for print_string
	la	$a0,endl		# point $a0 to string
	syscall				# print the linefeed

# print result string
	li	$v0,4			# code for print_string
	la	$a0,result		# point $a0 to string
	syscall				# print the result string
	
# print out the result
	li	$v0,1			# code for print_int
	move	$a0,$s3			# put result in $a0
	syscall				# print out result

# print line feed
	li	$v0,4			# code for print_string
	la	$a0,endl		# point $a0 to string
	syscall				# print the linefeed

# All done, thank you!
	li	$v0,10			# code for exit
	syscall				# exit program





# This program takes an array of numbers as input
#   and computes the sum


	.data

# Constant strings to be output to the terminal



prompt1:    	.asciiz "How many numbers?"
prompt2:    	.asciiz "Number["
prompt3:	.asciiz	"]: "

result:		.asciiz "Sum is: "

linefeed:	.asciiz	"\n"

    .text

main: 

# Prompt for number of items to be entered
	li	$v0,4			# code for print_string
	la	$a0,prompt1		# point $a0 to prompt string
	syscall				# print the prompt

# get an integer from the user
	li	$v0,5			# code for read_int
	syscall				# get an int from user --> returned in $v0
	move	$s0,$v0			# move the resulting int to $s0


# Loop for $s1 = 0 to $s0-1, getting numbers as input
#      $s1: loop index
#      $s2: current input
#      $s3: sum

     # initialize loop index and sum
	li	$s1,0		# $s1 <-- 0
	li	$s3,0		# $s3 (sum) <-- 0

getnum:	
# Prompt for number of items to be entered
	li	$v0,4			# code for print_string
	la	$a0,prompt2		# point $a0 to prompt string
	syscall				# print the prompt
	
# print out the array index
	li	$v0,1			# code for print_int
	move	$a0,$s1			# put array index in $a0
	syscall				# print out array index

# Prompt for number of items to be entered
	li	$v0,4			# code for print_string
	la	$a0,prompt3		# point $a0 to prompt string
	syscall				# print the prompt

# get an integer from the user
	li	$v0,5			# code for read_int
	syscall				# get an int from user --> returned in $v0
	move	$s2,$v0			# move the resulting int to $s2

	add	$s3,$s3,$s2	# add to total

	addi	$s1,$s1,1	# increment loop counter
	blt	$s1,$s0,getnum	# loop until $s1 = $s0


# output total

# print line feed
	li	$v0,4			# code for print_string
	la	$a0,linefeed		# point $a0 to string
	syscall				# print the linefeed

# print result string
	li	$v0,4			# code for print_string
	la	$a0,result		# point $a0 to string
	syscall				# print the result string
	
# print out the result
	li	$v0,1			# code for print_int
	move	$a0,$s3			# put result in $a0
	syscall				# print out result

# print line feed
	li	$v0,4			# code for print_string
	la	$a0,linefeed		# point $a0 to string
	syscall				# print the linefeed


# All done, thank you!
	li	$v0,10			# code for exit
	syscall				# exit program

Attached File(s)



Is This A Good Question/Topic? 0
  • +

Replies To: Need help with a piece of my simple MIPS code

#2 JackOfAllTrades   User is offline

  • Saucy!
  • member icon

Reputation: 6258
  • View blog
  • Posts: 24,026
  • Joined: 23-August 08

Re: Need help with a piece of my simple MIPS code

Posted 24 October 2014 - 02:46 AM

Moved to Assembly.
Was This Post Helpful? 0
  • +
  • -

#3 turboscrew   User is offline

  • D.I.C Lover
  • member icon

Reputation: 171
  • View blog
  • Posts: 1,112
  • Joined: 03-April 12

Re: Need help with a piece of my simple MIPS code

Posted 24 October 2014 - 04:39 PM

Well:

Quote

58 beq $s0,$s1,done # if all elements examined, quit
60 addi $s1,$s1,4 # increment index i by 1
64 add $s5, $s2, $s5 #Sets min=$s2

line 58 and 60: Index or address?
line 64: you don't set. You add. The other source is supposed to be zero?

I wonder if that helps. If not, do continue asking.
Was This Post Helpful? 0
  • +
  • -

#4 Sa1ntest   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 23-October 14

Re: Need help with a piece of my simple MIPS code

Posted 25 October 2014 - 01:57 AM

Thanks for the reply turbo, excuse type errors, im replyijg from my phone..
line 58 is talking about index..so pretty much an
(i<= $s0), where $s0 is the user input from line 32.

Ive changed my code by deleting line 22 and editing the get line label
# Get integer values from user
	beq   	$s0,$s1,done		# if all elements examined, quit
	
	li 	$v0,5			# code for read_int
	syscall				# get an int from user --> returned in $v0
	sw	$v0, array($s1)		#store the input to current element of array

	lw	$s2, array($s1)		#load current input
	addi  	$s1,$s1,4	 	# increment index i by 1, load next 
	lw 	$
	
	blt	$s3,$s2 ,getnum		#check min   
	move	$s4, $s2		#Sets min=$s2
	
	j getnum			
done:



Do basically i want to store the values of my input into the array, then i want compare past inputs to find the smallest integer. I have no idea how to read a past input..ive tried copying to different registers, but everything i do will overwite the value im trying to compare to, leaving me with only the curremt input. i heard this was easier to do using an array butni cant figure it out.
Was This Post Helpful? 0
  • +
  • -

#5 turboscrew   User is offline

  • D.I.C Lover
  • member icon

Reputation: 171
  • View blog
  • Posts: 1,112
  • Joined: 03-April 12

Re: Need help with a piece of my simple MIPS code

Posted 25 October 2014 - 03:03 PM

What I meant, is that on line 58 you treat $s1 as an index, but on line 60 like it was an address.
You increment it with size of int instead of 1.


On line 64 you do basically "min = minn + current" instead of "min = current".
You probably meant to do
add $s5, $s2, [b]$0[/b]  #Sets min=$s2


The linenumbers refer to the first listing in your first posting.

When it comes to the data, do you find any store instructions to ($s4)?
Like stw $s2 ($s4).

Also, you branch always to 'getnum' where, before you read the next number, do:
45	    move    $s1,$0          #intialize i = 0


Your last code has the same branch issue, and, if you haven't change the register usage:
    la $s4, array           # initialize address of array to $s4


It seems you got confused about which register is used for what.
So, before rushing into a new version, comment the register usage at the beginning and stick to it.

It looks to me something like:
s0 = count of data items
s1 = data item index
s2 = data item
s3 = not used
s4 = data item address (or array base address)
s5 = current min

This post has been edited by turboscrew: 25 October 2014 - 04:04 PM

Was This Post Helpful? 0
  • +
  • -

#6 Sa1ntest   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 23-October 14

Re: Need help with a piece of my simple MIPS code

Posted 27 October 2014 - 12:52 PM

I decided to make some kind of big changes to my code, so I will post the whole thing.
I decided to use seperate counter i and j so that I can make sure that the number of values I enter is the same as the value I entered at the beginning
and still keep my interface as:

Number[1]:
Number[2]:

instead of

Number[0]
Number[1]

Im sure there is a simpler way of accomplishing this, but for now it works.

I have two problems with my new code, however.

After my second input I am getting two errors:

Exception 5 [Address error in store] occurred and ignored
Exception 4 [Address error in inst/data fetch] occurred and ignored

And my code isn't actually returning the lowest value, but says

Lowest Integer is: 0

everytime I run the code, regardless of what I enter.

I am probably butchering the use of the arrays.

So, my code

# This program takes an array of numbers as input
#   and finds the lowerst value


	.data
	array: .space 80
	
# Constant strings to be output to the terminal


prompt1:    	.asciiz "How many numbers?(Max 20): "
prompt2:    	.asciiz "Number["
prompt3:	.asciiz	"]: "

 result:	.asciiz "Lowest Integer is: "

   endl:	.asciiz	"\n"

	.text

   main:  

# Prompt for number of items to be entered
	li	$v0,4			# code for print_string
	la	$a0,prompt1		# point $a0 to prompt string
	syscall				# print the prompt

# get an integer from the user
	li	$v0,5			# code for read_int
	syscall				# get an int from user --> returned in $v0
	move	$s0,$v0			# move the resulting int to $s0

# Loop for $s1 = 0 to $s0-1, getting numbers as input
#      $s1: loop index
#      $s2: current input
#      $s4: smallest value

	la	$s6,array

	li 	$s1,1			#intialize i = 1
	li	$s4,500			# $s4 (small) <-- 500
	li	$s2,0			#initialize j=0

getnum:   

	beq   	$s0,$s2,done		# if all elements examined, quit

# Prompt pt1 for numbers
	li	$v0,4			# code for print_string
	la	$a0,prompt2		# point $a0 to prompt string
	syscall				# print the prompt
	
# print out the array index
	li	$v0,1			# code for print_int
	move	$a0,$s1			# put array index in $a0
	syscall				# print out array index

# Prompt pt2 for numbers
	li	$v0,4			# code for print_string
	la	$a0,prompt3		# point $a0 to prompt string
	syscall				# print the prompt

# Get integer	
	
	li 	$v0,5			# code for read_int
	syscall				# get an int from user --> returned in $v0
	sw	$v0, array($s2)		#store the input to current element of array

	lw	$t0, array($s2)
	slt	$t1, $t0, $s4 		#$t0 < small?

	addi  	$s1,$s1, 1	 		#increment index i by 1
	addi	$s2,$s2, 1			#increment index j by 1	

	beq	$t1, $0, getnum	   	#if not, branch back to start of loop
	
	move	$s4, $t0		#set Small = $t0

	j	getnum		

done:
# output Smallest

# print line feed
	li	$v0,4			# code for print_string
	la	$a0,endl		# point $a0 to string
	syscall				# print the linefeed

# print result string
	li	$v0,4			# code for print_string
	la	$a0,result		# point $a0 to string
	syscall				# print the result string
	
# print out the result
	li	$v0,1			# code for print_int
	move	$a0,$s4			# put result in $a0
	syscall				# print out result

# print line feed
	li	$v0,4			# code for print_string
	la	$a0,endl		# point $a0 to string
	syscall				# print the linefeed

# All done, thank you!
	li	$v0,10			# code for exit
	syscall				# exit program


Was This Post Helpful? 0
  • +
  • -

#7 Sa1ntest   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 23-October 14

Re: Need help with a piece of my simple MIPS code

Posted 27 October 2014 - 01:54 PM

Made another change and got it working!!!

Here is my new code:
	.data
	array: .space 80

	prompt1:    	.asciiz "How many numbers?(Max 20): "
	prompt2:    	.asciiz "Number["
	prompt3:	.asciiz	"]: "

 	 result:	.asciiz "Lowest Integer is: "

  	   endl:	.asciiz	"\n"

	.text

main:  

# Prompt for number of items to be entered
	li	$v0,4			# code for print_string
	la	$a0,prompt1		# point $a0 to prompt string
	syscall				# print the prompt

# get an integer from the user
	li	$v0,5			# code for read_int
	syscall				# get an int from user --> returned in $v0
	move	$s0,$v0			# move the input int to $s0

# Loop for $s1 = 0 to $s0-1, getting numbers as input
#      $s1: loop index
#      $s2: current input
#      $s4: smallest value

	la	$s6,array

	li 	$s1,1			#intialize i = 1
	li	$s4,99			# $s4 (small) <-- 500
	li	$s2,0			#initialize j=0

getnum:   

	beq   	$s0,$s2,done		# if all elements examined, quit

# Prompt pt1 for numbers
	li	$v0,4			# code for print_string
	la	$a0,prompt2		# point $a0 to prompt string
	syscall				# print the prompt
	
# print out the array index
	li	$v0,1			# code for print_int
	move	$a0,$s1			# put array index in $a0
	syscall				# print out array index

# Prompt pt2 for numbers
	li	$v0,4			# code for print_string
	la	$a0,prompt3		# point $a0 to prompt string
	syscall				# print the prompt

# Get integer	
	
	li 	$v0,5			# code for read_int
	syscall				# get an int from user --> returned in $v0

	move	$t0, $v0		#stores input into $t0

	sll 	$t6, $s2, 2 		# $t2 = 4 * j
	sll 	$t6, $t6, 2 		# forming the byte address

	sw	$t0, 0($s6)		# Array[4*j] = $t0


	slt	$t1, $t0, $s4 		#$t0 < small?

	addi  	$s1,$s1, 1	 		#increment index i by 1
	addi	$s2,$s2, 1			#increment index j by 1	

	beq	$t1, $0, getnum

	move	$s4, $t0		# Small = $t0

	j	getnum		

done:
# output Smallest

# print line feed
	li	$v0,4			# code for print_string
	la	$a0,endl		# point $a0 to string
	syscall				# print the linefeed

# print result string
	li	$v0,4			# code for print_string
	la	$a0,result		# point $a0 to string
	syscall				# print the result string
	
# print out the result
	li	$v0,1			# code for print_int
	move	$a0,$s4			# put result in $a0
	syscall				# print out result

# print line feed
	li	$v0,4			# code for print_string
	la	$a0,endl		# point $a0 to string
	syscall				# print the linefeed

# All done, thank you!
	li	$v0,10			# code for exit
	syscall				# exit program
		



Now however, I do not really understand why it works, I essentially took code from other programs and tweaked to get it working. Answering this may help me with my next two questions.

I also would like to change my code a bit. As stands the user can only implement a total of 20 numbers because of the array size I entered on line 002. How do you change the code to to create a dynamic size? Meaning, the size changes depending on the number entered in the segment on line 022?

Also, I initialized the small number integer to an arbitrary value of 100. This is flawed because if a user inputs values that are higher than that, the code wont work. I need help fixing this.

Thank you for looking at my code!!
Was This Post Helpful? 0
  • +
  • -

#8 turboscrew   User is offline

  • D.I.C Lover
  • member icon

Reputation: 171
  • View blog
  • Posts: 1,112
  • Joined: 03-April 12

Re: Need help with a piece of my simple MIPS code

Posted 28 October 2014 - 03:21 PM

The error sounds like alignment error - many processors don't allow word access to odd address and similar.

Now you seem to store the data as words instead of bytes.

You could do with the previous version too (I think) if you used byte load and store instead of word load and store.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1