In this article let's learn how to find 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 previous two numbers.So in this we should initialize first two numbers as -1 and +1 so that our algorithm works fine.

Let's see how the algorithm works.

### Algorithm:

1)Start
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 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
9)Stop

### Code goes here:

``````
area ascen,code,readonly
entry
code32
adr r0,thumb+1
bx r0
code16
thumb
mov r0,#00        ;  first two fibonacci numbers
sub r0,r0,#01     ; assigning -1 to first register
mov r1,#01
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
mov r3,r0
mov r0,r1
mov r1,r3
sub r4,#01        ;decrementing the counter
cmp r4,#00        ;comparing the counter to zero
bne back          ;looping back
end
``````

Thank you visit again :)

#### No comments:

Powered by Blogger.