For this assignment, you will write functions in SPIM. These functions MUST follow the MIPS Convention for Register Usage. You are to write functions that will:
Your program may have other functions, but the above must be included.
Your program is to have a data section that contains two floating point arrays. Each array should be able to store up to 100 values; however, may actually hold fewer values. All access to the arrays and any supporting variables (like a length or size information) from the functions must be made through the use of parameters. Any information about the arrays that is accessed directly by a function will result in a grade of zero for the assignment.
Your program is to perform the following steps:
The function to read in a floating point array should prompt the user for values and read in values until either the value 0.0 is read in or the array is filled. The value of 0.0 is not to be stored in the array. This function must pass back or return the number of values actually stored in the array. A possible C prototype for this function could be:
void readInput (float arr[], int maxSize, int* actualSize);
The function to output the floating point array should list the numbers in some uniform manner (like two per line). A possible C prototype for this function could be:
void printArray (float arr[], int actualSize);
The function that determines the smaller of the two floating point numbers is to return the value of 1 if the first parameter is smaller than the second parameter. This function is to return the value of 0 if the first parameter is not smaller than the second parameter. This function is simply an isLessThan() function. Note: this function is not to use the floating point comparison operations. You are to write your own algorithm to determine if the first floating point parameter is smaller than the second floating point parameter. This function may use any of the "integer comparison" operation. Thus you may wish to seperate a floating point value into its three parts of sign, mantissa and exponent and work with these. Remember that floating point numbers use a "Sign & Magnitude" style of representation. Thus, when comparing two negative numbers, the value with the larger magnitude is the smaller of the two numbers. The exponent and the mantissa combine to make up the magnitude of the floating point number. A possbile C prototype for this function could be:
int isLessThan (float val1, float val2);
The function to sort a floating point array can use any algorithm that you like; however, you must use your comparison function to perform any comparison of two floats. Note that a less than function can be used to determine any of the six relational operations.
Relational Operation | Function |
---|---|
X < Y | isLessThen(X, Y) == 1 |
X <= Y | isLessThen(Y, X) == 0 |
X > Y | isLessThen(Y, X) == 1 |
X >= Y | !isLessThen(X, Y) == 0 |
X == Y | (isLessThen(X, Y) == 0) && (isLessThan(Y, X) == 0) |
X != Y | (isLessThen(X, Y) == 1) || (isLessThan(Y, X) == 1) |
void sort (float *arr[], int actualSize);
For the function that loops through two arrays and compares the corresponding values in each position of the arrays, you are to print out the smaller value at each position. Remember you are to use your comparison function to perform any comparison of two floating point numbers. The C code to do this is:
/* assume we have parameters arr1, arr2, len1 and len2 */ /* determine the size of the smaller array */ if (len1 < len2) len = len1; else len = len2; for (i = 0; i < len; i++) { printf ("The smaller value at position %d is ", i); if (isLessThen(arr1[i], arr2[i]) == 1) printf ("%f\n", arr1[i]); else printf ("%f\n", arr2[i]); }
The programs will be graded based on how they execute on xspim running in the CS Department computers. You are to submit your program electronically using the the turnin command with the project name of mp3. Your program must be well commented and use blank lines to separate logical sections of your code.
You can earn 10 points extra credit on this assignment by using a recursive quicksort or recursive mergesort sorting algorithm for your sorting function. Note that the recursive sorting function will still need to use the "isLessThan()" function to compare two floating point numbers. If you are doing this, you MUST have your program print out the appropriate message:
This program uses the recursive quicksort algorithm. or This program uses the recursive mergesort algorithm.at the beginning of your program. If you do not print out the message, the TA will NOT look for the recursive sorting function and you will not get any extra credit.
Register Name | Alternative Name | Description |
---|---|---|
$0 | the value 0 | |
$1 | $at | reserved by the assembler |
$2 - $3 | $v0 - $v1 | expression evaluation and function results |
$4 - $7 | $a0 - $a3 | the first four parameters - not preserved across procedure calls |
$8 - $15 | $t0 - $t7 | temporaries - not preserved across procedure calls |
$16 - $23 | $s0 - $s7 | saved values - preserved across procedure calls |
$24 - $25 | $t8 - $t9 | temporaries - not preserved across procedure calls |
$26 - $27 | $k0 - $k | reserved for use by the operating system |
$28 | $gp | global pointer |
$29 | $sp | stack pointer |
$30 | $s8 | saved value - preserved across procedure calls |
$31 | $ra | return address |
$f0 - $f2 | floating point function results | |
$f4 - $f10 | temporaries - not preserved across procedure calls | |
$f12 - $f14 | the first two floating point parameters - not preserved across procedure calls | |
$f16 - $18 | temporaries - not preserved across procedure calls | |
$f20 - $f30 | saved values - preserved across procedure calls |
Service | System Call Code placed in $2/$v0 | Arguments | Results |
---|---|---|---|
print_int | 1 | $a0 = integer | |
print_float | 2 | $f12 = float | |
print_double | 3 | $f12 = double | |
print_string | 4 | $a0 = address of string | |
read_int | 5 | integer (in $v0) | |
read_float | 6 | float (in $f0) | |
read_double | 7 | double (in $f0) | |
read_string | 8 | $a0 = address of string buffer $a1 = length of string buffer | |
sbrk | 9 | $a0 = amount | address (in $v0) |
exit | 10 |
The following special characters are used with character strings in SPIM. The special characters follow the C language convention:
Character | Encoding Sequence |
---|---|
Newline | \n |
Tab | \t |
Double quote | \" |