Question: Create MIPS assembly code for the following C function. It is an insertion sort program. You dont need to generate any code for the include,
Create MIPS assembly code for the following C function. It is an insertion sort program. You dont need to generate any code for the include, namespace, and function prototype lines. Assume the random number function is at memory address RAND :, and dont generate code for it.
Remember that any local values in temporary registers will NOT be maintained across function calls. If you use a saved register, you must maintain the original value by placing it on the stack, and then restoring the original value at the end.
It might be easier to write the shift function first, then write the main function.
// Dont generate any MIPS for the next 4 lines #include #include using namespace std; void shift( int array[], int index ); // start creating MIPS code for the rest. // Note that some lines are commented (no code for those)
int main(){ int array[1000];
int x;
for( int i = 0; i < 1000; i++ ){ array[i] = 2001; }
// for ( int i = 0; i < 1000; i++ ) cout << array[i] << " "; // cout << "----"<
// Assume function rand() is implemented at memory address RAND // and it returns a random integer in register $v0
for( int i = 0; i < 1000; i++){ x = rand()%2000;
for ( int j = 0;j < 1000; j++ ) { if ( x < array[j] ){
shift( array, j); array[j] = x; j = 1000;
} } }
// for ( int i = 0; i < 1000; i++ ) cout << array[i] << " "; // cout << "----"<
}
void shift( int array[], int index ){
for (int i = 999; i > index; i-- )
array[i] = array[ i-1 ];
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
