Search code examples
loopsmipsinfinite

Need help in getting out of infinite loop in a mips code


I was trying to code Floyd's triangle in MIPS and for some reason, I cannot seem to figure out the problem but since my QTspim is crashing while I am running the code therefore I am guessing it is stuck in an infinite loop. I need help in getting out of the infinte loop. This is the part of code with the loops and everything :

main:

li $v0, 4
la $a0, prompt  #Enter number of rows
syscall

li $v0, 5
syscall
move $t1, $v0  #rows

li $t2 , 1 #i
li $t3, 1 #j
li $t4 , 1 #number 

loop1:
    beq $t2, $t1 , exit
    li $t3 , 1
    
loop2:
    beq $t3, $t2 , newline
    li $v0 , 1
    move $a0, $t4
    syscall
    addi $t4 , $t4 , 1 #number++
    j loop2

newline:
    li $v0 , 4
    la $a0 , nline
    syscall
    addi $t2 , $t2 , 1
    j loop1

Solution

  • It seems like you are not incrementing the inner loop i.e. loop 2 anywhere and therefore its having issues and is getting stuck in an infinite loop. Apart from that it looks fine. Here is my code: main:

          li $v0, 4
          la $a0, prompt             
          syscall
    
          li $v0, 5                  #read number from console
          syscall
          move $t1, $v0
    

    li $t2 , -1 #i li $t3, -1 #j li $t4 , 1 #number

    loop1: beq $t2, $t1 , exit li $t3 , -1

    loop2: beq $t3, $t2 , newline li $v0 , 1 move $a0, $t4 syscall addi $t4 , $t4 , 1 #number++ addi $t3 , $t3 , 1 #j++ j loop2

    newline: li $v0 , 4 la $a0 , nline syscall addi $t2 , $t2 , 1 j loop1