# Compute several Fibonacci numbers and put in array, then print .data fibs:.wor
ID: 667146 • Letter: #
Question
# Compute several Fibonacci numbers and put in array, then print
.data
fibs:.word 0 : 19 # "array" of words to contain fib values
size: .word 19 # size of "array" (agrees with array declaration)
.text
la $s0, fibs # load address of array
la $s5, size # load address of size variable
lw $s5, 0($s5) # load array size
li $s2, 1 # 1 is the known value of first and second Fib. number
sw $s2, 0($s0) # F[0] = 1
sw $s2, 4($s0) # F[1] = F[0] = 1
addi $s1, $s5, -2 # Counter for loop, will execute (size-2) times
# Loop to compute each Fibonacci number using the previous two Fib. numbers.
loop: lw $s3, 0($s0) # Get value from array F[n-2]
lw $s4, 4($s0) # Get value from array F[n-1]
add $s2, $s3, $s4 # F[n] = F[n-1] + F[n-2]
sw $s2, 8($s0) # Store newly computed F[n] in array
addi $s0, $s0, 4 # increment address to now-known Fib. number storage
addi $s1, $s1, -1 # decrement loop counter
bgtz $s1, loop # repeat while not finished
# Fibonacci numbers are computed and stored in array. Print them.
la $a0, fibs # first argument for print (array)
add $a1, $zero, $s5 # second argument for print (size)
jal print # call print routine.
# The program is finished. Exit.
li $v0, 10 # system call for exit
syscall # Exit!
###############################################################
# Subroutine to print the numbers on one line.
.data
space:.asciiz " " # space to insert between numbers
head: .asciiz "The Fibonacci numbers are: "
.text
print:add $t0, $zero, $a0 # starting address of array of data to be printed
add $t1, $zero, $a1 # initialize loop counter to array size
la $a0, head # load address of the print heading string
li $v0, 4 # specify Print String service
syscall # print the heading string
out: lw $a0, 0($t0) # load the integer to be printed (the current Fib. number)
li $v0, 1 # specify Print Integer service
syscall # print fibonacci number
la $a0, space # load address of spacer for syscall
li $v0, 4 # specify Print String service
syscall # print the spacer string
addi $t0, $t0, 4 # increment address of data to be printed
addi $t1, $t1, -1 # decrement loop counter
bgtz $t1, out # repeat while not finished
jr $ra # return from subroutine
# End of subroutine to print the numbers on one line
###############################################################
Questions:
What does the final value of $s2 represent?
What does the final value of $s3 represent?
What does the final value of $s4 represent?
What does the final value of $s5 represent?
Modify the code to generate 21 Fibonacci numbers
Explanation / Answer
Run the program One Step at a Time from start (at line 7) until the.
program finish execute the instruction at line 24 (addi $ 1, $ 5, -2) and.
describe what happen to the Registers and Data Segment on each.
step.
la $ S0, fibs # Register $ s0 holds the first address.
la $ S5, size # Register $ s0 holds the address offset reserved.
lw $ S5, 0 ($ s5). # Register $ s5 keep the size of the array.
li $ S2, 1 # Register $ s2 has a value of one.
sw $ S2, 0 ($ s0). # Data segment is set to the first one.
sw $ S2, 4 ($ s0). # Data segment where the value is 1 second.
addi $ s1, $ s5, -2. # Register $ s5
Set a break point (Bkpt) on the instruction at line 24 and run the.
program. Describe what happen in the Data Segment when the.
program stop at the breakpoint. Do the same until the execution
finishes.
Data Segment low, it's to keep the sum of the previous two numbers. Then put on hold indefinitely
$2 = F0 + F1 = 2.
$3 = F1 + F2 = 3.
$4 = F2 + F3 = 5.
$5 = F3 + F4 = 8.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.