Reputation: 87
I am trying to translate A[2*i]=A[2*k+j];
into assembly language. Array 'A' of ints (four bytes) starts at address Astart
(32 bit) and i, j, k
are stored in $s0, $s1, $s3
.
So if I am understanding the problem correctly, I need to set the address and value of the [2*k+j]
element to the [2*i]
element? My confusion is modifying the addresses. This is what I have:
add $t0, $s0, $s0 #2*i
add $t1, $s3, $s3 #2*k
add $t2, $t1, $s3 #2*k+j
sw $t2, Astart
sw $t0 4(Astart)
Is this correct?
Upvotes: 1
Views: 1110
Reputation: 4961
There are two steps necessary here, first to load from A[2*k+j]
and second to store at A[2*i]
.
You have already calculated 2*i
and 2*k+j
, but you have forgotten to multiply by 4 (because the offset of each element of a 32 bit integer is 4 bytes).
The corrected code might look like:
add $t0, $s0, $s0 #2*i
add $t1, $s3, $s3 #2*k
add $t2, $t1, $s3 #2*k+j
sll $t1 $t1 2
sll $t2 $t2 2
la $t0 Astart
add $t3 $t0 $t1 #Address of A[2*k]
add $t4 $t0 $t2 #Address of A[2*k+j]
lw $t0 0($t3)
sw $t0 0($t4)
Upvotes: 1