MIPS:带有分支的无限循环



所以我有一个程序,从用户(0以上的整数)接受输入,并将其以下的所有偶数相加以获得返回答案(例如:input: 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

所以我不知道你是否需要使用单词存储之类的东西,但你这样做确实使它过于复杂了。你所需要的只是一个简单的循环,它有一个计数器,增量为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

相关内容

  • 没有找到相关文章

最新更新