JX30
JX30

Reputation: 77

My mips code won't print out the max and min

My mips code compiles and runs but the problem is it doesn't output what I want it to. The code is supposed to find the max and min of an array of 8 integers. Can someone please take a look thanks.

.data
    X: .word 1, 2, 3, 4, 5, 6, 7, 8
    Max: .asciiz "Max: "
    Min: .asciiz "Min: "
    Space: .asciiz " "
.text
.globl main
main:
    la $t0, X
    lw $s0, 0($t0)  #Sets Max to first value in array
    lw $s1, 0($t0)  #Sets Min to first value in array
    addi $t4, $0, 0 #Sets the counter to 0
li $t1, 0   #Index for the array
    lw $t2, X($t1)
lw $t3, X($t1)

loop: 
    bge $t0, 8, EndLoop
    bgt $t2, $s0, SetMax
    blt $t3, $s1, SetMin
    addi $t1, $t1, 4
    addi $t0, $t0, 1
    j loop


SetMax:
    move $s0, $t2
    j loop
SetMin: 
    move $s1, $t3
    j loop
EndLoop:
    li $v0 4
    la $a0 Max
    syscall

    li $v0 1
    la $a0 ($s0)
    syscall

    li $v0 4
    la $a0 Space
    syscall

    li $v0 4
    la $a0 Min
    syscall

    li $v0 1
    la $a0 ($s1)
    syscall

    li $v0 10
    syscall

The code prints out a max of 1 and a min of 1. Please help, thanks.

Upvotes: 0

Views: 368

Answers (1)

JX30
JX30

Reputation: 77

I figured it out. I realized that I was not using the correct register as the counter. Here's the updated code.

.data
    X: .word 1, 2, 3, 4, 5, 6, 7, 8
    Max: .asciiz "Max: "
    Min: .asciiz "Min: "
    Space: .asciiz " "
.text
.globl main
main:
    la $t0, X
    lw $s0, 0($t0)  #Sets Max to first value in array
    lw $s1, 0($t0)  #Sets Min to first value in array
    addi $t4, $0, 0       #Sets the counter to 0
    li $t1, 0   #Index for the array


loop: 
    bge $t4, 8, EndLoop
    lw $t2, X($t1)
    bgt $t2, $s0, SetMax
    blt $t2, $s1, SetMin
cont:
    addi $t1, $t1, 4
    addi $t4, $t4, 1
    j loop


SetMax:
    move $s0, $t2
    j cont
SetMin: 
    move $s1, $t2
    j cont
EndLoop:
    li $v0 4
    la $a0 Max
    syscall

    li $v0 1
    la $a0 ($s0)
    syscall

    li $v0 4
    la $a0 Space
    syscall

    li $v0 4
    la $a0 Min
    syscall

    li $v0 1
    la $a0 ($s1)
    syscall

    li $v0 10
    syscall

Yeah it works, thanks to Gerardo I believe his name was for the idea of using cont. But in the end I believe that using $t4 as the counter was the real solution.

Upvotes: 1

Related Questions