user2259082
user2259082

Reputation: 29

converting C code to MIPS Assembly Language with arrays

Ok, so I have to convert the following C code segment to MIPS Assembly.

f = k + A[5]

The question tells me that f is stored in register $s3, k is in $s2 and the base address of array A is $s4. This is what I put as my answer:

add $s3, $s2, $s4

Is this correct? Do I have to do anything special with the 5 in the array? I'm very new to MIPS, so any and all help if VERY much appreciated.

Upvotes: 2

Views: 2606

Answers (2)

Mercurybullet
Mercurybullet

Reputation: 889

Are you working on this for homework? If so, are you actually writing out an executable program or just responding to a list of questions?

Either way yes, you do need to account for the 5 in the array. The question is telling you that $s4 points to the base address of the array, not the 5th index.

hint: A[0] would be at the same address as the base of the array.

Upvotes: 1

wazy
wazy

Reputation: 1065

Try this out. (Off the top of my head). Remember each index is * 4.

li $t2, 6            # init 6 to $t2
addi $t2, $t2, $t2   # $t2 * 2
addi $t2, $t2, $t2   # $t2 * 2
addi $t1, $t2, $s4   # A[6 * 4] 
lw $t4, 0($t1)       # load A[6] int $t4
addi $s3, $s2, $t4   # obtain f

Upvotes: 0

Related Questions