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 The first n number will be shown as the output, where n will be given by the user. As the first two numbers and are predefined in a Fibonacci sequence, n must be Otherwise, the program will show an error message. The skeleton program uses two leaf procedures:
printnumber: this procedure prints an integer followed by a space character.
addtwo: this procedure does the addition of two integers, save the result in $s and return the value of $s 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 $s in the main program.
The addtwo procedure must use $s 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 changedelete the existing code andor 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 and n must be
#Two restrictions:
#$s must store the value of n in the main program.
#$s must also be used as the register for storing the temporarylocal result of the addition in the procedure
text
main:
# Print input prompt
li $v # syscall code for printing a string
la $a inprompt # assign the prompt string to a for printing
syscall
# Read the value of n from user and save to t
li $v # syscall code for reading an integer from user to v
syscall
### Set s with the user input & if invalid jumpt to exiterror ###
### Set proper argument register and call the procedure printnumnber to show the first two numbers of the sequence & ###
li $t # initialize counter register t
### Load the first two numbers & to s & s ###
loop:
### set the argument registers a and a to the last two values in the sequence for addition, and then call proedure addtwo###
move $s $s # s now stores the last value in the sequence
move $s $v # s now new value as returned from the addition procedure
move $a $v # update a for printing the returned value
jal printnumber
### Increment the counter and compare it with user input. If equal, jumpt to exit.###
j loop # Go to the start of the loop
addtwo:
### Push the value of s in the stack ###
add $s $a $a # s 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 s from the stack ###
jr $ra # return to the caller
# segement for printing an integer
printnumber:
### Write the syscall code for printing the integer ###
# syscall for printing a space character
li $v
la $a space
syscall
jr $ra #return to the caller
# exit block for wrong value of the input
exiterror:
li $v # syscall code for printing the error message
la $a errorstring
syscall
li $v # syscall code for terminate the execution of the program
syscall
# exit block for the program
exit:
li $v # syscall code for terminate the execution of the program
syscall
data
inprompt: asciiz "how many numbers in the sequence you want to see must be at least :
errorstring: asciiz "The number ust be greater than or equal to
space: asciiz
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
