title: How to translate this sort algorithm to mips assembly
body: <p>My homework for my Computer Architecture was to write a program in MIPS assembly that can sort a 2D array in ascending order. I wrote a bubblesort algo in Java that can do this; however, I still am very new to MIPS and have no idea how to use the same logic in MIPS syntax.</p>
  
  <p>Specifically, how might I construct the while-for loops in MIPS? I know how the if and method calls translate (jal/j/jr and bgt/blt/slt) but I'm having trouble understanding how to structure my for-loops</p>
  
  <pre><code>public class D2ArraySort {
  
      public static int[][] switchValsEnd(int[][] a, int i, int j) {
          int temp;
          temp = a[i][j];
          a[i][j] = a[i + 1][0];
          a[i + 1][0] = temp;
          return a;
      }
  
      public static int[][] switchVals(int[][] a, int i, int j) {
          int temp;
          temp = a[i][j];
          a[i][j] = a[i][j + 1];
          a[i][j + 1] = temp;
          return a;
      }
  
      public static int[][] sort(int[][] a) {
          while (true) {
              for (int i = 0; i &lt; a.length; i++) {
                  for (int j = 0; j &lt; a[i].length; j++) {
                      if (j == a[i].length - 1 &amp;&amp; i != a.length - 1) {
                          if (a[i][j] &lt; a[i + 1][0]) {
                              a = switchValsEnd(a, i, j);
                              i = 0;
                              j = -1;
                          }
                      } else {
                          if (j == a[i].length - 1) {
                              return a;
                          }
                          if (a[i][j] &lt; a[i][j + 1]) {
                              a = switchVals(a, i, j);
                              i = 0;
                              j = -1;
                          }
                      }
                  }
              }
          }
      }
  
      public static void main(String[] args) { // don't need main other than to call j sort
          int[][] input_data = { // global input_data
                  {2, 0, -7, -1, 3, 8, -4, 10},
                  {-9, -16, 15, 13, 1, 4, -3, 14},
                  {-8, -10, -15, 6, -13, -5, 9, 12},
                  {-11, -14, -6, 11, 5, 7, -2, -12},
          };
          int[][] output_data = sort(input_data); // jal sort
          for (int i = 0; i &lt; output_data.length; i++) {
              System.out.print("( ");
              for (int j = 0; j &lt; output_data[i].length; j++) {
                  System.out.print(output_data[i][j] + ", ");
              }
              System.out.println(")");
          }
      }    
  }
  </code></pre>
  
  <p>Expected output:
  ( 15, 14, 13, 12, 11, 10, 9, 8, )
  ( 7, 6, 5, 4, 3, 2, 1, 0, )<code>enter code here</code>
  ( -1, -2, -3, -4, -5, -6, -7, -8, )
  ( -9, -10, -11, -12, -13, -14, -15, -16, )</p>
  
  <p>MIPS Assembly so far (I have no idea where to start with the while-for loops):
  Also ignore my incomplete main method lol</p>
  
  <pre><code>#
  #  Author: Joshua Baroni
  #  Date: October 16, 2019
  #  Description: Sorting 2D array in descending order
  #
  .text
  .align 4
  
  main:
  
  
  sort:
  
      la $t4, vals #t0 is number up to outer loop
      la $t1, vals #t1 is number comparing to inner loop
      addi $t1,$t1,4
      la $t8,vals
      add $t8,$t0,$t8
      la $t9,vals
      add $t9,$t0,$t9
      addi $t9,$t9,-4
      loops: lw $t2,($t4) #get number 1 outer loop
      lw $t3,($t1) #get number 2 inner loop
      bgt $t2,$t3, next #don't need to swap
      sw $t3,($t4) #swap
      sw $t2,($t1)
      next: addi $t1,$t1,4
      bgt $t1,$t8,loops #inner loop done?
      addi $t4,$t4,4 #yes-increment outer loop
      move $t1,$t4
      addi $t1,$t1,4
      bgt $t4,$t9,loops #outer loop done?
  
  
  .data
  .align 4
  Input_data:
      .word 2, 0, -7, -1, 3, 8, -4, 10
      .word -9, -16, 15, 13, 1, 4, -3, 14
      .word -8, -10, -15, 6, -13, -5, 9, 12
      .word -11, -14, -6, 11, 5, 7, -2, -12
  Output_data:
      .word 0, 0, 0, 0, 0, 0, 0, 0
      .word 0, 0, 0, 0, 0, 0, 0, 0
      .word 0, 0, 0, 0, 0, 0, 0, 0
      .word 0, 0, 0, 0, 0, 0, 0, 0
  </code></pre>