Question: IN MIPS: Write the code in main to call the subprogram prompt_for_positive_number using the system stack to allocate space for the expected arguments OUT. The

IN MIPS:

  1. Write the code in main to call the subprogram prompt_for_positive_number using the system stack to allocate space for the expected arguments OUT. The subprogram itself has been written for you -- you just need to set up the stack to call it properly.
  2. Set up the call to the subprogram factorials using the system stack as well.
  3. Finish writing the stub method for factorials which takes in an integer as an argument IN and provides the calculated factorial as an argument OUT.

#######################################################################

# Lab #7 - recursion

# Name:

# Date:

#

# Notes:

#

# The purpose of this lab is to help you understand more about

# system stack, especially how to use system stack to achieve

# multilevel subprogram calls. We will practice this by introducing

# recursion.

#

# Recursion is an important concept in programming and it can be

# done in MIPS using system stack. Because by backing up return

# addresses and all currently used registers, we can design a

# subprogram that will keep calling itself to do some algorithms.

#

# In order to achieve this goal, you will need to do all 13 steps

# to call a subprogram. Please try not to take any shortcuts.

#

# Description:

#

# main:

# - first it will call prompt_for_positive_number to get a

# positive integer and then pass it into factorial to get

# the factorial result

# - it will then print this result to console (this part is

# given)

#

# prompt_for_positive_number:

# - this subprogram will prompt user for a number, if it's

# positive, return the number, otherwise, print error msg

# and re-prompt. (this subprogram is given)

#

# factorial:

# - this subprogram will take a positive number as argument

# IN, and then recursively calculate the factorial result.

# Notice:

# (registers are all 32 bits long, the largest number an

# integer register can represent is 2^31-1 which is around

# 2 billian. and 13! is around 6 billian. therefore,

# because of the limitation, we can't do more than 12!

# in main processor)

#

# High level design:

# print factorials( prompt_for_positive_number() )

#

#######################################################################

# Register Usage

# $t0 number

# $t1 factorial output

#######################################################################

.data

factorial_of_p: .asciiz "Factorial of "

factorial_is_p: .asciiz " is: "

#######################################################################

.text

main:

# calling subprogram prompt_for_positive_number

####################

# TODO:

# using system stack, call subprogram prompt_for_positive_number that

# will read a integer from console. then store the positive number in

# register $t0

####################

# calling subprogram factorials

####################

# TODO:

# using system stack, call subprogram factorials that will calculate a

# factorial of number that is in register $t0. then store the result

# of factorial in register $t1

####################

# printing original number and factorial result

li $v0, 4 # print factorial of:

la $a0, factorial_of_p

syscall

li $v0, 1 # print value of factorial

move $a0, $t0 # move the original number into register $ao

syscall

li $v0, 4 # print factorial of:

la $a0, factorial_is_p

syscall

li $v0, 1 # print value of factorial

move $a0, $t1 # move the factorial value form $t1 into register $a0

syscall

mainEnd:

li $v0, 10

syscall # halt

#######################################################################

# Prompt for positive number Subprogram

#

# prompts for a positive number, and validate the number is positive

#

# High level design:

# int prompt_for_positive_number() {

# while (true) {

# number <-- prompt and read a number

#

# if (number > 0)

# return number;

# }

# }

#

#######################################################################

# Arguments IN and OUT of subprogram

# $sp+0 Positive number (OUT)

#######################################################################

# Register Usage

# $t0 Holds the recursive argument (number, or number -1, or number - 2 and etc.)

# $t1 Holds the the return value of the recursive subprogram

# $t2 Hols the return value of number * (number - 1)!

#######################################################################

.data

prompt_for_positive_number_number_p: .asciiz "Please enter a positive number (number > 0): "

prompt_for_positive_number_invalid_number_p: .asciiz "Please enter a valid number! "

#######################################################################

.text

prompt_for_positive_number:

prompt_for_positive_number_loop:

la $a0, prompt_for_positive_number_number_p # prompt for positive number

li $v0, 4

syscall

li $v0, 5 # read number

syscall

bgtz $v0, prompt_for_positive_number_end # if number is greater than or equal to zero,

# then branch to prompt_for_positive_number_end

la $a0, prompt_for_positive_number_invalid_number_p # print error message when number <= 0

li $v0, 4

syscall

b prompt_for_positive_number # branch unconditionally back to the beginning of the loop

prompt_for_positive_number_end:

sw $v0, 0($sp) # store the number in allocated stack

jr $ra # jump back to the main

#######################################################################

# Factorials Subprogram

#

# calculates the factorial of the given number

#

# High level design:

# int factorials(int number) {

# if (number == 0)

# return 1;

# else

# return number * factorial(number - 1);

# }

#

#######################################################################

# Arguments IN and OUT of subprogram

# $sp+0 Positive number (IN)

# $sp+4 Result of factorial (OUT)

#######################################################################

# Register Usage

# $t0 Holds the recursive argument (number, or number -1, or number - 2 and etc.)

# $t1 Holds the factorial output from lower subprogram

#######################################################################

.data

#######################################################################

.text

factorials:

####################

# TODO:

# write a subprogram factorials. first load argument IN from stack and check if

# argument IN number equals to zero, if yes then return value one. else call subprogram factorials

# and pass ( number - 1 ) as argument IN, then multiply ( number ) with ( number - 1 )

# and return the result

####################

factorial_return:

jr $ra # jump to parent call

#######################################################################

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!