Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

. section . data input _ prompt: . asciz Please enter a number between 1 and 1 0 input _ spec: . asciz

.section .data
input_prompt: .asciz "Please enter a number between 1 and 10
"
input_spec: .asciz "%d"
fib: .asciz "%d
"
oob_mess: .asciz "Input is out of bounds
"
.section .text
.global main
main:
// Print greeting and ask for n between 1-10
ldr x0,=input_prompt
bl printf
sub sp, sp, #16// Allocate stack space for input (8 bytes aligned to 16 bytes)
// Read inputted number
ldr x1,=input_spec
mov x0, sp // Use stack space to store input
bl scanf
// Check for out of bound input
ldr x2,[sp]// Load the inputted number from stack
// Compare if value inputted is less than 1
cmp x2, #1
blt checkOutOfBounds
// Compare if value inputted is greater than 10
cmp x2, #10
bgt checkOutOfBounds
// Calculate Fibonacci and print result
mov x0, x2
bl recurse
// Print the result
ldr x1,=fib
bl printf
b exit
checkOutOfBounds:
ldr x0,=oob_mess
bl printf
b exit
base_case_1:
mov x0, #0
ret
base_case_2:
mov x0, #1
ret
recurse:
subs x9, x0, #1
B.GT recurse
cmp x0, #1
beq base_case_1
cmp x0, #2
beq base_case_2
// Allocate stack space for call with n-1
sub sp, sp, #16
str x30,[sp, #8]
str x0,[sp]
sub x0, x0, #2
bl recurse
// Load result from fibonacci(n-2) into x2
str x0,[sp, #8]
ldr x0,[sp]
ldr x30,[sp, #8]
add sp, sp, #16
sub x0, x0, #1
bl recurse
add x0, x0, x2
ret
exit:
mov x0, #0
mov x8, #93
svc #0
It is a fibonacci sequence procedure fib(argument: n)
return fib(n-2)+fib(n-1)
1should return 0(base case)
2should return 1(base case)
3should return 1
4should return 2
5should return 3
6should return 5
7should return 8
8should return 13
9should return 21
10should return 34
For some reason the program is printing the "Please enter a number"... then automatically printing "Input is out of bounds and quitting" I think that is my error. Should I have a seperate fibonacci function and then calling the recurse? How so?

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Learning PostgreSQL

Authors: Salahaldin Juba, Achim Vannahme, Andrey Volkov

1st Edition

178398919X, 9781783989195

More Books

Students also viewed these Databases questions