【问题标题】:MIPS: Infinite loop with branchesMIPS:带分支的无限循环
【发布时间】:2021-12-02 09:54:35
【问题描述】:

所以我有一个程序,它接受用户的输入(大于 0 的整数)并将其下方的所有偶数相加以获得返回答案(例如:输入:7;答案:2 + 4 + 6 = 12) .

这个程序的问题在于,它的目的是根据我的“活动偶数变量”($t1) > 输入来跳出循环。尽管我的程序似乎永远不会正确解释分支并无限期地循环直到 $t1 溢出(我检查了调试器并且知道程序确实每次都运行分支行)。以下是我的代码:

    .data   
    
N:       .word 0
Result:  .word 0

    .text
    
    
    .globl main
initialize:
    li $v0, 5      #getting arg1 from user
    syscall
    la $t0, N
    sw $v0, 0($t0)
    
    li $t1, 2
    li $t2, 0
main:                    
    blt $t0, $t1, fin2
fori:
    add $t2, $t2, $t1 #t2 += t1
    add $t1, $t1, 2   #t1 += 2
    
    slt $t5, $t1, $t0
    bne $t5, $zero, fori
fin:
    
    
    li $v0,1              #prints return value
    move $a0, $t2
    syscall
    
    li  $v0, 10
    syscall

fin2:
    
    
    li $v0,1              #prints return value
    move $a0, $zero
    syscall
    
    li  $v0, 10
    syscall

【问题讨论】:

    标签: mips infinite-loop mars-simulator


    【解决方案1】:

    所以我不知道您是否需要使用文字存储等,但实际上您这样做只是过于复杂了。你所需要的只是一个简单的循环,它有一个递增 2 的计数器,检查它是否大于初始值,然后将该整体值添加到结果中

    .text    
    .globl main
    
    initialize: 
        li $v0, 5           # Getting arg1 from user
        syscall             # System call
        move $t0, $v0       # Store the input value in $t0
            
        li $t1, 0           # Initializing the result register
        li $t2, 0           # Initializing the addition/counter register
    main:                    
    
    loop:
        add $t2, $t2, 2     # Increase the value to be added by 2 (next even value)
        bge $t2, $t0, fin   # Check if the increment is larger than or equal to the initial input, if so break to finish
        add $t1, $t1, $t2   # Increment the result by adding the even value
        j loop              # jump bak to the top of the loop
    
    fin:
        li $v0,1            # let the system know an integer is going to be printed
        move $a0, $t1       # Load the result into the $a0 register (the register that prints values)
        syscall             # System Call
    
        li  $v0, 10         # Let the system know the program is going to exit
        syscall             # System Call
    

    所以你可以看到$t2 每次递增 2。每次递增后,都会将其与输入值进行比较。如果输入 ($t0) 大于 $t2,则将 $t2 的值添加到结果 ($t1)。这样就有一个增量计数器,它也用于将必要的偶数值添加到结果中。


    编辑:

    不确定这是否完全是你的意思,但我只是在一些加载和保存中折腾,使用 s 寄存器,因为这些是在保存值时应该使用的寄存器。

    .data
    
    N:      .word 0
    Result: .word 0
    
    .text    
    
    .globl main
    
    initialize:
        li $v0, 5           # Getting arg1 from user
        syscall             # System Call
        la $s0, N           # Load the address of N into $s0
        sw $v0, 0($s0)      # Store the input value in 0 index of N
                    
        li $t2, 0           # Initializing the addition/counter register
    
        la $s1, Result      # Load the address of Result into $s1
    main:                    
        sw $t2, 0($s1)      # Setting the 0 index of Result to 0
    loop:
        add $t2, $t2, 2     # Increase the value to be added by 2 (next even value)
        lw  $t4, 0($s0)     # Loading the input value into the $t4 register
        bge $t2, $t4, fin   # Check if the increment is larger than or equal to the initial input, if so break to finish
        lw  $t4, 0($s1)     # Loading the current result into the $t4 register
        add $t4, $t4, $t2   # Increment the result by adding the even value
        sw $t4, 0($s1)      # Saving the new current result into the $t4 register
        j loop              # jump bak to the top of the loop
    
    fin:
        li $v0,1            # let the system know an integer is going to be printed
        lw $a0, 0($s1)      # Load the result into the $a0 register (the register that prints values)
        syscall             # System Call
    
        li  $v0, 10         # Let the system know the program is going to exit
        syscall             # System Call
    

    【讨论】:

    • 感谢 FivePly,重新设计的循环似乎可以解决问题。坦率地说,我需要变量,因为它们是我的程序的要求(加载/保存到 N 和结果),但是您的解决方案摆脱了我遇到的无限循环问题。
    • @CalebErnst 很高兴 :) 但您应该将 N 设置为用户输入并将结果设置为最终结果或定期更新?
    • 无需定期更新,只需获取最终答案并将其存储在“结果”中,N 将保持不变并在初始化时保存回来。但我自己能够做到这一点非常好。
    猜你喜欢
    • 2022-01-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-29
    • 1970-01-01
    • 2020-10-31
    相关资源
    最近更新 更多