使用stl beq bne在MIPS中进行While循环

时间:2018-12-10 17:35:31

标签: while-loop mips fibonacci

需要一个带有while循环的MIPS程序来控制斐波那契数字的输出,它曾经只打印出前15个数字,但是现在我需要在它达到大于50(或任何其他数字)之前停止它)。

我尝试执行以下操作:(f

从书中即时阅读:
slt $ s1,$ s2,$ s3->如果($ s2 <$ s3)$ s1 = 1;否则$ s1 = 0 因此我的代码的WHILE部分对我来说很有意义,但是由于某种原因,它仍然会打印出前15个,而不是在它达到小于50的数字时停止输出。

.text
la  $s0, HOW_MANY
lw  $s0, 0($s0)         # s0: 15 : how many Fibonacci to print

addi  $t0, $zero, 0     # t0: which Fibonacci to print
addi  $t1, $zero, 0     # t1: the current Fibonacci
addi  $t2, $zero, 1     # t2: the next Fibonacci
addi  $t4, $zero, 50    # fMax = 50

WHILE:
slt $s1, $t1, $t4       # if($t1 < $s0) $s1 = 1 else $s1 = 0  if(f < fMax)
beq $s1, 1, NEXT_FIBO   # if $s1 == 1 then goto NEXT_FIBO
bne $s1, 1, DONE        # if $s1 != 1 then goto DONE

NEXT_FIBO:
addi $t0, $t0, 1        # t0++

addi $v0, $zero, 1      # syscall code to print integer
add $a0, $zero, $t1     # t1 to be printed
syscall                 # print t1

beq $t0, $s0, DONE      # goto DONE if t0==s0


add $t3,  $t1, $t2      # add variable = 1st number + 2nd number
move $t1, $t2           # 1st number =  2nd number
move $t2, $t3           # 2nd number = variable


addi  $v0, $zero, 4     # syscall code to print a string
la  $a0, COMMA_FOR_SEPARATION
syscall                 # print a comma (and a space)

j NEXT_FIBO             # go to the NEXT_FIBO label

DONE:
addi $v0, $zero, 10     # syscall code to exit
  syscall                 # exit

.data
COMMA_FOR_SEPARATION:  .asciiz ", " # comma (and space) string
HOW_MANY:   .word   15    # how many Fibonacci numbers to print

0 个答案:

没有答案