Update the provided assignment2.asm file with the necessary MIPS assembly code to implement the functionality in the pro
-
- Site Admin
- Posts: 899603
- Joined: Mon Aug 02, 2021 8:13 am
Update the provided assignment2.asm file with the necessary MIPS assembly code to implement the functionality in the pro
MIPS program will produce.
MIPS CODE PROVIDED BELOW
#################################################################
# DO NOT MODIFY any code above the STUDENT_CODE label.
#
#################################################################
.data
.align 0
msg0: .asciiz "Statistical Calculator!\n"
msg1: .asciiz "-----------------------\n"
msg2: .asciiz "Average: "
msg3: .asciiz "Maximum: "
msg4: .asciiz "Median: "
msg5: .asciiz "Minimum: "
msg6: .asciiz "Sum: "
msg7: .asciiz "\n"
msg8: .asciiz "Elapsed Time: "
.align 2
array: .word 91, 21, 10, 56, 35, 21, 99, 33, 13, 80,
79, 66, 52, 6, 4, 53, 67, 91, 67, 90
size: .word 20
timer: .word 0 # Used to calculate elapsed
time of program execution
.text
.globl main
# Display the floating-point (%double) value in
register (%register) to the user
.macro display_double (%register)
li $v0, 3
# Prepare the system for output
mov.d $f12, %register #
Set the integer to display
syscall
# System displays the
specified integer
.end_macro
# Display the %integer value to the user
.macro display_integer (%integer)
li $v0, 1
# Prepare the system for
output
add $a0, $zero,
%integer # Set the integer to display
syscall
# System
displays the specified integer
.end_macro
# Display the %string to the user
.macro display_string (%string)
li $v0, 4
# Prepare the system for output
la $a0, %string
# Set the string to display
syscall
# System displays the
specified string
.end_macro
# Perform floating-point division %value1 /
%value2
# Result stored in register specified by
%register
.macro fp_div (%register, %value1, %value2)
mtc1.d %value1,
$f28 # Copy integer %value1 to
floating-point processor
mtc1.d %value2, $f30
# Copy integer %value2 to floating-point
processor
cvt.d.w $f28, $f28
# Convert integer %value1 to double
cvt.d.w $f30, $f30
# Convert integer %value2 to double
div.d %register, $f28,
$f30 # Divide %value1 by %value2 (%value1 /
%value2)
.end_macro
# Quotient stored in the
specified register (%register)
# Get start time for computing elapsed time
.macro get_start_time
get_current_time
sw $a0, timer
# Store the start time (in milliseconds) in the
timer memory
li $v0, 0
.end_macro
# Compute elapsed time
.macro compute_elapsed_time
get_current_time
lw $a1, timer
# Read the start time (in milliseconds) in the
timer memory
sub $a1, $a0, $a1 #
Subtract the start time from the finish time
display_string msg8 #
Display the "Elapsed Time: " string
display_integer $a1 #
Display the computed elapsed time of program execution
display_string msg7
.end_macro
# Request current time (in milliseconds) from OS
.macro get_current_time
li $v0, 30
# Prepare request the current
time (in milliseconds) from OS
syscall
# Submit
the request to the OS
.end_macro
main:
get_start_time # Used
to compute elapsed time
la $a0, array # Store
memory address of array in register $a0
lw $a1, size # Store
value of size in register $a1
jal getMax # Call the
getMax procedure
add $s0, $v0, $zero # Move maximum value
to register $s0
jal getMin # Call the
getMin procedure
add $s1, $v0, $zero # Move minimum value
to register $s1
jal calcSum # Call the
calcSum procedure
add $s2, $v0, $zero # Move sum value to
register $s2
jal calcAverage # Call
the calcAverage procedure (result is stored in floating-point
register $f2
jal sort # Call the
sort procedure
jal calcMedian # Call
the calcMedian procedure (result is stored in floating-point
register $f4
add $a1, $s0, $zero # Add maximum value to
the argumetns for the displayStatistics procedure
add $a2, $s1, $zero # Add minimum value to
the argumetns for the displayStatistics procedure
add $a3, $s2, $zero # Add sum value to the
argumetns for the displayStatistics procedure
jal displayStatistics # Call the
displayResults procedure
compute_elapsed_time # Used to compute
elapsed time
exit: li $v0, 10 #
Prepare to terminate the program
syscall
# Terminate the program
# Display the computed statistics
# $a1 - Maximum value in the array
# $a2 - Minimum value in the array
# $a3 - Sum of the values in the array
displayStatistics:
display_string msg0
display_string msg1
display_string msg6
display_integer $a3 #
Sum
display_string msg7
display_string msg5
display_integer $a2 # Minimum
display_string msg7
display_string msg3
display_integer $a1 # Maximum
display_string msg7
display_string msg2
display_double $f2 # Average
display_string msg7
extra_credit:
display_string msg4
display_double $f4 # Median
display_string msg7
jr $ra
#################################################################
# DO NOT MODIFY any code above the STUDENT_CODE label.
#
#################################################################
# Place all your code in the procedures provided below the
student_code label
student_code:
# Calculate the average of the values stored in the array
# $a0 - Memory address of the array
# $a1 - Size of the array (number of values)
# Result MUST be stored in floating-point register $f2
calcAverage:
#fp_div $f2, $rs, $rt # Perform
floating-point division on registers $rs and $rt ($rs / $rt)
jr $ra # Return to calling procedure
################################################################################
# Calculate the median of the values stored in the array
# $a0 - Memory address of the array
# $a1 - Size of the array (number of values)
# Result MUST be stored in floating-point register $f4
calcMedian:
#fp_div $f4, $rs, $rt # Perform
floating-point division on registers $rs and $rt ($rs / $rt)
jr $ra
# Return to calling procedure
################################################################################
# Calculate the sum of the values stored in the array
# $a0 - Memory address of the array
# $a1 - Size of the array (number of values)
# Result MUST be stored in register $v0
calcSum:
jr $ra # Return to calling procedure
################################################################################
# Return the maximum value in the array
# $a0 - Memory address of the array
# $a1 - Size of the array (number of values)
# Result MUST be stored in register $v0
getMax:
jr $ra # Return to calling procedure
################################################################################
# Return the minimum value in the array
# $a0 - Memory address of the array
# $a1 - Size of the array (number of values)
# Result MUST be stored in register $v0
getMin:
jr $ra # Return to calling procedure
################################################################################
# Perform the Selection Sort algorithm to sort the array
# $a0 - Memory address of the array
# $a1 - Size of the array (number of values)
sort:
jr $ra # Return to calling procedure
################################################################################
# Swap the values in the specified positions of the array
# $a0 - Memory address of the array
# $a1 - Index position of first value to swap
# $a2 - Index position of second value to swap
swap:
jr $ra # Return to calling procedure
Update the provided assignment2.asm file with the necessary MIPS assembly code to implement the functionality in the provided C program. Important Notes • The first 100 lines of code in the assignment2.asm file are provided by the professor to help students complete the assignment • Students are encouraged to not modify any of the MIPS assembly code provided by the professor . The professor will not troubleshoot any changes to the provided code Summary of Provided MIPS Code . One (1) array of the 20 integer values used in the C program One (1) size variable containing the size of the array used in the C program • Eight (8) strings used in the printf statements in the C program · Four (4) macros, which can be thought of as functions display_double %register) - Displays the double value stored in the specified floating-point register in the output display_integer (%integer) - Displays the specified integer in the output display_string (%string) - Displays the specified string in the output • fp_div %register, %value1, %value2) - Divides %value1 by %value2 and stores the quotient in the specified floating-point %register . The result of the fp_div macro is stored in the specified floating-point register Requirements Convert the functions (listed below) in the provided C program into the equivalent MIPS assembly code. Refer to the C Program tab in the Reference Material section for a listing of the entire C program. Students should refer to the Example Output tab in the Reference Material section for help with testing their solution. main Function • Notes 1. This function has been completed by the professor. 2. The memory address of the array is stored in register $a0. 3. The value of the size variable is stored in register $a1. • Warnings 1. The main procedure expects to find the results for the calcSum, getMax, and getMin procedures in the $VO register 2. The main procedure expects to find the results for the calcAverage and calcMedian (extra credit) procedures in the following floating-point registers: • calcAverage - $f2 • calcMedian - $f4 3. Students are responsible for ensuring the results are stored in the correct registers int main() // Array of values int array[SIZE] = {91, 21, 10,56,35,21,99,33,13,80,79,66,52,6,4,53,67,91,67,90}; int max = getMax (array, SIZE); int min = getMin(array, SIZE); int sum = calcSum(array, SIZE); double average calcAverage (array, SIZE); sort(array, SIZE); double median = calcMedian(array, SIZE); // Extra Credit displayStatistics(max, min, sum, average, median); return; }
getMax Function . Find the maximum value in the array . Be careful: The array is NOT sorted when this function is called • Store the minimum value in register $vo int getMax(int array[], int size) { int max = array[@]; for(int x = 1; x< size; x++) x x { if(array[x] > max) { max = array[x]; } } return max; } getMin Function . Find the minimum value in the array . Be careful: The array is NOT sorted when this function is called • Store the maximum value in register $vo int getMin(int array[], int size) { int min = array[0]; @ for(int x = 1; x< size; x++) { if(array[x] < min) { min = array[x]; } } } return min; } calcSum Function Compute the sum of all the values in the array • Store the sum value in register $vo · Warning . Be careful to NOT exceed the size of the array int calcSum(int array[], int size) { int sum = ; for(int x = 0; x< size; x++) x { sum += array[x]; } return sum } calcAverage Function . Compute the average of all the values in the array • To perform the sum / size operation, call the fp_div macro provided by the professor The result of the fp_div macro is stored in the specified floating-point register for later use . Example: fp_div $f2, $t0,$t1 # The result will be stored in floating-point register $82 • Note • Use the floating-point register $f2 when calling the fp_div macro to compute the average value double calcAverage (int array[], int size) { double sum = calcSum(array, size); return sum/ size; }
Statistical Calculator! Sum: 1034 Minimum: 4 Maximum: 99 Average: 51.7 Median: 54.5