title: MIPS assembly sorting
body: <p>I've been tasked with sorting and printing a hardcoded integer array using MIPS assembly and QTSpim. I've been trying to follow the other similar questions posted here but I am struggling to understand where I am now going wrong. I'm getting &quot;Exception occurred at PC=0x00400068
  Bad address in data/stack read: 0x10040000&quot;
  QTSpim highlights &quot;[80000180] 0001d821  addu $27, $0, $1         ; 90: move $k1 $at # Save $at&quot;
  I'm fairly new at assembly, so any help is much appreciated!</p>
  <pre><code>.data
  myarray: .word 4, 1, 3, 2, 16, 9, 10, 14, 8, 7
  arraysize: .word 10
  str1: .asciiz &quot;Unsorted array: &quot;
  str2: .asciiz &quot;\nSorted array: &quot;
  nline: .asciiz &quot;\n&quot;
  
  .text
  .globl main
  
  main:
      li      $v0, 0
      la      $t1, myarray #Coping base address of array into $t1
  loop1: #Printing unsorted array
      bge $t0, 10, sortFlag
      lw      $t2, 0($t1) # loading word from addrs, then goes to the next addrs
      addi    $t1, $t1, 4
  
      li      $v0, 1 # Syscall to print value 
      move    $a0, $t2
      syscall
      li      $a0, 32 # Syscall number for printing space
      li      $v0, 11  
      syscall
  
      addi    $t0, $t0, 1 # Incrementing counter
      j      loop1
  sortFlag:
      add $t6, $0, $0 # t6 holds flag to determine whether the list is sorted
      la  $a1, myarray # Sets $a1 to base address of array
  sort:
      lw  $t2, 0($a1) #Sets $t0 to current element in array
      lw  $t3, 4($a1) #Sets $t1 to next element in array
      slt $t5, $t2, $t3 # Sets $t5 if $t0 &lt; $t1
      beq $t5, $0, swap #Swaps if $t5 = 1
      add $t6, $0, 1 #Check list again if swapped
      sw  $t2, 4($a1) #Storing greater numbers in higher position in the array
      sw  $t3, 0($a1) #Storing lesser numbers in lower position in the array
  swap:
      addi $a1, $a1, 4 #Moves to next location in array
      bne  $a1, $t6, sort #If $a0 doesn't equal the end of the array, jump to sort
      bne  $t6, $0, sortFlag #If $t6 = 1, jump to sortFlag
  loop2: #Repeated from above to re-print the now sorted array
      bge     $t7, 10, exit
  
      lw      $t4, 0($t1)
      addi    $t1, $t1, 4
  
      # syscall to print value
      li      $v0, 1      
      move    $a2 $t4
      syscall
  
      li      $a2, 32
      li      $v0, 11  
      syscall
  
      addi    $t7, $t7, 1
      j      loop2
   exit:
      li      $v0, 10
      syscall
  </code></pre>