简体   繁体   中英

MIPS stuck in infinite loop

As part of my uni report it wants me to edit some MIPS code and put repeating code in a subroutine, however, whenever I call my subroutine it gets stuck in an infinite loop repeating the entire subroutine. The program is suppose to take 4 inputs from the user and add them together and display the total which works without the subroutine, I'm not sure if I'm missing something from my code or if I've just written it wrong? Thanks for any help: :]

.data

enterMsg1: .asciiz "Please enter the last four digits of your student id \n"
enterMsg2: .asciiz "Press enter between each digit \n"
enterMsg3: .asciiz "Enter next digit \n"
totalMsg1: .asciiz "The total of the digits is: "
    
.text

# output the initial instruction text to the console
addi $v0, $zero, 4
la $a0, enterMsg1
syscall

# output the second instruction text to the console
addi $v0, $zero, 4
la $a0, enterMsg2
syscall

# read an integer from keyboard input and store the input in $s0 for the total
addi $v0, $zero, 5
syscall
add $s0, $zero, $v0

jal NextDigit
jal NextDigit
jal NextDigit

# output the text asking for the next digit to the console
# then receive the input, add to total ($s0)
NextDigit:
addi $v0, $zero, 4
la $a0, enterMsg3
syscall

addi $v0, $zero, 5
syscall
add $s0, $s0, $v0
jr $ra


# output the total instruction text to the console
addi $v0, $zero, 4
la $a0, totalMsg1
syscall


add $a0, $s0, $zero
addi $v0, $zero, 1
syscall

addi $v0, $zero, 10
syscall ```

You have embedded your NextDigit function inside main , and that is bad.

So, from main that function will be called 3 times, and it will return to main 3 times, but then what happens after that?

Since the function is embedded within the main then main accidentally drops into the embedded function, but this time it hasn't been called properly. So, when it tries to return it will go back to the the return address associated with the last proper invocation of the function (the return address for the third jal).

The most common solution and efficient solution is to separate the two functions entirely. Put all the code for NextDigit fully after all the code of main .

(Alternately, you could make your main jump around the embedded function.)

The labels do not execute; they are removed by the assembler and do not appear in the machine code, so, the processor never sees them. The only thing the processor sees is machine code instructions, it skips right over labels in you're source code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM