title: Mips 2d array function
body: <p>I am attempting to create an algorithm that finds the trace of an n-by-n square matrix A.the trace of an n-by-n square matrix A is defined to be the sum of the elements on the main diagonal (the diagonal from the upper left to the lower right) of A.The main idea involved is that at this level multi-dimensional arrays are stored as one-dimensional arrays, and the multi-dimensional indexing (for a matrix with m rows and n columns) is converted to one-dimensional indexing.As I'am unfamiliar with mips attempts to integrate it into code are unsuccessful my latest attempt below.</p>
  
  <p>I have set the registers to the following:</p>
  
  <p>$a0 = base address of array (matrix), a</p>
  
  <p>$a1 = n, number of rows and columns</p>
  
  <p>$v0 = trace </p>
  
  <p>$t0 = i</p>
  
  <pre><code>trace:   move $v0, $zero       
           move $t0, $zero       
           bne $t0,$a1,end      
           sll $t1,$a0,4
           add $t1,$t1,$t0
           sll $t1,$t1,2
           add $t2,$t1,$a0
           lw $t0,0($t1)
           addi $sp, $sp, 8
           sw $t1,0($t0)
           j trace
  end:     jr $ra       
  </code></pre>
  
  <p>but to no avail the answer does not come out as desired the format of the algorithm should be as follows;</p>
  
  <pre><code> trace = 0
   for i = 0 to n-1
     trace = trace + a[i,i]
   end for
  </code></pre>