Question: . 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:
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 KEEPS AUTOMATICALLY PRINTING OUT "Input is out of bounds" and ending the program

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!