rite a program to find and print a Fibonacci sequence of numbers. The Fibonacci
ID: 3652055 • Letter: R
Question
rite a program to find and print a Fibonacci sequence of numbers. The Fibonacci sequence isdefined as follows:
Fn = Fn-2 + Fn-1,n >= 2,where F0 = 0,F1 = 1
Your program should prompt the user to enter a limit, print the sequence up to the limit, and then
indicate what is the greatest power of 2 that the final number printed is divisible by. Note: You
are NOT allowed to use the divide function.
When trying to compile this program i get the below errors
$ m4 k9.m > k9.s
$ gcc k9.s -o k9
Undefined first referenced
symbol in file
i3 /var/tmp//ccg4vymi.o
i4 /var/tmp//ccg4vymi.o
l0 /var/tmp//ccg4vymi.o
l1 /var/tmp//ccg4vymi.o
l2 /var/tmp//ccg4vymi.o
l3 /var/tmp//ccg4vymi.o
l4 /var/tmp//ccg4vymi.o
l5 /var/tmp//ccg4vymi.o
uinput_r /var/tmp//ccg4vymi.o
outfib /var/tmp//ccg4vymi.o
ld: fatal: Symbol referencing errors. No output written to k9
collect2: ld returned 1 exit status
define(fib1, i4) ![%fp + fib1]
define(fib2, i3) ![%fp + fib2]
define(fib3, l0) ![%fp + fib3]
define(j_r, l1) ![%fp + j_r]
define(temp_r, l2) ![%fp + temp_r]
define(userinput_r, l3) ![%fp + uinput_r]
define(final_r, l4) ![%fp + final_r]
define(max_r, l5) ![%fp + max_r]
.section ".data"
/*Variables data section*/
intro: .asciz " This program prints the Fibonacci sequence." !Program purpose statemt
userp: .asciz " Enter a limit on the largest number to be displayed: " !Prompt user to enter a number
number: .asciz "%d" !number to be entered
formtp: .word 0 !where the user input number will be read in
pfib1: .asciz " %d "
oufib: .asciz "%d "
uoutp: .asciz " The last number %d is disvisible by %d. " !Power of 2
conte: .asciz " Do you want to print a different sequence (y/n):" !Continue or stop statement
anser: .asciz "%c"
formta: .word 0 !where the user input number will be read in
stopgo: .word 0 !where the user input number will be read in
off: .asciz "pause"
.align 4
.global main
.section ".text"
main: save %sp, -152, %sp
start:
!Post program statement to the user
mov 1, %o0
st %o0, [%fp + fib1]
mov 1, %o1
st %o1, [%fp + fib2]
set intro, %o0
call printf
nop
!Prompts for userinput
!Enter a limit on the largest number to be displayed
set userp, %o0
call printf
nop
set number, %o0 !sets the type of data to be entered: integer
set formtp, %o1 !location where input will be stored
call scanf !gets the input into the variable userinput
nop
!Store fib sequence
set pfib1, %o0 !sets the type of data to be entered: integer
ld [%fp + fib1], %o1
call printf !print fibonacci sequence
nop
while: !Do loop that calculates the fib sequence and prints it
set outfib, %o0
ld [%fp + fib2], %o1 !printsf("%d ", fib2)
call printf
nop
ld [%fp + fib1], %o2
ld [%fp + fib2], %o3
add %o2, %o3, %o5
st %o5, [%fp + fib3] !fib3 = fib1 + fib2
ld [%fp + fib2], %o2
st %o2, [%fp + fib1] !fib1 = fib2
ld [%fp + fib3], %o2
st %o2, [%fp + fib2] !fib2 = fib3
ld [%fp + fib3], %o2
ld [%fp + userinput_r], %o3
cmp %o2, %o3 !while(fib3 <= userinput)
bg swap
nop
b while
swap:
st %g0, [%fp + j_r] !int j
ld [%fp + fib1], %o0
st %o0, [%fp + temp_r] !int temp = fib1
mov 2, %o1
st %o1, [%fp + final_r] !int final = 2
mov 1, %o1
st %o1, [%fp + max_r] !int max = 1
forloop:
ld [%fp + temp_r], %o1
mov %o1, %o0
ld [%fp + max_r], %o2
call start
nop
mov %o0, %o1
cmp %o1, 0
bne printlast
nop
ld [%fp + max_r], %o0
ld [%fp + temp_r], %o1
cmp %o0, %o1
bg printlast
nop
ld [%fp + max_r], %o0
st %o0, [%fp + j_r]
ld [%fp + max_r], %o0
add %o0, %o0, %o0
st %o0, [%fp + max_r]
b forloop
nop
printlast:
set uoutp, %o0 !The last number %d is disvisible by %d. "
ld [%fp + temp_r], %o1
ld [%fp + j_r], %o2
call printf
nop
set conte, %o0 !Continue or stop statement
call printf
nop
add %fp, -25, %o5 !User response to stop or continue
set anser, %o3
mov %o5, %o1
call scanf
nop
ldub [%fp + uinput_r], %o3
sll %o3, 24, %o3
sra %o3, 24, %o3
cmp %o3, 110
be done
nop
b start
nop
done: !LL3
set off, %o0
call system, 0
nop
mov 0, %o0
mov %o0, %i0
ret
restore
Explanation / Answer
//C Program for generate Fibonacci series #include "stdio.h" #include "conio.h" void main() { int a,b,c,i,n; clrscr(); a=0; b=1; printf(" enter n for how many times generate series"); scanf("%d",&n); printf(" FIBONACCI SERIES "); printf(" %d %d",a,b); for(i=0;iRelated Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.