Carson
Carson

Reputation: 1257

Using arrays in MIPs

Could someone help walk me through what some lines in this MIPs code are doing?

The C code is B[8] = A[i - j] with i = $s3, j = $s4, &A[] = $s6, and &B[] = $s7

The MIPs code is as follows...

sub $t0, $s3, $s4  # i - j
sll $t0, $t0, 2 #Gets the offset of 8 for B[]
add $t0, $s6, $t0 #Goes to the offset in B[] ?
lw $t1, 0($t0) #????
sw $t1, 32($s7) #????

I get a bit lost once it gets to the last 3 lines.

Why is it 0($t0) and 32($s7)? Or just why 0 and 32?

Upvotes: 0

Views: 370

Answers (1)

Bran
Bran

Reputation: 647

sll $t0, $t0, 2   // This multiplies (i-j) * 4, not 8. Because the indexes are 4-byte ints
add $t0, $s6, $t0 // $t0 = A + (i-j)*4. So $t0 = &A[i-j]
lw $t1, 0($t0)     // $t1 = A[i-j]
sw $t1, 32($s7)   // B[32/4] = $t1

32($s7) means $s7 + 32. The reason you add 32 because you are accessing the 8th element of an integer array, which is located in the memory address B + 8*sizeof(int) = B + 32

Upvotes: 1

Related Questions