我正在尝试编写一个程序,允许用户 find 找出第 n 个斐波那契数列。然而,我是 MIPS 的新手,我似乎无法让它工作。这让我很困惑。
.data
msg: .asciiz "Please enter a number to calculate the fibonnaci sequence for"
equalsOne: .asciiz "The answer is 1"
equalsTwo: .asciiz "The answer is 2"
answer: .asciiz "The answer is "
.text
addi $v0, $zero, 4 #Set $v0 to 4 (Print String)
la $a0, msg #Set memory address of msg to $a0
syscall
addi $v0, $zero, 5 #Set $v0 to 5 (Read Integer)
syscall
add $t0, $zero, $v0 #Store input integer in $t0
beq $t0, 1, equals1 #If input integer == 1, go to equals1
beq $t0, 2, equals2 #If input integer == 2, go to equals2
addi $t1, $zero, 1 #Stores 1 in register $t1
addi $a1, $zero, 1 # $a1 = currentNumber
addi $a2, $zero, 1 # $a2 = oldNumber
fib: slt $t2, $a1, $t0 #While $a1 < $t0
bne $t2, 1, exit
add $a3, $a1, $a2 # $a3 = nextNumber = currentNumber + oldNumber
add $a2, $a1, $zero #Set oldNumber = currentNumber
add $a1, $a3, $zero #Set currentNumber = nextNumber
j fib
equals1: la $a0, equalsOne
addi $v0, $zero, 4
syscall
equals2: la $a0, equalsTwo
addi $v0, $zero, 4
syscall
exit: addi $v0, $zero, 1 #Print integer
add $a1, $a1, $zero #Print nextNumber
syscall