0

So I'm new to MIPS and I'm trying to build a program in Assembly MIPS which finds the maximum value in array:

    .data 
    A: .word 11 100 3 5 8 13 1 16  #array of integers
    N: .word 8  # array length
    output_max: .asciiz "The max value is: "
    
    .text
    .globl main
    
main:
    la $a0 N
    la $a1 A
    
    jal max
    move $s0 $v0
    
    
    li $v0 4
    la $a0 output_max
    syscall
    
    li $v0 1
    move $a0 $s0
    syscall
    j exit
    
    
exit:
    li $v0 10
    syscall

max:
    
    move $t0 $a0  
    move $t1 $a1  
    lw $v0 0($t1)  
    li $t2 0  
    j maxLoop
    
maxLoop:
    addi $t2 $t2 1 
    beq $t2 $t0 maxEnd  
    addi $t1 $t1 4 
    lw $t3 0($t1)
    slt $t4 $v0 $t3 
    beq $t4 1 changeMax
    j maxLoop
    
changeMax:
    move $v0 $t3
    j maxLoop
        

maxEnd:
    jr $ra

The max function should return the maximum value in the input array. So after compiling, it goes into an infinite loop. I can't quite see where the problem is..

1
  • Did you try debugging it? Commented Jun 2, 2021 at 19:06

1 Answer 1

1

The problem is you are running your loop using the address of N and not the value of N. The first line of main loads the address of N into $a0 which becomes $t0 in your max function. However, you then use that address as if its the value of N (ie, 8) in your loop with beq $t2 $t0 maxEnd.

Either just load the value directly into $a0 at the beginning:

main:
    li $a0 8 # Length of A

Or dereference the argument in max first:

max:
    lw $t0 0($a0) # Get value of N from pointer to N
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.