PLEASE WRITE IN MIPS! Focus: System stack, dynamic arrays. Objectives: You will write a program that will create an array and perform various tasks with
PLEASE WRITE IN MIPS!
Focus:
System stack, dynamic arrays.
Objectives:
You will write a program that will create an array and perform various tasks with it. The array length should be between 5 and 15 (inclusive).
Once the array has been created, it should be printed to the console in a readable format. Then you will call a subprogram to sum up all the even values contained in the array, returning this sum and the number of even values (a.k.a. count) back to main. Finally, main will call print_sum_average to calculate the average, then print the sum, count, and average, before returning back to main and terminating the program.
This will require at least 6 subprograms, which are described in more detail below.
create_array:
Inputs a number between 3-10 (inclusive) from the user, then uses two subprograms to allocate an array of that size and fill it with user input. It should call allocate_array after obtaining a valid array length, and then use the length and returned base address to call read_array to prompt the user for values. Once both actions are complete, it sends back both the array base address and the length as arguments back to main.
Arguments IN: none
Arguments OUT:
- $sp+0: array base address
- $sp+4: array length
allocate_array:
Creates a dynamic array of the given size.
Arguments IN:
- $sp+0: array length
Arguments OUT:
- $sp+4: array base address
read_array:
Reads in a series of integers as user input into the given array.
Arguments IN:
- $sp+0: array base address
- $sp+4: array length
Arguments OUT: none
print_array:
Prints each value of an array to the console in a readable format, starting from index 0.
Arguments IN:
- $sp+0: array base address
- $sp+4: array length
Arguments OUT: none
sum_even_values:
Loops through the array and sums up the elements in the array, but only include the ones that are even. It then returns the sum back to main. Do not print the result in this subprogram!
Ex: sum of array [ 2, 5, 17, 8, -1, 6 ] is 16 (2 + 8 + 6)
Arguments IN:
- $sp+0: array base address
- $sp+4: array length
Arguments OUT:
- $sp+8: sum of all even values in the array
- $sp+12: count of all even values in the array
print_sum_average:
Calculates the average of the even values in the array by dividing the sum by the count passed in, and prints the sum, count, and average before returning to main. If the sum and count are 0, the program should NOT crash!
Arguments IN:
- $sp+0: sum of all even values in array
- $sp+4: count of all even values in array
Arguments OUT: none
Program Outline:
- main
- create_array
- allocate_array
- read_array
- print_array
- sum_even_values
- print_sum_average
- create_array
Other Requirements:
- You should start with the skeleton file subprogram_template.s, which can be found under the Class Files module on the class homepage. This file contains stubs to subprograms, but you may need to copy/paste another stub if theres not enough pre-made.
- Make sure to write your name, date, a brief description of the program, and a brief description of each subprogram where prompted in the comments.
- You should write some comments as you go along, where appropriate, to indicate whats happening in your program.
- Fill out the Registers section with the registers you used and the purpose for which you used them, as well as the stack offsets youre using to pass arguments in and out of the subprograms.
- You must use the stack to pass all of your arguments, in the exact order specified in this document. Failure to do so will lose you points in your program!
Other Helpful Notes:
- Start early enough to be able to ask for help if needed!
- Feel free to use the solutions to prior labs as guidance on how to approach certain tasks.
- The array length should be between [5, 15], i.e. no less than 5 and no greater than 15: 5 and 15 are both valid array lengths.
- When working on MIPS programs, it is recommended to take things one step at a time and test your program as you complete each objective. Doing too much at once can create confusion and make it much more difficult to catch bugs!
Sample output:
Note that you output does not have to look completely identical to this! This is just an example of how it should behave you are welcome to write your own error messages/prompts.
Console Enter an array length between 5 and 15: 3 Invalid length entered! Enter an array length between 5 and 15: 6 Enter an integer: 2 Enter an integer: 3 Enter an integer: 6 Enter an integer: 8 Enter an integer: 5 Enter an integer: 1 Array: 2 3 6 8 5 1 Sum of even values: 16 Count of even values: 3 Average: 5 Console Enter an array length between 5 and 15: 5 Enter an integer: 1 Enter an integer: 3 Enter an integer: 5 Enter an integer: 7 Enter an integer: 9 Array: 1 3 5 7 9 Sum of even values: 0 Count of even values: 0 Average: no average to calculate !|Step by Step Solution
There are 3 Steps involved in it
Step: 1
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