assembly - Best practices for Indexing an array in MIPS -
when base address of array stored in register, understanding 1 not want adjust register. trying understand when principle valid. valid practice when dealing saved registers or saved registers , argument registers, too?
below example working involves converting simple c code mips instruction. attempted solution in 2 ways.
in first solution, increment registers holding base address of array , array b. thoughts given base address registers, $a0 , $a1, argument registers, not saved registers. thought, perhaps, wouldn't matter adjusted. in second solution, stored base address of each array temporary registers , manipulated instead.
i did find solutions online particular example, i'm trying understand basis each instruction, rather following procedural solution. advice on best mips practice appreciated!
# c code: (i=0; i<=100; i=i+1)  {      a[i] = b[i] + c;  }  # $a0 = base address of array # $a1 = base address of array b # $s0 = c  # attempted solution # 1  li      $t0, 0                  # t0 = 0 li      $t1, 101                # t1 = 100  loop: lw      $t2, 0($a1)             # t2 = b[i] add     $t3, $t2, $s0           # t3 = b[i] + c  sw      $t3, 0($a0)             # a[i] = b[i] + c addi    $a0, $a0, 4             # a0 = address of a[i+1] add     $a1, $a1, 4             # a1 = address of b[i+1] addi    $t0, 1                  # t0 = i++ bne     $t0, $t1, loop          # if t0 != 101, loop again.  # attempted solution # 2:  li      $t0, 0                  # t0 = 0 li      $t1, 101                # t1 = 100 add     $t2, $a0, $zero         # t2 = adress of a[0] add     $t3, $a1, $zero         # t3 = adress of b[0]  loop: lw      $t4, 0($t3)             # t4 = b[i] add     $t4, $t4, $s0           # t4 = b[i] + c  sw      $t4, 0(t2)              # a[i] = b[i] + c  addi    $t2, $t2, 4             # t2 = address of a[i+1] addi    $t3, $t3, 4             # t3 = address of b[i+1] bne     $t0, $t1, loop          # if t0 != 101, loop again.      
if converting c code assembly (i.e. manually compiling) have make sure no information lost.
if "copy" of base address of arrays , b stored in $a0 , $a1 , them after loop should preserve them. if addresses preserved elsewhere may modify them if not going use them later in routine.
for example, suppose c code else arrays or b after loop, e.g:
for (i=0; i<=100; i=i+1)  {      a[i] = b[i] + c;  }  c = a[0]   then either preserve base address of array or recalculate after loop.
Comments
Post a Comment