Question: Central to many sorting algorithms is the ability to swap array elements that are in the wrong order. Since we have not yet covered conditional
Central to many sorting algorithms is the ability to swap array elements that are in the wrong order. Since we have not yet covered conditional (if) statements in MIPS and we would need that to check for ordering, we will for now just concern ourselves with swapping two elements i and j, i.e. in Java:
int tmp = nums[i];
nums[i] = nums[j];
nums[j] = tmp;
We will assume i has already been loaded into register $s1 and j into $s2 (I have code that sets $s1 to 2 and $s2 to 5, but it should work for any i and j between 0 and 11). You may use any available $s register for tmp (Remember $s1 and $s2 are currently occupied, as are $s0 and $s5). Use temporaries as appropriate (do not overwrite variables with intermediate results).
Your job is to translate the above code to MIPS, within the block labeled PUT SWAP CODE HERE. If you keep $s1 at 2 and $s2 at 5 the code should swap elements 2 and 5 (which are the third and sixth elements of the array). You can check this by viewing the state of the RAM in MARS after the program runs.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
