Question: Please help me fix the bug, do run the entire code in your IDE first, please dont post vague answer, and read the entire code
Please help me fix the bug, do run the entire code in your IDE first, please dont post vague answer, and read the entire code as well. Previously someone posted the answer without reading full code. Thanks in advance:
This is Quick Sort in MIPS
########################################
.data
smallTable: .word 7,8,1,9,2,6,3,10,4,5
.text
# test quicksort with smallTable la $a0, smallTable li $a1, 0 li $a2, 9 jal qs # terminate li $v0, 10 syscall
qs: addi $sp,$sp, -20 sw $ra, 16($sp) sw $s3,12($sp) # save $s3 on stack sw $s2, 8($sp) # save $s2 on stack sw $s1, 4($sp) # save $s1s on stack sw $s0, 0($sp) # save $s0 on stack
move $s1, $a0 move $s2, $a1 #start move $s3, $a2 #end slt $t0, $s2, $s3 beq $t0, $zero, done add $a1, $s2, $zero add $a2, $s3, $zero jal partition move $s0, $v0 #pivot lw $s2, 8($sp) move $a1, $s2 add $a2, $s0, -1 jal qs add $a1, $s0, 1 lw $s3,12($sp) add $a2, $s3, $zero jal qs
done:
lw $s0, 0($sp) # restore $s0 from stack lw $s1, 4($sp) # restore $s1 from stack lw $s2, 8($sp) # restore $s2 from stack lw $s3,12($sp) # restore $s3 from stack lw $ra,16($sp) # restore $ra from stack addi $sp,$sp, 20 # restore stack pointer
jr $ra
partition: addi $sp,$sp, -28 sw $ra, 24($sp) sw $s5, 20($sp) sw $s4, 16($sp) sw $s3,12($sp) # save $s3 on stack sw $s2, 8($sp) # save $s2 on stack sw $s1, 4($sp) # save $s1 on stack sw $s0, 0($sp) # save $s0 on stack
move $s3, $a0 #v[] move $s4, $a1 #low/start move $s5, $a2 #high/end sll $t0, $s5, 2 add $t0, $s3, $t0 lw $s0, 0($t0) #$ s0 = pivot add $s1, $s4, $zero #i add $s2, $s4, $zero # j forloop: slt $t1, $s2, $s5 # check if j exit2: addi $s2, $s2, 1 #j++ j forloop exit1: move $a0, $s3 move $a1, $s1 move $a2, $s5 #v0 = i+1 return (i + 1); jal swap add $v0, $zero, $s1 ################################################### lw $s0, 0($sp) # restore $s0 from stack lw $s1, 4($sp) # restore $s1 from stack lw $s2, 8($sp) # restore $s2 from stack lw $s3,12($sp) # restore $s3 from stack lw $s4,16($sp) lw $s5,20($sp) lw $ra,24($sp) # restore $ra from stack addi $sp,$sp, 28 # restore stack pointer jr $ra swap: ################################################### # YOUR CODE HERE sll $t0, $a1, 2 #t1 = 4a add $t0, $a0, $t0 #t1 = arr + 4a lw $t1, 0($t0) #s3 t = array[a] sll $t2, $a2, 2 #t2 = 4b add $t2, $a0, $t2 #t2 = arr + 4b lw $t3, 0($t2) #s4 = arr[b] sw $t3, 0($t0) #arr[a] = arr[b] sw $t1, 0($t2) #arr[b] = t jr $ra #jump back to the caller
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
