title: sort array in asssembly code and stack pointer
body: <p>i made program , for sorting array and print original position of the number in array 
  for example <strong>10,5,3,15,21,14</strong>
  for <em>3rd</em> position will return <strong>14</strong> and <strong>6</strong> th place (number <strong>14</strong> position in original array)</p>
  
  <p>Im trying to sort array of six using <code>bubble sort</code> in assembly
  the array can be sorted correctly but Im having trouble with stack pointer
  i use stack pointer to store the original array, as i need to know the original position before sorting. but when I tried to load lw <strong>$t0,0($sp)</strong> in loops function, <strong>$t0</strong> give <strong>0</strong>,what is wrong <strong>?</strong> and how can I access each element in stack pointer
  however, i can correctly load the $sp, after i changed 
  lw $t0,-24($sp) to this in loop function, why cant i retrieve value using 0($sp)?</p>
  
  <pre><code>loops: ##this part causing trouble
  lw $t0,0($sp) ##but when jump to this, the stack pointer give 0 in $t0 why?
  addi $sp,$sp,4
  addi $t7,$t7,1
  beq $t8,$t0,koko
  j loops
  
  
  
  
  
  
  
  .text
  .globl main
  
  
  main:
  la $t1,array ##copy base address of array to t1
  lw $t0,num ##copy num to t0
  lw $s3, num ##stop =6
  ##lw $t2,0($t1)
  
  li $v0,4
  la $a0,prompt
  syscall
  
  
  li $v0,5
  syscall
  move $s0,$v0 ##user input
  
  
  addi $t6,$0,0
  sub $t9,$s3,$s0 ## 6-3
  addi $t3,$t3,1
  
  addiu $sp,$sp,-32 ## here im made stack,
  jal shokika
  j loops ##go to here after return from end:
  
  shokika:  ##use to store original array
  lw $t2,0($t1)
  bge $t6,6,outer
  sw $t2,0($sp)
  addi $t1,$t1,4
  addi $sp,$sp,4
  addi $t6,$t6,1
  j shokika
  
  loops:
  lw $t0,0($sp) ##but when jump to this, the stack pointer give 0 in $t0 why?
  addi $sp,$sp,4
  addi $t7,$t7,1
  beq $t8,$t0,koko
  j loops
  
  outer:
  la $t1,array
  la $sp,0($sp)
  addi $s3,$s3,-1 ##5
  ble $s3,$zero,end
  li $s0,0 ##inner loop counter
  li $s1,0 ##outer counter
  
  
  inner:
  bge $s0,$s3,inner_end
  lw $t3,0($t1) ##cur elem
  lw $t4,4($t1) ## next el
  addi $s0,$s0,1
  ble $t4,$t3, swap
  addi $t1,$t1,4
  
  j inner
  
  inner_end:
  la $t1,array
  j outer
  
  swap:
  sw $t3,4($t1)
  sw $t4,0($t1)
  addi $t1,$t1,4
  j inner
  
  
  end:
  addi $t5,$0,4
  mul $t5,$t5,$t9 ##4*3
  add $t9,$t5,$t1 ##t1 base address arr
  lw $t8,0($t9) ##sort
  jr $ra
  
  koko:
  
  li $v0,4
  la $a0,result
  syscall
  
  li $v0,1
  move $a0,$t7 ##position
  syscall
  
  li $v0,4
  la $a0,sort
  syscall
  
  li $v0,1
  move $a0,$t8 ##psort
  syscall
  
  li $v0, 10
  syscall
  
  
  .data 
  array:
  .word 10,5,3,15,21,14
  array2:
  .space 400
  num:
  .word 6
  sort: .asciiz "$s2-&gt;"
  result: .asciiz "$s1-&gt;"
  prompt: .asciiz "$s0-&gt;"
  </code></pre>