Question: ########################################################### # Lab 3 # Name: # Date: # # Description: # Write main and one subprogram read_values. # # main should have two static
########################################################### # Lab 3 # Name: # Date: # # Description: # Write main and one subprogram read_values. # # main should have two static variables sum_var, count_var, and also a static array # of 10 integers (array_var), all have initial value of 0. It will load address of # that static variable and pass it into subprogram read_values as argument IN. Then # main should print out sum and count of user input (arguments OUT), also calculate # and print the integer average. Sum and count of user input should be stored into # static variable sum_var and count_var before your program ends. # # read_values should do as follow: # # - take the base address from $a0 (argument IN) # # - use a loop to ask user input no more than 10 non-negative integers (we # only have enough space for 10 integers in static memory) and store them # one by one into the static array, stop when user input -1. User don't # have to fill out the entire array. # # - an error message should show to user if invalid number is input. # # - calculate sum and count during the loop and return them as arguments OUT # ($v0 & $v1) # # - you should prepend "read_values" to all labels in subprogram (both .data # and .text). ex: loop --> read_values_loop # prompt_p --> read_values_prompt_p # # High level design: # main: # sum & count <-- read_values(base address of array) # # store sum & count in static variables "sum_var", "count_var" respectively # # print sum, count and integer average # # read_values(base address): # initialize sum and count to 0 # # while (true) { # if (number == -1 || count == 10) { # break # } # # prompt for number and read number # # if (number < 0) { # print error message # } # # array[base address] = number # base address = base address + 4 // because integers are 4 bytes in MIPS # // and addresses in MIPS are in bytes # # sum = sum + number # count = count + 1 # } # # return sum & count in register $v0 and $v1 respectively # ########################################################### # Register Usage # $t0 Holds the returned sum # $t1 Holds the returned count # $t3 Holes integer average # $t4 # $t5 # $t6 # $t7 # $t8 # $t9 temporally register ########################################################### .data sum_p: .asciiz "Sum: " count_p: .asciiz "Count: " average_p: .asciiz "Average: " nextline_p: .asciiz " "
# To Do: create static array of 10 integers sum_var: .word 0 # sum variable initialized to 0 count_var: .word 0 # count variable initialized to 0 ########################################################### .text main:
mainEnd: li $v0, 10 syscall # Halt ########################################################### # read_values subprogram # # Subprogram description: # Write an assembly sub-program that will read a series of non-negative integers into # static array, It will stop reading when -1 is entered OR count of numbers becomes 10, # it should print out an error for all invalid inputs and those invalid entries will # be ignored. After all the integers are read, Program will output the sum, count to main # ########################################################### # Arguments IN and OUT of subprogram # $a0 Holds base address of an array # $a1 # $a2 # $a3 # $v0 Holds sum # $v1 Holds count # $sp # $sp+4 # $sp+8 # $sp+12 ########################################################### # Register Usage # $t0 Holds base array base address # $t1 Holds sum # $t2 Holds count # $t3 # $t4 # $t5 # $t6 # $t7 # $t8 Holds constant 10 # $t9 Holds constant -1 ########################################################### .data read_values_prompt_p: .asciiz "Enter a non-negative integer (>=0): " read_values_invalid_p: .asciiz "Invalid entry. Number should be non-negative. " ########################################################### .text read_values:
jr $ra # jump back to the main ###########################################################
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
