'Recursive Quicksort in MIPS Assembly language
Trying to change the following java code into MIPS asm code. All the code i can find online is using the asm language designed for intel chips and not MIPS where they push the arguments onto the stack and not safe registers values.
void quicksort( int[] array, int p, int r) {
if( p < r) {
int q = partition( array, p, r);
quicksort( array, p, q - 1);
quicksort( array, q + 1, r);
}
}
int partition( int[] array, int p, int r) {
int pivot = array[ r];
int i = p -1;
for( int j = p, j < r; j++) {
if( array[ j] <= pivot) {
i = i + 1;
swap( array, i, j);
}
}
swap( array, i + 1, r);
return( i + 1);
}
void swap( int[] array, int i, int j) {
int temp = array[ i];
array[ i] = array[ j];
array[ j] = temp;
}
The following swap function was provided:
swap:
addi $sp, $sp, -28 # Push the stack pointer down to hold seven values
sw $ra, 0 ($sp) # Store the return address and s registers on the stack
sw $s0, 4 ($sp)
sw $s1, 8 ($sp)
sw $s2, 12($sp)
sw $s3, 16($sp)
sw $s4, 20($sp)
sw $s5, 24($sp)
# Done with protecting registers. Now for the real work:
sll $s0, $a1, 2 # from * 4 into $s0
sll $s1, $a2, 2 # to * 4 into $s1
add $s2, $a0, $s0 # Address of array[from] into $s2
add $s3, $a0, $s1 # Address of array[ to] int9 $s3
lw $s4, 0($s2) # Value of array[from] into $s4
lw $s5, 0($s3) # Value of array[to] into $s5
sw $s4, 0($s3) # Value from $s4 into array[to]
sw $s5, 0($s2) # Value from $s5 into array[from]
# Done with the work. No return value.
lw $ra, 0 ($sp) # Restore the $ra and $s registers from the stack
lw $s0, 4 ($sp)
lw $s1, 8 ($sp)
lw $s2, 12($sp)
lw $s3, 16($sp)
lw $s4, 20($sp)
lw $s5, 24($sp)
addi $sp, $sp, 28 # Pop the stack pointer
jr $ra
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
