Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a program to nd and print a Fibonacci sequence of numbers. The Fibonacci s

ID: 3630880 • Letter: W

Question

Write a program to nd and print a Fibonacci sequence of numbers. The Fibonacci
sequence is dened as follow:

Your program should prompt the user to enter a limit and indicate whether the last
number in the sequence printed is either even or odd.
Here is what the user will see when the program is executed:
This program prints the Fibonacci sequence
Enter a limit on the largest number to be displayed: 5
1 1 2 3 5
The last number in this sequence is an odd number.
Do you want to print a dierent sequence (Y/N): Y
Enter a limit on the largest number to be displayed: 50
1 1 2 3 5 8 13 21 34
The last number in this sequence is an even number.
Do you want to print a dierent sequence (Y/N): N

Explanation / Answer

#Program stores fibonacci number in an array and displays
fibonacci:
save %sp,-96,%sp   
     
cmp %i0,49    ! n >= MAX_FIB_REPRESENTABLE ?
      ! note, n, the 1st parameter to
      ! fibonacci(), is stored in %i0 upon
      ! entry
mov 0,%i2    ! initialization of variable
      ! prev_number is executed in the
      ! delay slot

sethi %hi(.L20),%o0   ! if branch not taken, call printf(),
or %o0,%lo(.L20),%o0  ! set up 1st, 2nd argument in %o0, %o1;
call printf,2   ! the ",2" means there are 2 out
mov %i0,%o1    ! registers used as arguments
/* exit(1); */
call exit,1    
mov 1,%o0
.L77003:     ! initialize variables before the loop

mov 1,%i4    ! curr_number = 1
mov 2,%i3    ! i = 2
cmp %i3,%i0    ! i <= n?
bge .L77006    ! if not, return
sethi %hi(.L16+8),%o0   ! use %i5 to store fib_array[i]
add %o0,%lo(.L16+8),%i5
.LY1:      ! loop body
/* fib_array[i] = prev_number + curr_number; */
add %i2,%i4,%i2   ! fib_array[i] = prev_number+curr_number
st %i2,[%i5]
/* prev_number = curr_number; */
mov %i4,%i2    ! prev_number = curr_number
/* curr_number = fib_array[i]; */
ld [%i5],%i4   ! curr_number = fib_array[i]
inc %i3    ! i++
cmp %i3,%i0    ! i <= n?
bl .LY1    ! if yes, repeat loop
inc 4,%i5    ! increment ptr to fib_array[]
.L77006:
/* return(fib_array); */
sethi %hi(.L16),%o0   ! return fib_array in %i0
add %o0,%lo(.L16),%i0
ret
restore     ! destroy stack frame and register
      ! window
.type fibonacci,#function  ! fibonacci() is of type function
.size fibonacci,(.-fibonacci)  ! size of function:
      ! current location counter minus
      ! beginning definition of function


.proc 18    ! main program
.global main
.align 4
main:
save %sp,-104,%sp   ! create stack frame for main()
/* printf("Fibonacci(n):, please input n: "); */
sethi %hi(.L31),%o0   ! call printf, with 1st arg in %o0
call printf,1
or %o0,%lo(.L31),%o0
/* scanf("%d", &n); */
sethi %hi(.L33),%o0   ! call scanf, with 1st arg, in %o0
or %o0,%lo(.L33),%o0  ! move 2nd arg. to %o1, in delay slot
call scanf,2
add %fp,-4,%o1

/* result = fibonacci(n); */
call fibonacci,1   
ld [%fp-4],%o0

      ! some initializations before the for-
      ! loop, put the variables in registers
/* for (i = 1; i <= n; i++) */
mov 1,%i5    ! %i5 <-- i
mov %o0,%i4    ! %i4 <-- result
sethi %hi(.L38),%o0   ! %i2 <-- format string for printf
add %o0,%lo(.L38),%i2
ld [%fp-4],%o0   ! test if (i <= n) ?
cmp %i5,%o0    ! note, n is stored in [%fp-4]
bg .LE27
nop
.LY2:      ! loop body

ld [%i4],%o2   ! call printf, with (*result) in %o2,
mov %i5,%o1    !   i in %o1, format string in %o0
call printf,3
mov %i2,%o0
inc %i5    ! i++
ld [%fp-4],%o0   ! i <= n?
cmp %i5,%o0    
ble .LY2
inc 4,%i4    ! result++

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote