Im trying to make a basic while loop example im preperation for my next assignment and im stuck in an infinite loop. $t3 never reaches to 3, or its not detecting that its at 3. What am i doing wrong here? Thanks!
.data #data segment
msg1:.asciiz "please enter a number to convert to ASCI: "
nl:.asciiz "\n"
msg2:.asciiz "done! "
.text # Code segment
.globl main # declare main to be global
main:
la $a0,msg1 # $a0 := address of message 1
li $v0,4 # system call, type 4, print an string
syscall
li $t3,0 #initial value of $t3
li $v0,5
syscall #read an int
loop:
la $a0,msg1 # $a0 := address of message 1
li $v0,4 # system call, type 4, print an string
syscall
addi $t3,$t3,1
beq $3,$t3,Exit # branch to the label Exit if $t3=3
j loop # branch unconditionally to loop ($t3 != 0)
Exit:
li $v0,10 # System call, type 10, standard exit
syscall
The problem is here:
$3is a register; it is not the value 3. It’s referring to the contents of the$3register, which is actually$v1, which holds a value of 0 because you haven’t put anything in there. So instead of comparing to a value of 3, you’re comparing to 0. (It’s not actually an infinite loop, since$t3will eventually wrap around to 0, but you get the idea.)MIPS lacks an instruction for comparing with an immediate, so you need to load the value 3 into a register first.
Add this line before the loop, because you only need to load the value once:
And change your compare to this:
This will get you out of the loop. Your program will then print msg1 three times and exit, which I suspect isn’t what you want, but hopefully this lets you continue finishing up.