我正在尝试创建一个简单的汇编代码,它接受输入 N 并返回第 N 个斐波那契数(例如,如果您输入 2,它应该输出 1,如果输入 3,它应该输出 2)。我的代码没有抛出任何错误,但是在你输入一个数字后,它只会输出一个奇怪的小数。
我真的是组装新手,感谢您的帮助!
.data
introText: .asciiz "Type a positive integer, please! \n"
input: .word 0
.text
# ask user for input
li $v0, 4
la $a0, introText
syscall
# read input int
li $v0, 5
syscall
# store input
addi $s1, $v0, 0
syscall
# main loop
li $s2, 0 # s2 starts at 0 and will increase until it's equal to $s1, the player input
li $s3, 0 # this will hold the most recent fib number
li $s4, 0 # this will hold the second most recent fib number
loop:
addi $s2, $s2, 1 # increment s2 for loop
add $s5, $s3, $s4 # make the current result the sum of the last two fib numbers
bne $s5, $zero, notequalzero # if the current result is 0, make it one, otherwise continue
addi $s5, $zero, 1
notequalzero:
addi, $s4, $s3, 0 # make the most recent fib number equal to the current fib number
addi, $s3, $s5, 0 # make the second most recent fib number equal to the most recent fib number
bne $s2, $s1, loop
# return the answer
li $v0, 1
addi $a0, $s5, 0
syscall
# end program
li $v0, 10
syscall