MIPS:带分支的无限循环
MIPS: Infinite loop with branches
所以我有一个程序接受用户的输入(大于 0 的整数)并将其下方的所有偶数相加以获得 return 答案(例如:输入:7;答案:2 + 4 + 6 = 12).
这个程序的问题在于,它的目的是根据 if my 'active even variable' ($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
所以我有一个程序接受用户的输入(大于 0 的整数)并将其下方的所有偶数相加以获得 return 答案(例如:输入:7;答案:2 + 4 + 6 = 12).
这个程序的问题在于,它的目的是根据 if my 'active even variable' ($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