0

I'm going to convert from c program to mips(by using eclipse, ubuntu)

In the process, when I debug it. I met

No source available for "QuickSort()"

and the cursor for debugging is stopped at

add $t1, $s0, $t2

And then go to break(); I tried to exchange it with two sentences(which have load $s0 and then add) it didn't work.

Could anyone check and correct my program?

The mips program I trasnlated data = numbers[] in c program

.globl QuickSort
QuickSort:
        beq $a1, $a2, QuickSortEnd
        nop
        addi $sp, $sp, -20
        sw $ra, 16($sp)
        sw $s3, 12($sp) #pivot
        sw $s2, 8($sp)  # right
        sw $s1, 4($sp)  # left
        sw $s0, 0($sp)  # data[]

        move $s2, $a2   #get right (move)
        move $s1, $a1   #get left (move)
        move $s0, $a0   #get data address (move)

        addi $t0, $a1, 1        # l_hold = left + 1
        add $t1, $a2, $zero     # r_hold = right

        sll $t2, $s1, 2         # $t2 = left*4
        add $t3, $s0, $t2       # $t3 = data + left*4 = data[left]
        lw $t4, 0($t3)          # load data[left]
        move $s3, $t4           # save it to pivot(s3)
while_1:
        slt $t4, $s1, $s2       #if left<right, t4=1
        beq $t4, $zero, Exit_while1 #if not (left<right), go to exit
while_in2:sll $t2, $s2, 2
        add $t3, $s0, $t2
        lw $t5, 0($t3)          # $t5 = data[right]
        slt $t2, $t5, $s3       # num[right] >=pivot -> t2=0
        slt $t3, $s1, $s2       # left<right    -> t3=1
        addi $t3, $t3, -1
        bne $t2, $t3, If1       #(numbers[right] >= pivot) && (left < right)
        addi $s2, $s2, -1       # right--
        nop
        j while_in2             # loop
If1:    beq $s2, $s1, While_in3
        nop
        sll $t2, $s1, 2
        add $t3, $s0, $t2       # data[left]
        sll $t5, $s2, 2
        add $t6, $s0, $t5
        lw $t7, 0($t6)          # t7 = data[right]
        sw $t7, 0($t3)          # data[left] = data[right]
        addi $s1, $s1, 1        # left++
While_in3:sll $t2, $s2, 2
        add $t3, $s0, $t2
        lw $t2, 0($t3)          # $t2 = data[left]
        slt $t4, $s3, $t2       # pivot < num[left] : 1
        slt $t5, $s1, $s2
        addi $t5,$t5, -1
        bne $t4, $t5, If2
        nop
        addi $s1, $s1, 1        # left++
        nop
        j While_in3
If2:    beq $s2, $s1, Exit_If2
        nop
        sll $t2, $s1, 2
        add $t3, $s0, $t2
        lw $t4, 0($t3)          # t4= data[left]
        sll $t5, $s2, 2
        add $t6, $s0, $t5       # data[right]
        sw $t4, 0($t6)          # data[right] = data[left]
        addi $s2, $s2, -1       # right--
        nop
Exit_If2:j while_1
Exit_while1:
        sll $t2, $s1, 2
        add $t3, $s0, $t2       # data[left]
        sw $s3, 0($t3)          # data[left] = pivot
        lw $t4, 4($sp)
        sw $t4, 12($sp)         # pivot = left
        sw $t0, 4($sp)          # left = l_hold
        sw $t1, 8($sp)          # right = r_hold
lastIf1:bge $s1, $s3, lastIf2
        addi $a2, $s3, -1       #right= pivot-1
        nop
        jal QuickSort
lastIf2:bge $s3, $s2, QuickSortEnd
        addi $a1, $s3, 1
        nop
        jal QuickSort
QuickSortEnd: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

The original C program for Quick Sort (Ref: Wikipedia, korea)

 /* void quickSort(int numbers[], int array_size)
 {
   q_sort(numbers, 0, array_size - 1);
 }

 void q_sort(int numbers[], int left, int right)
 {
   if(left == right) return;
   int pivot, l_hold, r_hold;
   l_hold = left+1;
   r_hold = right;
   pivot = numbers[left];

   while (left < right)
   {
     while ((numbers[right] >= pivot) && (left < right))
       right--;

     if (left != right)
     {
       numbers[left] = numbers[right];
       left++;
     }

     while ((numbers[left] <= pivot) && (left < right))
       left++;

     if (left != right)
     {
       numbers[right] = numbers[left];
       right--;
     }
   }

   numbers[left] = pivot;
   pivot = left;
   left = l_hold;
   right = r_hold;

   if (left < pivot)
     q_sort(numbers, left, pivot-1);
   if (right > pivot)
     q_sort(numbers, pivot+1, right);
 }*/
Cœur
  • 37,241
  • 25
  • 195
  • 267
Eunbi
  • 1
  • _"by using eclipse, ubuntu"_ So you have an actual MIPS-based device to test this on? If so, you should go over all your branches/jumps and ensure that you've correctly filled the branch delay slots. I saw a few that looked wrong, like `j` immediately followed by `beq`. From _MIPS32™ Architecture For Programmers Volume II: The MIPS32™ Instruction Set_: _"**Processor operation is UNPREDICTABLE if a branch, jump**, ERET, DERET, or WAIT instruction **is placed in the delay slot of a branch or jump**"_. – Michael Oct 30 '15 at 17:57
  • As far as your debugging problems go; you could try running your code in a MIPS simulator on your computer instead (e.g. PCSpim, QtSpim or MARS). Note that none of these simulate branch delay slots by default, so you'd have to enable that in the simulator's settings dialog first. – Michael Oct 30 '15 at 17:59
  • I just use Oracle VM VirtualBox, and Eclipse MARS in the virtualbox(ubuntu). I have mips gcc compiler. Thanks to your comment, I edit my code . but it didn't work . I will keep finding solution. thks I'm downloading PCSpim for another checking. I will try it. thanks. – Eunbi Oct 31 '15 at 11:08
  • _"I just use Oracle VM VirtualBox"_ VirtualBox is an x86 virtualizer. You can certainly _compile_ MIPS code on an x86 system using a cross-compiler, but to _run_ the compiled code you'd need a MIPS emulator or simulator (or a physical MIPS-based device). – Michael Oct 31 '15 at 11:40
  • and also I found another (probably important) problem. I tried to make another project to reset all configurations. And the DEBUG folder was not created. And also original project also doesn't have debug file.....hahaha................. (I am trying this because my professor explain it by that system. it just doesn't work!! omg) Okay I need to try MIPS emulator. – Eunbi Oct 31 '15 at 13:34
  • I... think my code is working well in MIPS emulator! Maybe I will explain it to professor. PROBABLY it works!!!! I thought my code(corrected) was not mistake. just the setting was well not organized. Thanks so michael! – Eunbi Oct 31 '15 at 13:56

0 Answers0