Assembly Pep8 question The Pep8 subroutine below is a transl
Assembly Pep/8 question
The Pep/8 subroutine below is a translation of the following C function int fib (int N) {If (N 1 || N 2) return 1; else return fib(N - 1)+ fib(N - 2);} Add appropriate comments to the three bold sectionsSolution
The c code provided is used to calculate fibonacci numbers. the fibonacci series is based on the numbers where the number is calculated based on the sum of the previous 2 numbers. the sequence will be
1,1,2,3,5,8,13........
So based on that the fibonacci numbers are generated.
The assembly code of the same c code is provided which is correct. The first part:
cpa 2,i
breq ret1
Here we are comparing the value of N which is the nth term in the fibonacci series. Here we are checking if N=2. Here N = i and if the value of that is 2 then we return from the function by giving 1.
The next part is for the stack call for fib(N-1)
Here lda 6,s we load the value from the stack. Then we subtract 1 from the value of N i.e N-1 using the instruction suba 1,i. Now the same is called on the stack and the call to the function is made.
The last part
lda 2,s
adda 6,s
sta 12,s
Here we are adding the 2 parts of the stacks fib (n-1) + fib (n-2). Here we add the return call of both the stacks and then call the function. the adda 6,s adds both the parts and sta store the updated value of the function cal on the stack.
