1

so I've been given an assignment in which I'm supposed to find the index of the max and min values in a user-created 10 digit array in MIPS assembly language. I've been able to find the max value but I'm having trouble finding the min value. I figured it should be a simple solution once I find the max value but I was wrong.

Here is what i have to find the max value:

deleteMax:
# $s7 is the register where the address of the array is stored
# $s0 is the register where the array size is stored

# Clear out unimportant registers
addi $s1, $zero, 0
addi $t0, $zero, 0
addi $s3, $zero, 0
addi $s4, $zero, 0
addi $t6, $zero, 0
addi $t7, $zero, 0

addi $t0, $zero, 0               # Address of first element in bytes
li $t3, -1                       # Max index
li $s2, 0                        # Max value
li $t1, 0                        # Counter


loop:
sll $s1, $t1, 2
add $s1, $s1, $s7
lw $s3, 0($s1)                   # Load the first element into $s3
li $v0, 1
move $a0, $s3
syscall


slt $t2, $s3, $s2      # If $s3 < $s2, $t2 = 1, if $s3 >= $s2, $t2 = 0
bne $t2, $zero, find_max
ori $s2, $s3, 0            # Updates the maximum value
ori $t3, $t1, 0            # Updates the maximum value index

find_max:
addi $t1, $t1,1           # Increase the counter
bne $t1, $s0, loop        # if the counter hasn't reached the end, go 
                          back to the loop
j print_max

This does what I want: Find the max and it's index. However when I try switching these 2 lines of codes:

slt $t2, $s3, $s2      # If $s3 < $s2, $t2 = 1, if $s3 >= $s2, $t2 = 0
bne $t2, $zero, find_max

to lets say:

slt $t2, $s3, $s2      # If $s3 < $s2, $t2 = 1, if $s3 >= $s2, $t2 = 0
bne $t2, 1, find_max

The min value comes out to 0?

I'm not sure why this is happening and unsure what I can do for my current situation to find the min value.

I hope I've explained myself and my code + comments have explained the situation enough. Im more than happy to answer any more confusion you guys may have.

  • _"The min value comes out to 0?"_ From the code you've posted it's impossible to tell whether this simply is because the minimum value _is_ 0. To analyze the runtime behavior of your code, use the simulator's single-step feature (usually the F10 key). – Michael Oct 15 '18 at 07:49
  • Do you think it’s because I’m loading my first min value as 0? – John Thompson Oct 15 '18 at 15:10
  • Figured it out: I was storing 0 as my min value, oh my goodness – John Thompson Oct 15 '18 at 17:02
  • The saved registers (s0-s7) are important, and must not be overwritten carelessly. In order to use them, they must be saved in the stack first, and then their original value be restored before the function returns. That way, when you call a subroutine via a jal/jalr instruction, you are certain any data placed into s0-s7 is left intact after the subroutine returned. If you don't need data to be kept across the subroutine calls, you can use temporary registers (t0-t9), function arguments registers (a0-a3) or function return registers (v0-v1). – Linblow Oct 19 '18 at 17:18

0 Answers0