简体   繁体   English

使用数组将C代码转换为MIPS汇编语言

[英]converting C code to MIPS Assembly Language with arrays

Ok, so I have to convert the following C code segment to MIPS Assembly. 好的,所以我必须将以下C代码段转换为MIPS Assembly。

f = k + A[5] 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. 这个问题告诉我,f存储在寄存器$ s3中,k存储在$ s2中,数组A的基地址为$ s4。 This is what I put as my answer: 这就是我的回答:

add $s3, $s2, $s4 加$ s3,$ s2,$ s4

Is this correct? 这个对吗? Do I have to do anything special with the 5 in the array? 我必须对阵列中的5做些特别的事情吗? I'm very new to MIPS, so any and all help if VERY much appreciated. 我是MIPS的新手,因此非常感谢任何帮助。

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. 无论哪种方式,您都需要考虑数组中的5。 The question is telling you that $s4 points to the base address of the array, not the 5th index. 问题是告诉您$s4指向数组的基地址 ,而不是第5个索引。

hint: A[0] would be at the same address as the base of the array. 提示:A [0]与数组的基址在同一地址。

Try this out. 试试看 (Off the top of my head). (在我的头顶)。 Remember each index is * 4. 记住每个索引是* 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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