Pages

Saturday, November 6, 2010

Array Operation in MIPS

Array

base address of the array A is $s1, the index of array A is k,
lw: A[k] = 4k ( $s1)
lb: A[k] = k ( $s1)


1.  
e.g.
A [12] = h + A[8]  ;  $r2 = h, base address of the array A is in $s3
lw $t0, 32($s3)
add $t1, $r2, $t0
sw $t1, 48($s3)

2. 
e.g.
g = h + A[k] ;   $s1 = g; $s2 = h;  $s3 = base address of array A;  $s4 = k
add $t0, $s4, $s4   # $t0 = k + k = 2k
add $t0, $t0, $t0     # $t0 = $t0 + $t0 = 2k + 2k = 4k
add $t1, $s3, t0 # $t1 = memory address of A[0] + 4k
lw $t1,  0($t1)     # $t1 = A[k]
add $s1, $s2, $t1   # $s1 = g + A[k]

No comments:

Post a Comment