Question
Write a MIPS code The objectives of this program is to understand dynamic arrays, stacks and subprogram calling another subprogram (function calls that are more
Write a MIPS code The objectives of this program is to understand dynamic arrays, stacks and subprogram calling another subprogram (function calls that are more than one level deep).
create_array will have no arguments IN and two arguments OUT:
$sp+0 - array base address (OUT) $sp+4 - array length (OUT)
create_array will call subprogram allocate_array to dynamically allocate array. Then it will call read_array to fill the array. Lastly, create_array will return base address and array size back to the main.
allocate_array will have no arguments IN and two arguments OUT:
$sp+0 - array base address (OUT)
$sp+4 - array length (OUT)
allocate_array will ask the user for the size of the array and will validate that it is between 5 (exclusive) and 10 (inclusive) elements or (5, 10]. If an invalid length is entered, allocate_array will print an error message and reprompt the user for the length. When a valid length is entered, allocate_array will dynamically allocate exactly enough memory to hold the array, and then return base address and array size back to create_array subprogram.
read_array will have two arguments IN and no arguments OUT:
$sp+0 - array base address (IN)
$sp+4 - array length (IN)
read_array will read values from the user and store them in the array until the array is full. A prompt must be printed before each entry is read (e.g. Enter an integer: ).
print_array will have two arguments IN and no arguments OUT:
$sp+0 - array length (IN)
$sp+4 - array base address (IN)
print_array will print all values from the array with each value separated by a space.
print_every_nth will have three arguments IN and no arguments OUT:
$sp+0 - array base address (IN)
$sp+4 - array length (IN)
$sp+8 - element stride N (IN) print_every_nth will print every nth value from the array, starting from the first value (index 0). For example, consider the array [1 2 3 4 5 6 7 8 9 0].
For N = 3, the output would be [1 4 7 0].
For N = 4, the output would be [1 5 9].
If N 0, the subprogram must print an error and return without printing anything.
sum_odd_values will have two arguments IN and one argument OUT:
$sp+0 - array base address (IN) $sp+4 - array length (IN) $sp+8 - sum of odd values (OUT)
sum_odd_values will calculate the sum of all odd values in the array and return that sum. For example: given the array [8 4 3 6 5 5 2 8 7 2], the sum would be 20. sum_odd_values MAY NOT print anything (main must print the sum).
reverse_array will have two arguments IN and one argument OUT:
$sp+0 - array base address (IN) $sp+4 - array length (IN)
$sp+8 - array base address of reversed array (OUT)
reverse_array will dynamically allocate an array with the same size as original array. Then copies values from original array into reversed array. Lastly, it will return array base address of reversed array back to main. Remember the size of reverse array is the same as size of original array so there is no need to return it back to main.
pseudocode:
revered array [0] = original array [size -1]
revered array [1] = original array [size -2]
revered array [size - 1] = original array [0]
Array: [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]
Reversed array: [0, 9, 8, 7, 6, 5, 4, 3, 2, 1]
Main will call subprograms create_array, print_array, print_every_nth, sum_odd_values and reverse_array in this order. The main should also ask the user to specify size of the interval to be passed to print_every_nth. Note that create_array itself calls subprograms allocate_array and read_array. Remember that main should also print the returned sum of odd values in an array and reverse of array.
Notes:
Arguments MUST be passed on the system stack as specified (do not reorder them).
Subprograms MAY NOT access labels from another subprogram data section.
template
########################################################### # Program Description
########################################################### # Register Usage # $t0 # $t1 # $t2 # $t3 # $t4 # $t5 # $t6 # $t7 # $t8 # $t9 ########################################################### .data
########################################################### .text main:
li $v0, 10 #End Program syscall ########################################################### ########################################################### # Subprogram Description
########################################################### # Arguments In and Out of subprogram # # $a0 # $a1 # $a2 # $a3 # $v0 # $v1 # $sp # $sp+4 # $sp+8 # $sp+12 ########################################################### # Register Usage # $t0 # $t1 # $t2 # $t3 # $t4 # $t5 # $t6 # $t7 # $t8 # $t9 ########################################################### .data
########################################################### .text
jr $ra #return to calling location ########################################################### ########################################################### # Subprogram Description
########################################################### # Arguments In and Out of subprogram # # $a0 # $a1 # $a2 # $a3 # $v0 # $v1 # $sp # $sp+4 # $sp+8 # $sp+12 ########################################################### # Register Usage # $t0 # $t1 # $t2 # $t3 # $t4 # $t5 # $t6 # $t7 # $t8 # $t9 ########################################################### .data
########################################################### .text
jr $ra #return to calling location ###########################################################
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started