In this article let’s learn how to find the first 10 Fibonacci numbers.Usually Fibonacci numbers start from 0,1,1,2……In this, we can observe that the present number in Fibonacci is the summation of the previous two numbers. So in this, we should initialize the first two numbers as -1 and +1 so that our algorithm works fine.
Let’s see how the algorithm works.
2)Initialize R0 with -1, R1 with +1, counter R4 with 10 and first memory location with R2
3) Add R0 and R1
4)Store the result in the memory address
5)Increment the pointer
6)Change R0 and R1 i.e store the added result in R1 and store R1 in R0 by taking temporary variable R3
7)Decrement the pointer and compare.
8)If the counter is less then repeat step 3,4,5,6,7,8
Code goes here:
mov r0,#00 ; first two fibonacci numbers
sub r0,r0,#01 ; assigning -1 to first register
mov r4,#10 ;No of fibonacci numbers to generate
ldr r2,=0x40000000;address to store fibonacci numbers
back add r0,r1 ;adding the previous two numbers
str r0,[r2] ; storing the number in a memory
add r2,#04 ;incrementing the address
sub r4,#01 ;decrementing the counter
cmp r4,#00 ;comparing the counter to zero
bne back ;looping back
Thank you visit again 🙂 You can also visit our 8051 code to find Fibonacci numbers. Click Here!