In: Computer Science
The Fibonacci Sequence is a series of integers. The first two numbers in the sequence are both 1; after that, each number is the sum of the preceding two numbers.
1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, ...
For example, 1+1=2, 1+2=3, 2+3=5, 3+5=8, etc.
The nth Fibonacci number is the nth number in this sequence, so for example fibonacci(1)=1, fibonacci(2)=1, fibonacci(3)=2, fibonacci(4)=3, etc. Do not use zero-based counting; fibonacci(4)is 3, not 5.
Your assignment is to write an ARM assembler code (Fibonacci.s) that prompts the user for the nth term of the fibonacci sequence. The program will then calculate the users selected nth fibonacci term and print it out.
The program should produce this input:
Enter the desired Fibonacci term: 6
The 6th Fibonacci number is: 8
Please find the ARM code for finding the desired term in Fibonnaci Sequence
.LC0:
.ascii "Enter the desired Fibonacci term: \000"
.LC1:
.ascii "%d\000"
.LC2:
.ascii "Fibonacci of negative number is not possible.\000"
.LC3:
.ascii "The %d of Fibonacci is %d\012\000"
main:
push {fp, lr}
add fp, sp, #4
sub sp, sp, #8
ldr r0, .L5
bl printf
sub r3, fp, #12
mov r1, r3
ldr r0, .L5+4
bl __isoc99_scanf
ldr r3, [fp, #-12]
cmp r3, #0
bge .L2
ldr r0, .L5+8
bl puts
b .L3
.L2:
ldr r3, [fp, #-12]
mov r0, r3
bl fibonacci
str r0, [fp, #-8]
ldr r3, [fp, #-12]
ldr r2, [fp, #-8]
mov r1, r3
ldr r0, .L5+12
bl printf
.L3:
mov r3, #0
mov r0, r3
sub sp, fp, #4
pop {fp, lr}
bx lr
.L5:
.word .LC0
.word .LC1
.word .LC2
.word .LC3
fibonacci:
push {r4, fp, lr}
add fp, sp, #8
sub sp, sp, #12
str r0, [fp, #-16]
ldr r3, [fp, #-16]
cmp r3, #0
bne .L8
mov r3, #0
b .L9
.L8:
ldr r3, [fp, #-16]
cmp r3, #1
bne .L10
mov r3, #1
b .L9
.L10:
ldr r3, [fp, #-16]
sub r3, r3, #1
mov r0, r3
bl fibonacci
mov r4, r0
ldr r3, [fp, #-16]
sub r3, r3, #2
mov r0, r3
bl fibonacci
mov r3, r0
add r3, r4, r3
.L9:
mov r0, r3
sub sp, fp, #8
pop {r4, fp, lr}
bx lr