Hello I am unsure why my MIPS merge sort is not working PLEASE HELP! Can you ple
ID: 3550537 • Letter: H
Question
Hello I am unsure why my MIPS merge sort is not working PLEASE HELP! Can you please re-write it in a way that will run!!
#class main_project1 {
#static int Y1[] = {13, 101, 79, 23, 154, 4, 11, 38, 88, 45, 17, 94, 62};
#static int lenY1 = 13;
#public static void main(String[] args) {
# mergesort(Y1,0,lenY1-1);
# for(int i = 0; i<lenY1; i++)
# System.out.print(Y1[i] + " ");
# System.out.println(" ");
#}
#public static void mergesort(int x[], int left, int right) {
# int middle;
# if (left>=right) {
# return;
# }
#else {
# middle=left+((right-left+1)>>1);
# mergesort(x,left,middle-1);
# mergesort(x,middle,right);
# merge(x,left,middle,right);
# }
#}
#public static void merge(int x[], int left, int middle, int right) {
# int leftind, endleft, rightind, endright, temp, i;
# leftind=left;
# rightind=middle;
# endleft=middle-1;
# endright=right;
# while ((leftind<=endleft)&&(rightind<=endright)) {
# if (x[leftind]<=x[rightind]) {
# leftind++;
# }
# else {
# temp=x[rightind];
# for (i=rightind-1; i>=leftind; i--) {
# x[i+1]=x[i];
# }
# x[leftind]=temp;
# leftind++;
# endleft++;
# rightind++;
# }
# }
#}
#}
.data
str: .asciiz "Unsorted array:"
str2: .asciiz "Sorted array:"
#Y1: .word 13, 101, 79, 23, 154, 4, 11, 38, 88, 45, 17, 94, 62
lenY1: .word 5
Y1: .space 20 # 5 ints in 20 bytes
.text
.globl main
main:
la $s7, Y1 #array
li $t1, 200 # first #
sw $t1, 0($s7)
li $t1, 101 # second #
sw $t1, 4($s7)
li $t1, 79 #third #
sw $t1, 8($s7)
li $t1, 23 #fourth #
sw $t1, 12($s7)
li $t1, 154 #fifth #
sw $t1, 16($s7)
lw $t0, lenY1
#add $s7, $s7, $zero
addi $a1, $zero, 0
addi $a2, $t0, -1
li $v0, 4
la $a0, str # printing unsorted
syscall # print the string
loop_print:
li $v0, 1 # print the array
lw $a0, 0($s7)
syscall
addi $s7, $s7, 4
beq $t0, $zero, mergesort
sub $t0, $t0, 1
j loop_print
#jal mergesort
lw $t0, lenY1
li $v0, 4
la $a0, str2 # print Sorted array
syscall # print the string
loop_print2:
li $v0, 1 # print the array
lw $a0, 0($s7)
syscall
addi $s7, $s7, 4
beq $t0, $zero, exit_loop_print
sub $t0, $t0, 1
j loop_print2
mergesort:
addi $sp,$sp,-32 #make room on stack
sw $s0, 0($sp) #array
sw $s1, 4($sp) #left
sw $s2, 8($sp) #right
sw $s3, 12($sp) #middle
sw $s4, 16($sp)
sw $s5, 20($sp)
sw $s6, 24($sp)
sw $s7, 28($sp)
sw $ra, 32($sp) #return reg
add $s0, $s7, $zero #array
add $s1, $a1, $zero #left
add $s2, $a2, $zero #right
slt $t1, $s2, $s1 #is right < left
bne $t1, $zero, end
addi $t2, $s1, 1 #t2= left +1
sub $t3, $s2, $t2 #t3 = $s2+$t2
srl $t4, $t3, 1
add $s3, $s1, $t4 #middle = left +t4
add $s7, $s0, $zero
add $a1, $s1, $zero
addi $a2, $s3, -1
jal mergesort
add $s7, $s0, $zero
add $a1, $s3, $zero
add $a2, $s2, $zero
jal mergesort
add $s7, $s0, $zero
add $a1, $s1, $zero
add $a2, $s3, $zero
add $a3, $s2, $zero
jal merge
end:
lw $s0, 0($sp)
lw $s1, 4($sp)
lw $s2, 8($sp)
lw $s3, 12($sp)
lw $s4, 16($sp)
lw $s5, 20($sp)
lw $s6, 24($sp)
lw $s7, 28($sp)
lw $ra, 32($sp)
addi $sp, $sp, 32
jr $ra
merge:
add $s4, $a1, $zero #leftind
add $s5, $a2, $zero #rightind
addi $s6, $a2, -1 #endleft
add $t9, $a3, $zero #endright
while:
slt $t5, $s6, $s4 #t5 = s6<s4
bne $t5, $zero, exit
slt $t5, $t9, $s5 #t5 = t9<s5
bne $t5, $zero, exit
if:
add $s5, $s5, $a0
lw $t1, 0($s5) #t1 =x[rightind]
add $s4, $s4, $a0
lw $t2, 0($s4)
slt $t5, $t1, $t2
bne $t5, $zero, else
addi $s4, $s4, 1
else:
add $s5, $s5, $a0
add $t6, $t1, $zero #temp = x[rightind]
add $t8, $s5, -1 #i
loop:
slt $t7, $s4, $t5
beq $t7, $zero, exit_loop
add $t8, $t8, $a0
lw $t3, 0($t8) #x[i]
sw $t3, 4($t8)
sub $t5, $t5, 1
j loop
exit_loop:
add $s4, $s4, $a0
lw $t1, 4($s4) #x[leftind]
sw $t1, 4($t6)
addi $s4, $s4, 1
addi $s6, $s6, 1
addi $s5, $s5, 1
j while
exit:
jr $ra
exit_loop_print:
li $v0, 10
syscall
Explanation / Answer
//this is the correct working code as per your requirements.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.