Question: Below is C code for a fibonacci sequence and it translated into assembly. Can you explain line by line how each was translated from C

Below is C code for a fibonacci sequence and it translated into assembly. Can you explain line by line how each was translated from C to assembly? Thanks
C code:
long fibonacci_gd_goto(long n)
{
long i=2;
long next, first =0, second =1;
if (n1)
goto done
loop:
next = first + second;
first = second; second = next;
i++;
if (in)
goto loop;
done:
return n;
}
Assembly Code:
fibonacci:
pushq %rbp
movq %rsp,%rbp
movq %rdi, -40(%rbp)
movq $2,-8(%rbp)
movq $0,-16(%rbp)
movq $1,-24(%rbp)
cmpq $1,-40(%rbp)
jle .L5
nop
.L2:
movq -16(%rbp),%rdx
movq -24(%rbp),%rax
addq %rdx,%rax
movq %rax, -32(%rbp)
movq -24(%rbp),%rax
movq %rax, -16(%rbp)
movq -32(%rbp),%rax
movq %rax, -24(%rbp)
addq $1,-8(%rbp)
movq -8(%rbp),%rax
cmpq -40(%rbp),%rax
jg .L6
jmp .L2
.L5:
nop
jmp .L3
.L6:
nop
.L3:
movq -40(%rbp),%rax
popq %rbp
ret

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!