简单的MIPS组装 - 返回fibbonacci数

时间:2017-03-24 18:42:41

标签: assembly mips mars

我正在尝试创建一个简单的汇编代码,它接受一个输入N并返回第N个fibbonacci数字(例如,如果你输入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

0 个答案:

没有答案