187k views
4 votes
Implement a subprogram which takes 4 numbers in the argument registers $a0...$a3 and

returns the largest value and the average in $v0 and $v1 to the calling program. The
program must be structured as follows:
Subprogram largestAndAverage($a1, $a2, $a3, $a4)
{
int var0 = $a0, var1 = $a1, var2 = $a2, var3 = $a3;
$s0 = getLarger($a1, $a2);
$s0 = getLarger($s0, $a3);
$v0 = getLarager(s0, $a4); // Largest is in $v0

$v1 = (var0 + var1 + var2 + var3)/ 4; // Average is in $v1
return;
}

Subprogram getLarger($a0, $a1) {
$v0 = $a0
if ($a1 > $a0)
$v0 = $a1
return;
}

1 Answer

3 votes

Final answer:

The question is about implementing a MIPS assembly subprogram that finds the largest value and average from four input numbers. The largest number is returned in register $v0, and the average in register $v1, using the getLarger function for comparisons.

Step-by-step explanation:

The student inquiry pertains to creating a subprogram in MIPS assembly that processes four numbers and computes the largest number and the average, returning these values in specific registers.

The correct MIPS assembly subprogram should first store the four arguments in local variables. Using a helper function named getLarger, the subprogram determines the largest number by comparing pairs of numbers sequentially. After finding the largest number, the program calculates the average of the four numbers by summing them up and dividing by four. The largest value is returned in the $v0 register and the average in the $v1 register.

It is important to note that there is a typo in the original pseudocode. The function name getLarager should be getLarger, and the final comparison should be made using the fourth variable var3 not $a4, which is not defined.

User Til Hund
by
8.0k points