mips语言递归实现斐波那契数列

用mips汇编语言实现斐波那契数列。
汇编环境为Mars

#Pseudocode#########################################################
#Fibnacci array:
#Fib(){
     
#if(n==1) return 1
#else 
#	return Fib(n-1)+Fib(n-2)
#}
#####################################################################
#cross variable
#$t1 -- array address
#$t2 -- initial value n,store recursion depth
#$t3 -- const n
######################################################################
.data
array: 	.space 1024
hello:	.asciiz "Input a number: \n"
bye:	.asciiz "\nSayonala\n"
.text	
	li $v0,4
	la $a0,hello
	syscall 
	li $v0,5
	syscall
	move $t2,$v0  
	move $t3,$t2
	li $t0,1		#set array(0) and array(1) = 1
	la $t1,array
	sw $t0,($t1)
	sw $t0,4($t1)
	blt $t2,3,print	#if n<3 ,just print,needless of recursion
	
Fib:	addi $sp,$sp,-8
	sw $ra,0($sp)
	sw $t2,4($sp)
	#base case
	beq $t2,2,re	#when $t2=2 ,we're gonna return
	addi $t2,$t2,-1
	jal Fib
	# Fib(n)=Fib(n-1)+Fib(n-2)
	move $t4,$0
	lw $a0,($t1)
	lw $a1,4($t1)
	add $t4,$a1,$a0
	sw $t4,8($t1)
	
	addi $t1,$t1,4
	beq $t2,$t3,print
	
re:	lw $ra,0($sp)
	addi $t2,$t2,1
	addi $sp,$sp,8
	jr $ra		#we're gonna do from line 39 

print:	la $t1,array
	li $t3,1
loop1:	lw $a0,($t1)
	li $v0,1
	syscall
	
	li $v0,11
	li $a0,' '
	syscall
	
	beq $t3,$t2,end
	addi $t3,$t3,1
	addi $t1,$t1,4
	b loop1

end:	li $v0,4  
	la $a0,bye
	syscall
	li $v0,10
	syscall
	

你可能感兴趣的:(mips汇编,mips)