嵌套函数MIPS代码中的无限循环

时间:2018-10-06 00:15:49

标签: mips

我对这部分MIPS代码有疑问。 它导致无限循环并导致模拟崩溃。 该代码的目的是创建一个嵌套函数以查找前n个平方的和。例如:如果输入4,则结果为30。

我相信无限循环正在发生第一个函数调用(sumSquares)。函数squares已经过独立测试,因此我相信它可以工作。

我希望有人能帮助我!

.data
    prompt:  .asciiz "Number to square: "
    prompt2: .asciiz "Number of squares to sum: "
    return:  .asciiz "Square: "
    return2: .asciiz "Sum of the first n sqaures"
    test:       .asciiz "Testing location"
.text
.globl main

main: 
    # Print the string asking of "Number of squares to sum: "
    li $v0, 4
    la $a0, prompt2
    syscall

    # capture user input
    li $v0, 5
    syscall

    # store the value in a register
    move $s0, $v0
    move $a0, $s0

    # store the function 'square' is $ra
    jal sumSquare

    # Exit
    li $v0, 10
    syscall

sumSquare:
    # allocate space on the stack 
    addi $sp $sp, -8
    sw   $ra, 4($sp)
    sw   $a1, 0($sp)

    # store the passed in argument in a register
    move $t3, $a0           # store user's input

    # initialize temp registers to be used
    add $t4, $t4, $0        # $t4 is used to store the sum
    add $t5, $t5, $0        # $t5 = 0; used for counter

    Loop2: beq $t5, $t3, Exit2
        move $a1, $t3
        jal  square
        add  $t4, $t4, $v0
        addi $t5, $t5, 1
        j Loop2

    Exit2:
    # Print the string asking of "Number of squares to sum: "
    li $v0, 1
    move $a0, $t4
    syscall

    # transfer control back to the caller -> main
    jr $ra

square:
    # store the passed in argument in a register
    move $t0, $a1           # store user's input

    # initialize variables to use in the loop
    add $t1, $t1, $0        # $t1 is used to store the sum
    add $t2, $t2, $t0       # $t2 = $t0; used for counter

    Loop: beq $t2, $0, Exit
        add  $t1, $t1, $t0
        addi $t2, $t2, -1
        j Loop
    Exit:

    # print the value back out
    li $v0, 1
    move $a0, $t1
    syscall

    # restore 
    lw $s0, 4($sp)
    lw $ra, 8($sp)
    addi $sp, $sp, 12


    # transfer control back to the caller  -> sumSquares
jr $ra

1 个答案:

答案 0 :(得分:0)

jr $ra 不会改变寄存器的值,所以它永远不会返回主,因此进入无限循环。而是使用 syscall 函数来终止。

.data
    prompt:  .asciiz "Number to square: "
    prompt2: .asciiz "Number of squares to sum: "
    return:  .asciiz "Square: "
    return2: .asciiz "Sum of the first n sqaures"
    test:       .asciiz "Testing location"
.text
.globl main

main: 
    # Print the string asking of "Number of squares to sum: "
    li $v0, 4
    la $a0, prompt2
    syscall

    # capture user input
    li $v0, 5
    syscall

    # store the value in a register
    move $s0, $v0
    move $a0, $s0

    # store the function 'square' is $ra
    jal sumSquare

    # Exit
    li $v0, 10
    syscall

sumSquare:
    # allocate space on the stack 
    addi $sp $sp, -8
    sw   $ra, 4($sp)
    sw   $a1, 0($sp)

    # store the passed in argument in a register
    move $t3, $a0           # store user's input

    # initialize temp registers to be used
    add $t4, $t4, $0        # $t4 is used to store the sum
    add $t5, $t5, $0        # $t5 = 0; used for counter

    Loop2: beq $t5, $t3, Exit2
        move $a1, $t3
        jal  square
        add  $t4, $t4, $v0
        addi $t5, $t5, 1
        j Loop2

    Exit2:
    # Print the string asking of "Number of squares to sum: "
    li $v0, 1
    move $a0, $t4
    syscall

    
   
li $v0, 10 # terminate
syscall 

square:
    # store the passed in argument in a register
    move $t0, $a1           # store user's input

    # initialize variables to use in the loop
    add $t1, $t1, $0        # $t1 is used to store the sum
    add $t2, $t2, $t0       # $t2 = $t0; used for counter

    Loop: beq $t2, $0, Exit
        add  $t1, $t1, $t0
        addi $t2, $t2, -1
        j Loop
    Exit:

    # print the value back out
    li $v0, 1
    move $a0, $t1
    syscall

    # restore 
    lw $s0, 4($sp)
    lw $ra, 8($sp)
    addi $sp, $sp, 12


   

li $v0, 10 # terminate
syscall