Each number in the Fibonacci series is the sum of the previo
Each number in the Fibonacci series is the sum of the previous two numbers. Table 6.16 lists the first few numbers in the series, fih[n). Convert the high-level function of pan into MIPS assembly code. Add comments after every line of code that explain clearly what it does. Use the SPLM simulator to test your code on fib (See the Preface for how- to install the SPIM simulator.) But use MARS (not Spim) to test the code on fib(9). Here is the solution to 6.22(b), which you will need. Note I am NOT using a loop as they say, because I would like you to translate a function that involves recursion (even though it is slower). You will need to translate BOTH recursive calls. int fib(int n) {if (n == 1) return 1; else if (n == 2) return 1; else return fib(n-l) + fib(n-2);}
Solution
Given C Program :-
int fib(int n)
{
if(n==1)
return 1;
else if(n==2)
return 1;
else
return fib(n-1)+fib(n-2);
}
MIPS Assembler Code :-
$Ltext0:
$LFB0 = .
fib(int):
addiu $sp,$sp,-40
sw $31,36($sp)
sw $fp,32($sp)
sw $16,28($sp)
move $fp,$sp
sw $4,40($fp)
lw $3,40($fp)
li $2,1 # 0x1
bne $3,$2,$L2
nop
li $2,1 # 0x1
b $L3
nop
$L2:
lw $3,40($fp)
li $2,2 # 0x2
bne $3,$2,$L4
nop
li $2,1 # 0x1
b $L3
nop
$L4:
lw $2,40($fp)
addiu $2,$2,-1
move $4,$2
jal fib(int)
nop
move $16,$2
lw $2,40($fp)
addiu $2,$2,-2
move $4,$2
jal fib(int)
nop
addu $2,$16,$2
$L3:
move $sp,$fp
lw $31,36($sp)
lw $fp,32($sp)
lw $16,28($sp)
addiu $sp,$sp,40
j $31
nop
$LFE0:
$Letext0:
$Ldebug_info0:
$Ldebug_abbrev0:
$Ldebug_line0:
$LASF2:
$LASF1:
$LASF0:

