MIPS Assembly language code help

help with correcting code

Page 1 of 1

7 Replies - 9698 Views - Last Post: 07 August 2009 - 07:43 PM Rate Topic: -----

#1 imclumsy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 07-August 09

MIPS Assembly language code help

Posted 07 August 2009 - 08:10 AM

Hi I have mips question that I direly need answered. It is a code that some lines have to be filled in to make the program work. Can someone please help? Here is the code.

# Display the minimum and maximum values of an array A of n integers.
# int min = A[0];
# int max = A[0];
# for (int i = 1; i < n; i++)
{
# if (A[i] < min)
# min = A[i];
# if (A[i] > max)
# max = A[i];
# }
# Read 10 integers and store in the array A, call proc minmax( ), display minimum and
# maximum
Registers:
# $a0 = address of the array
# $a1 = n (n=10)
# $v0 = minimum
# $v1 = maximum

.data
prompt1: .asciiz "\nThe program finds the minimum & maximum of array A. "
prompt2: .asciiz "\nEnter number"
prompt3: .asciiz "\nMinimum = "
prompt4: .asciiz "\nMaximum = "
A: .space 64


.text
.globl main
main:
li $v0, 4 # print prompt1
la $a0, prompt1
syscall

li $t0, 0 # counter i = 0 (i= $t0)
li $t1, 10 # read 10 integers ($t1 = 10)
la $t2, A # $t2 points to address of array A

# Read 10 integers
loop:
la $a0, prompt2 # print prompt2 "Enter integer number"
li $v0, 4
syscall

li $v0, 1 # print the number of the input
move $a0, $t0 # $a0 = counter i
syscall

li $v0, 4 # print string system service
li $v0, 5 # read an integer
syscall

move $t3, $v0 # $t3 = $v0 = i integer
addi $t4, $t0, 4 # $t4 = offset of A[i]
add $t4, $t4, $t2 # $t4 = address of A[i], $t2= address of array A
sw $t3, 0($t4) # store i integer in A[i]
addi $t0, $t0, 1 # i = i + 1
blt $t0, $t1, loop # if (i < 10) then go to loop ($t0= 0, $t1 =10)

# Call proc minmax
la $a0, A # $a0 = address of array A
addi $a1, $0, 10 # $a1 = n = 10

lw $a0, 4($sp) # allocate 4 bytes for the stack
sw $ra, 0($sp) # push return address($ra) into the stack
jal minmax # call minmax
lw $ra, 0($sp) # pop $ra out of the stack

addi $sp, $sp, - 4 # deallocate 4 bytes from the stack
move $t0, $v0 # $t0 = $v0 = minimum of array A
move $t1, $v1 # $t1 = $v1 = maximum of array A

la $a0, prompt3 # print prompt3 "Minimum ="
li $v0, 5
syscall

li $v0, 1 # print the minimum of array A
move $a0, $t0 # $a0 = $t0 = minimum
syscall

la $a0, prompt4 # print prompt4 "Maximum ="
li $v0, 5
syscall

li $v0, 1 # print the maximum of array A
move $a0, $t1 # $a0 = $t1 = maximum
syscall

jr $ra # return to caller

# Find the min and max of array A
minmax:
lw $t0, 0($a0) # int min = A[0]; $t0=0
lw $t1, 10($a0) # int max = A[0]; $t1= 10
lw $t2, 1 # int i = 1; $t2= address of array A

loop1:
bge $t2, $a1, exit # while (i < n) $a1=n, i = $t2
addi $t3, $t3, 4 # $t3 = offset of A[i]
add $t3, $t3, $a0 # $t3 = address of A[i]
lw $t4, 0($t3) # t4 = A[i];
bge $t4, $t0, max # if (min > t4)
move $t0, $t4 # min = t4;

max:
ble $t4, $t1, next # if (max < t4)
move $t1, $t4 # max = t4;

next:
addi $t2, $t2, 1 # i = i + 1;
j loop1 # jump to loop1

exit:
move $v0, $t0 # $v0 = min($t0)
move $v1, $t1 # $v1 = max($t1)
jr $ra # return to caller 


---------------------------------------------------------------------------------------------
original code:

.data
prompt1: .asciiz "\nThe program finds the minimum & maximum of array A. "
prompt2: .asciiz "\nEnter number"
prompt3: .asciiz "\nMinimum = "
prompt4: .asciiz "\nMaximum = "
A: .space 64


.text
.globl main
main:
li $v0, 4 # print prompt1
la $a0, prompt1
syscall

li $t0, 0 # counter i = 0 (i= $t0)
li $t1, 10 # read 10 integers ($t1 = 10)
la $t2, A # $t2 points to address of array A

# Read 10 integers
loop:
------------------------ # print prompt2 "Enter integer number"
------------------------
------------------------

------------------------ # print the number of the input
------------------------ # $a0 = counter i
syscall

------------------------ # print string system service
------------------------ # read an integer
syscall

------------------------ # $t3 = $v0 = i integer
------------------------ # $t4 = offset of A[i]
add $t4, $t4, $t2 # $t4 = address of A[i], $t2= address of array A
sw $t3, 0($t4) # store i integer in A[i]
------------------------ # i = i + 1
------------------------ # if (i < 10) then go to loop ($t0= 0, $t1 =10)

# Call proc minmax
------------------------- # $a0 = address of array A
------------------------- # $a1 = n = 10

------------------------- # allocate 4 bytes for the stack
------------------------- # push return address($ra) into the stack
------------------------- # call minmax
------------------------- # pop $ra out of the stack

------------------------- # deallocate 4 bytes from the stack
------------------------- # $t0 = $v0 = minimum of array A
------------------------- # $t1 = $v1 = maximum of array A

------------------------ # print prompt3 "Minimum ="
------------------------
------------------------

------------------------ # print the minimum of array A
------------------------ # $a0 = $t0 = minimum
syscall

------------------------ # print prompt4 "Maximum ="
------------------------
syscall

------------------------ # print the maximum of array A
------------------------ # $a0 = $t1 = maximum
syscall

jr $ra # return to caller

# Find the min and max of array A
minmax:
lw $t0, 0($a0) # int min = A[0]; $t0=0
------------------------ # int max = A[0]; $t1= 10
------------------------ # int i = 1; $t2= address of array A

loop1:
bge $a1, $t1, exit # while (i < n) $a1=n, i = $t2
------------------------ # $t3 = offset of A[i]
add $t3, $t3, $a0 # $t3 = address of A[i]
lw $t4, 0($t3) # t4 = A[i];
bge $t4, $t0, max # if (min > t4)
------------------------ # min = t4;

max:
ble $t4, $t1, next # if (max < t4)
------------------------ # max = t4;

next:
------------------------ # i = i + 1;
------------------------ # jump to loop1

exit:
------------------------ # $v0 = min($t0)
------------------------ # $v1 = max($t1)
jr $ra # return to caller


Thank you ahead for any help!

Is This A Good Question/Topic? 0
  • +

Replies To: MIPS Assembly language code help

#2 Guest_Neumann*


Reputation:

Re: MIPS Assembly language code help

Posted 07 August 2009 - 09:48 AM

Dream.In.Code has a policy by which we prefer to see a good faith effort on your part before providing source code for homework assignments. Please post the code you have written in an effort to resolve the problem, and our members would be happy to provide some guidance. Be sure to include a description of any errors you are encountering as well.

Please post like this:

Thank you for helping us helping you.

This post has been edited by Neumann: 07 August 2009 - 09:49 AM

Was This Post Helpful? 0

#3 wildgoose  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 67
  • View blog
  • Posts: 468
  • Joined: 29-June 09

Re: MIPS Assembly language code help

Posted 07 August 2009 - 09:55 AM

Show us your changed version and we'll review!
Was This Post Helpful? 0
  • +
  • -

#4 imclumsy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 07-August 09

Re: MIPS Assembly language code help

Posted 07 August 2009 - 02:31 PM

the posted code in the code brackets IS the changed version. that's the code that is not working. the original code is below with dashed lines.
i posted the original code and then my changed version to show the differences. the posted code does not work and that's why i'm having trouble fixing it

the problems i'm encountering are
1) the array loop is not working
2) it is giving incorrect results

sorry, i forgot to mention that the code in the brackets in my original post was my attempt to do the program

This post has been edited by imclumsy: 07 August 2009 - 02:33 PM

Was This Post Helpful? 0
  • +
  • -

#5 wildgoose  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 67
  • View blog
  • Posts: 468
  • Joined: 29-June 09

Re: MIPS Assembly language code help

Posted 07 August 2009 - 03:51 PM

It needs to be folded into the source! Stick a comment with a tag such as #@@@
so it stands out.

First thing I noticed in the data entry loop, temporary register $t0 is used as the index count AND summed not multiplied (left shift by 2) to calculate the scalar for the offset position.

The math doesn't appear to be correct!


A[64]
A[0] is at byte offset 0
A[1] is at byte offset 4
etc.


Also 10 entries 4 byts each so why a 64 byte buffer instead of 40?

Check the index 0...10 versus offset usage! There's no harm in multiplying 10 x 4 so 4 byte stepping still works as an index and an insertion!

Make comments such as the result of the syscall! Or more detail as to what the syscall does!


	prompt2: .asciiz "\nEnter number "  #@@@ Need space so # doesn't run into ASCII!
	prompt5: .asciiz "< "		 #@@@ Need a prompt!




	# Read 10 integers
loop:
		# ____________________________
		# Print NULL Terminated String
	la $a0, prompt2 				# print prompt2 "Enter integer number"
	li $v0, 4
	syscall

		# _____________
		# Print integer
	li $v0, 1 						# print the number of the input
	move $a0, $t0 					# $a0 = counter i
	syscall

		# ____________________________
		# Print NULL Terminated String
	la $a0, prompt5					#@@@ WHAT STRING???
	li $v0, 4 						# print string system service
	syscall							#@@@  

		# _____________________
		# Read integer into $v0
	li $v0, 5 						# read an integer
	syscall

		# $v0 has integer

	move $t3, $v0 					# $t3 = $v0 = i integer

#@@@	addi $t4, $t0, 4 				# $t4 = offset of A[i]
#That line made no sense!  Should be a multiply by 4 to get byte offset!

	add $t4, $t4, $t2 				# $t4 = address of A[i], $t2= address of array A

	sw $t3, 0($t4) 					# store i integer in A[i]

	addi $t0, $t0, 1 				# i = i + 1
	blt $t0, $t1, loop 				# if (i < 10) then go to loop ($t0= 0, $t1 =10)



This post has been edited by wildgoose: 07 August 2009 - 03:52 PM

Was This Post Helpful? 0
  • +
  • -

#6 wildgoose  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 67
  • View blog
  • Posts: 468
  • Joined: 29-June 09

Re: MIPS Assembly language code help

Posted 07 August 2009 - 04:20 PM

I've looked at the second loop, note the #@@@ tags

#	__________________________________________________________________________
#	MinMax()
#	$A0=buffer	$A1=# of numbers
#

	# Find the min and max of array A
minmax:
		# Set min, max to first element in array
	lw $t0, 0($a0) 			# int min = A[0]; $t0=0
#@@@@	lw $t1, 10($a0) 		# int max = A[0]; $t1= 10
	lw $t1,$t0		 		# int max = A[0];

	  # set to second number, first one handled!
	lw $t2, 1 				# int i = 1; $t2= address of array A

#@@@ Where are your condition flags preset???
#@@@ Where did $t3 get set with 0

	# While loop
loop1:	   #
	bge $t2, $a1, exit 		# while (i < n) $a1=n, i = $t2

 #@@@ Why keep doing the offset + base math. Why not set the base first, then
 #@@@   merely advance it by four bytes each loop!

	addi $t3, $t3, 4 		# $t3 = offset of A[i]
	add $t3, $t3, $a0 		# $t3 = address of A[i]

	lw $t4, 0($t3) 			# t4 = A[i];

	bge $t4, $t0, max 		#@@@ if (min >= t4)

	move $t0, $t4 			# min = t4;

max:
	ble $t4, $t1, next 		#@@@ if (max <= t4)
	move $t1, $t4			# max = t4;

next:
	addi $t2, $t2, 1 		# i = i + 1;
	j loop1 				# jump to loop1

exit:
	move $v0, $t0 			# $v0 = min($t0)
	move $v1, $t1 			# $v1 = max($t1)
	jr $ra 					# return to caller 	


Was This Post Helpful? 0
  • +
  • -

#7 imclumsy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 07-August 09

Re: MIPS Assembly language code help

Posted 07 August 2009 - 06:25 PM

i understand some of your comments, but according to the assignment every dashed line has to be filled in with a line of code. so that's why its difficult for me to write code according to the comments. the 64 byte buffer was given as part of the code. if you check the original code which i have posted below my attempt to fill in every dashed line, it is already given that there is a 64 byte buffer for the array. writing the code according to the comments confuses me.

are u saying

#@@@	addi $t4, $t0, 4   


should be mult $t4, $t0, 4?

i'm sorry if i did the coding on my own i probably would get halfway and would need help after that, but with only the comments given, i am unable to convert accurately due to the confusing comments
Was This Post Helpful? 0
  • +
  • -

#8 wildgoose  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 67
  • View blog
  • Posts: 468
  • Joined: 29-June 09

Re: MIPS Assembly language code help

Posted 07 August 2009 - 07:43 PM

So you are saying that you generated the bulk of the code?

You are using an index +1 +1 +1

But you are trying to reference an 32-bit (four byte) integer in the A[] array.
So you need a scalar for the index!

baseAddress + index * 4
baseAddress + index << 2

Read very carefully each comment that I placed in the code snippets. Compare it to your code and the original code!
Also for note. In the second loop


$a0 = base address

Loop
add $a0,$a0,4


branch to loop for 9 items since the first one was stuffed into min and max!

So ADD COMMENTS!
see what I did in my snippets, understand how it works, merge what makes sense, the repost your entire code!

Please use proper indentation! So the code is easier to read! Notice how I blocked off system calls to isolate those mini subblocks of code.

This post has been edited by wildgoose: 07 August 2009 - 07:45 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1