MIPS汇编:打印Fibonacci序列(带系统调用)

时间:2015-11-13 20:08:47

标签: linux assembly mips system-calls fibonacci

下午好!我有一个模拟Fibonacci序列的 MIPS指令程序。我的程序通过输入数字(n)来工作,该数字将决定程序将完成多少次迭代。该程序吐出正确的结果,但我想知道如何输出Fib方法的每个数字序列。首先是我的代码:

.data
msg1:.asciiz "Give a number: " # message for fib(n)
.text

main:

li $v0,4 #read string
la $a0,msg1 # set iterations to msg1 value
syscall 
li $v0,5 # read an int
syscall 
add $a0,$v0,$zero #move to $a0

jal fib #call fib

add $a0,$v0,$zero # add result into argument
li $v0,1 # output integer
syscall

li $v0,10
syscall

fib:
#a0=y
#if (y==0) return 0;
#if (y==1) return 1;
#return( fib(y-1)+fib(y-2) );

addi $sp,$sp,-12 #save in stack
sw $ra,0($sp) # save return adress to stack
sw $s0,4($sp)# save msg value
sw $s1,8($sp)# save fib(y-1)
add $s0,$a0,$zero # store msg value into s0
addi $t1,$zero,1 # comparable value

beq $s0,$zero,return0 # if s0 == 0 jump to return 0 block
beq $s0,$t1,return1# if s0 == t1 (1) jump to return 1 block
addi $a0,$s0,-1 # else decrement initial value by 1

jal fib
add $s1,$zero,$v0     #s1=fib(y-1)############################syscall needed#########################################
addi $a0,$s0,-2 # subtract two

jal fib               #v0=fib(n-2)
add $v0,$v0,$s1       #v0=fib(n-2)+$s1 ##########################syscall needed#################################

exitfib:

lw $ra,0($sp)       #read return adress from stack
lw $s0,4($sp)       # read msg value from stack
lw $s1,8($sp)       # read fib(y-1) from stack
addi $sp,$sp,12       #bring back stack pointer
jr $ra

return1:
 li $v0,10 # return 1 to result

 j exitfib
return0 :     li $v0,0 # return 0 to result


 j exitfib

希望你看到了,但我有两行评论,我相信每个序列(syscall)($s1 = fib(y-1))输出每个数字需要$v0 = fib(n-2) - $s1。我尝试将正确的代码加载到结果寄存器中以输出integer,但它仍然给我带来了问题(在最终结果之前没有显示过程)。我如何正确实现syscall以便我可以打印fib方法在达到最终结果之前计算的每个数字?

0 个答案:

没有答案