8051 Program to find 10 fibonacci numbers and store it an array.
Program to find 10 fibonacci numbers and store it an array.
In this article let's learn how to find fibonacci numbers and store it in an array. Basically fibonacci number is a series where each term is the sum of previous two numbers.
Algorithm:
1)Store the memmory address in R0 and counter in R3.
2)Store first two numbers that is 0 and 1 in memory space.
3)Add the previous two numbers and store in an memory space.
4)Exchange the registers and store the previous two numbers.
5)Repeat the steps 2,3 and 4 till the counter value becomes zero.
Code goes here:
ORG 0000h
LJMP MAIN
ORG 40h
MAIN: MOV R0,#40H ;Memory space where all the fibonacci numbers are stored
MOV R3,#8 ; Move number of fibonacci numbers to be generated
MOV R1,#00H
MOV @R0,#0H ;Move the first number in the memory space
INC R0
MOV @R0,#01H ;Move the second number in the memory space
MOV R2,#01H
LABEL2:INC R0
MOV A,R1
ADD A,R2 ; add the previous two numbers
MOV @R0,A ;store the fibonacci number in the memory space
MOV B,R2 ;exchange n store the preceding two numbers in r1
MOV R1,B
MOV R2,A
DJNZ R3,LABEL2 ;keep the track of counter
END
No comments: