Question: In this lab, you will practice writing leaf procedures. A skeleton code is provided for you to write a program which will calculate the Fibonacci

In this lab, you will practice writing leaf procedures. A skeleton code is provided for you to write a program which will calculate the Fibonacci sequence (0,1,1,2,3,5,8,....). The first n number will be shown as the output, where n will be given by the user. As the first two numbers (0 and 1) are predefined in a Fibonacci sequence, n must be >=3. Otherwise, the program will show an error message. The skeleton program uses two leaf procedures:
print_number: this procedure prints an integer followed by a space character.
add_two: this procedure does the addition of two integers, save the result in $s0, and return the value of $s0 to the caller.
The two following restrictions are added so that you have some practice of using stack in the callee procedure.
The input n must be stored in $s0 in the main program.
The add_two procedure must use $s0 to save the result of the addition before returning its value to the caller.
Your task is to complete the given skeleton program. You need to add one or more lines of code to accomplish tasks mentioned in the comments of this format: ### Comment ###. Add the lines of code after each of these comments to complete the program.
Do not change/delete the existing code and/or existing comments.
#This is a MIPS program to print first n numbers fo the fibonacci sequence. n will be user input.
#As the first two number of the sequence are given (0 and 1), n must be >=3
#Two restrictions:
#$s0 must store the value of n in the main program.
#$s0 must also be used as the register for storing the temporary/local result of the addition in the procedure
.text
main:
# Print input prompt
li $v0,4 # syscall code =4 for printing a string
la $a0, in_prompt # assign the prompt string to a0 for printing
syscall
# Read the value of n from user and save to t0
li $v0,5 # syscall code =5 for reading an integer from user to v0
syscall
### Set s0 with the user input & if invalid (<=2) jumpt to exit_error ###
### Set proper argument register and call the procedure print_numnber to show the first two numbers of the sequence (0 & 1)###
li $t0,2 # initialize counter register t0
### Load the first two numbers (0 & 1) to s1 & s2 ###
loop:
### set the argument registers (a0 and a1) to the last two values in the sequence for addition, and then call proedure add_two###
move $s1, $s2 # s1 now stores the last value in the sequence
move $s2, $v0 # s2 now new value as returned from the addition procedure
move $a0, $v0 # update a0 for printing the returned value
jal print_number
### Increment the counter and compare it with user input. If equal, jumpt to exit.###
j loop # Go to the start of the loop
add_two:
### Push the value of s0 in the stack ###
add $s0, $a0, $a1 # s0 now holds the result of the addition
### Set the register that will hold the reutrn value with the result of the addition ###
### Pop the value of s0 from the stack ###
jr $ra # return to the caller
# segement for printing an integer
print_number:
### Write the syscall code for printing the integer ###
# syscall for printing a space character
li $v0,4
la $a0, space
syscall
jr $ra #return to the caller
# exit block for wrong value of the input
exit_error:
li $v0,4 # syscall code =4 for printing the error message
la $a0, error_string
syscall
li $v0,10 # syscall code =10 for terminate the execution of the program
syscall
# exit block for the program
exit:
li $v0,10 # syscall code =10 for terminate the execution of the program
syscall
.data
in_prompt: .asciiz "how many numbers in the sequence you want to see (must be at least 3): "
error_string: .asciiz "The number ust be greater than or equal to 3"
space: .asciiz ""

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!