this is the answer, but still not working .section \".data\" /*Variables*/ promp
ID: 3630305 • Letter: T
Question
this is the answer, but still not working
.section ".data"
/*Variables*/
prompt: .asciz " This program prints the Fibonacci sequence"
prompt2: .asciz " Enter a limit on the largest number to be displayed:"
prompt5: .asciz " Do you want to print a different sequence (Y/N):"
input: .word 0
yesNo: .byte 0
nl: .asciz " "
format1: .asciz "%d%c"
format2: .asciz "%c%c"
!CODE!
.align 8
.global main
.section ".text"
define(f_1, l0)
define(f_2, l1)
define(count, l2)
main:
save %sp, -96, %sp
repeat:
!!!!Welcome and initial prompt!!!
set prompt, %o0 !set welcome message
call printf !print it
nop
set prompt2, %o0 !ask fo a number
call printf
nop
set format1, %o0 !input needs to be an interger
set input, %o1 !address where input is stored
set nl, %o2 !dump
call scanf
nop
ld [%o1], %o0
mov %o0, %count
mov 1, %f_1
mov 1, %f_2
mov 1, %o0
call printf
nop
mov 1, %o0
call printf
nop
loop:
add %f_1, %f_2, %o0
call printf
mov %f_1, %f_2
mov %o0, %f_1
cmp %f_1, %count
ble loop
!!!Continue?!!!
set prompt5, %o0
call printf
nop
set format2, %o0 !getting a character and a newline
set yesNo, %o1
set nl, %o2
call scanf
nop
set yesNo, %l0
ldub [%l0], %o0 !get the ys/no from memory
cmp %o0, 'y'
be repeat
nop !yes then try again
ret !get out
restore
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
/* printing ("Fibonacci(%d) cannot be represented in a 32 bits word ", n); */
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++
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.