I just got a question about the assembly program for Fibonacci sequence. The question is as following :
The Fibonacci sequence F is defined as F(1) = F(2) = 1 and for n ≥ 2,
F(n + 1) = F(n) + F(n − 1)
i.e., the (n + 1)th value is given by the sum of the nth value and the (n − 1)th value.
- Write an assembly program typical of RISC machines for computing the
kthvalueF(k), wherekis a natural number greater than2loaded from a memory locationM, and storing the result at memory locationM.
I received the answer of following:
LOAD r2, M
LOAD r0, #1
LOAD r1, #1
4: SUB r2, r2, #1
ADD r3, r0, r1
LOAD r0, r1
LOAD r1, r3
BNE 4, r2, #2 // jump to instruction 4 if r2 is not equal to 2
STOR M, r1
where # indicates immediate addressing and BNE stands for "branch if not equal".
I do not understand why... Can anyone please explain it to me?