4/11/08

MIPS NON recursive Fibonacci

MIPS NON recursive Fibonacci

As you may seen I posted an implementation of Fibonacci in C(recursive and not) and Recursive Fibonacci MIPS.
You may also want to see All my MIPS examples.
Here is the NON recursive implementation of Fibonacci for MIPS.

.data
msg1:.asciiz "Give a number : "
.text
.globl main
main:

li $v0,4
la $a0,msg1
syscall
li $v0,5
syscall
add $a0,$v0,$zero

jal fib

add $a0,$v0,$zero
li $v0,1
syscall

li $v0,10
syscall

fib:
#a0=a
#if (a==0) return 0;
#if (a==1) return 1;

# int x($t1),y($t2),z($t3),i($t4);
# for (x=0,y=0,z=1,i=1;i<a;i++) {
# x=y+z;
# y=z;
# z=x; }

# return(x);

addi $t0,$zero,1

beqz $a0,return0
beq $a0,$t0,return1

#arxikopiisi

add $t1,$zero,$zero
add $t2,$zero,$zero
addi $t3,$zero,1
addi $t4,$zero,1

loop:
bge $t4,$a0,endloop
add $t1,$t2,$t3
add $t2,$zero,$t3
add $t3,$zero,$t1
addi $t4,$t4,1
j loop

endloop:
add $v0,$zero,$t1
jr $ra

return0:
add $v0,$zero,$zero
jr $ra

return1:
addi $v0,$zero,1
jr $ra

2 comments:

Anonymous said...

thx for ur help mate. continue the good work. good luck

Unknown said...

If I wanted to print all the numbers as an array, what should be done? If user input is 5, for instance, output would be 1,1,2,3,5