Question: ########################################################### Do the ones that have TODO # Lab #8 - System Stack Practice # # Name: # Date: # # Description: # # The
###########################################################
Do the ones that have TODO
# Lab #8 - System Stack Practice
#
# Name:
# Date:
#
# Description:
#
# The majority of this lab has been written for you.
# You will notice that it looks very familiar to
# Program #2, except it has been adapted to use only
# dynamic arrays and the system stack.
#
# What you need to do: write the code in main to call
# the subprograms in the order specified. You MUST
# use the system stack to do so. There are also two
# other subprograms you need to write some code for:
#
# 1. create_array -- write the code to call read_array
# using the system stack.
# 2. sum_arrays -- write the code to obtain the
# arguments IN and put the argument OUT on the stack.
#
###########################################################
# Register Usage
# $t0 first array base address
# $t1 array length
# $t2 second array base address
# $t3 summed array base address
# $t4
# $t5
# $t6
# $t7
# $t8
# $t9 temporary register
###########################################################
.data
array_one_p: .asciiz " Array 1: "
array_two_p: .asciiz " Array 2: "
array_sum_p: .asciiz " Summed array: "
create_array_prompt: .asciiz "Enter an array length > 0: "
create_array_error: .asciiz "Array length is invalid! "
array_one: .word 0
array_two: .word 0
array_sum: .word 0
array_length: .word 0
###########################################################
.text
main:
create_array_loop:
li $v0, 4 # print a string
la $a0, create_array_prompt # $a0 = address of variable create_array_prompt
syscall
li $v0, 5 # input an integer
syscall # $v0 = user input
blez $v0, array_length_invalid # branch if $v0 <= 0
b array_length_valid # otherwise, branch to valid code
array_length_invalid:
li $v0, 4 # print a string
la $a0, create_array_error # $a0 address of variable create_array_error
syscall
b create_array_loop # branch to start of loop
array_length_valid:
move $t1, $v0 # $t1 = $v0
la $t9, array_length # $t9 = address of variable array_length
sw $t1, 0($t9) # store length into variable
##############################################################
# TODO: call subprogram create_array to create the first array
##############################################################
la $t9, array_one
sw $t0, 0($t9)
li $v0, 4
la $a0, array_one_p
syscall
##############################################################
# TODO: call subprogram print_array to print the first array
##############################################################
##############################################################
# TODO: call subprogram create_array to create the second array
##############################################################
la $t9, array_two
sw $t2, 0($t9)
li $v0, 4
la $a0, array_two_p
syscall
##############################################################
# TODO: call subprogram print_array to print the second array
##############################################################
##############################################################
# TODO: call subprogram sum_arrays to sum the two arrays together
##############################################################
la $t9, array_sum
sw $t3, 0($t9)
##############################################################
# TODO: call subprogram print_array to print the summed array
##############################################################
end_program:
li $v0, 10 # end program
syscall
###########################################################
###########################################################
# create_array
#
# Takes in a positive integer and dynamically allocates
# an array. It then calls read_array to prompt the user
# to fill the array with integers.
#
###########################################################
# Arguments In and Out of subprogram
#
# $sp array length (IN)
# $sp+4 array base address (OUT)
###########################################################
# Register Usage
# $t0 array base address
# $t1 array length
###########################################################
.data
###########################################################
.text
create_array:
lw $t1, 0($sp)
li $v0, 9 # allocate space in memory for an array
sll $a0, $t1, 2 # $a0 = $t1 * 4
syscall # $v0 = base address of newly allocated array
move $t0, $v0 # $t0 = $v0
##############################################################
# TODO: call subprogram read_array to read values into the array
##############################################################
create_array_end:
sw $t0, 4($sp)
jr $ra # return to calling location
###########################################################
# read_array
#
# Reads in integers greater than zero into a dynamic array,
# prompted by the user.
#
###########################################################
# Arguments In and Out of subprogram
#
# $sp array base address
# $sp+4 array length
###########################################################
# Register Usage
# $t0 array base address
# $t1 array length
###########################################################
.data
read_array_prompt: .asciiz "Enter an integer: "
###########################################################
.text
read_array:
lw $t0, 0($sp) # $t0 = array base address
lw $t1, 4($sp) # $t1 = array length
read_array_loop:
blez $t1, read_array_end # branch if $t1 <= 0
li $v0, 4 # print a string
la $a0, read_array_prompt # $a0 = variable address of read_array_prompt
syscall
li $v0, 5 # input an integer
syscall # $v0 = user input
sw $v0, 0($t0) # store $v0 into array position
addi $t0, $t0, 4 # increment array position
addi $t1, $t1, -1 # decrement count
b read_array_loop # branch to start of loop
read_array_end:
# read_array has no return values
jr $ra # return to calling location
###########################################################
###########################################################
# print_array
#
# Prints all integers in an array in an easy to read format.
#
###########################################################
# Arguments In and Out of subprogram
#
# $sp array base address
# $sp+4 array length
###########################################################
# Register Usage
# $t0 array base address
# $t1 array length
###########################################################
.data
###########################################################
.text
print_array:
lw $t0, 0($sp) # $t0 = array base address
lw $t1, 4($sp) # $t1 = array length
print_array_loop:
blez $t1, print_array_end # branch to end if $t1 <= 0
li $v0, 1 # print an integer
lw $a0, 0($t0) # get value at array position
syscall
li $v0, 11 # print a character
li $a0, 32 # $a0 = ASCII code 32 (space)
syscall
addi $t0, $t0, 4 # increment array position
addi $t1, $t1, -1 # decrement array size
b print_array_loop # branch to start of loop
print_array_end:
li $v0, 11 # print a character
li $a0, 10 # $a0 = ASCII code 10 (new line)
syscall
# print_array has no return values
jr $ra # return to calling location
###########################################################
###########################################################
# sum_arrays
#
# Creates a new array of the same length as the two input arrays.
# Then it adds together all values of the input arrays, storing
# the results in the newly created array. The base address of
# the new array is an argument OUT.
#
###########################################################
# Arguments In and Out of subprogram
#
# $sp first array base address
# $sp+4 second array base address
# $sp+8 array length
# $sp+12 new array
###########################################################
# Register Usage
# $t0 first array base address
# $t1 second array base address
# $t2 array length
# $t3 new array base address
# $t4 first array value
# $t5 second array value | sum
# $t6 copy of new array base address
###########################################################
.data
###########################################################
.text
sum_arrays:
##############################################################
# TODO: get arguments IN from the the stack using the proper offsets and registers
##############################################################
li $v0, 9 # allocate space in memory for an array
sll $a0, $t2, 2 # $a0 = $t2 * 4
syscall # $v0 = new array base address
move $t3, $v0 # copy base address into $t3 for looping
move $t6, $v0 # copy base address into $t6 for return value
sum_arrays_loop:
blez $t2, sum_arrays_end # branch to end if $t2 <= 0
lw $t4, 0($t0) # $t4 = get first array value
lw $t5, 0($t1) # $t5 = get second array value
add $t5, $t4, $t5 # $t5 = $t4 + $t5
sw $t5, 0($t3) # array position $t3 = $t5 (sum)
addi $t0, $t0, 4 # increment first array position
addi $t1, $t1, 4 # increment second array position
addi $t3, $t3, 4 # increment third array position
addi $t2, $t2, -1 # decrement counter
b sum_arrays_loop # branch to start of loop
sum_arrays_end:
##############################################################
# TODO: put argument OUT on the stack using the proper offset and register
##############################################################
jr $ra # return to calling location
###########################################################
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
