简体   繁体   中英

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.

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM