简体   繁体   English

MIPS。 C语言到MIPS汇编语言代码

[英]MIPS. C language to MIPS assembly language code

Assuming that the values of variables f, g, h, i, and j are stored in registers $s0, $s1, $s2, $s3, and $s4, respectively. 假设变量f,g,h,i和j的值分别存储在寄存器$ s0,$ s1,$ s2,$ s3和$ s4中。 Furthermore the base address of arrays of integers A and B are in registers $s6 and $s7 respectively. 此外,整数A和B的数组的基地址分别在寄存器$ s6和$ s7中。

Write MIPS assembly language code for following C language statements: f = g - A[B[4] + 2] 为以下C语言语句编写MIPS汇编语言代码:f = g-A [B [4] + 2]

lw $s0, 16($s7)     #$s0 = B[4]
sll $s0, $s0, 2     #$s0 = B[4] * 4 
add $s0, $s6, $s0   #$s0 = A + B[4] * 4
lw $s0, 0($s0)      #$s0 = A[B[4]]
sub $s0, $s1, $s0   #f = g – A[B[4]]

I found the above example online but I don't understand how it really works. 我在网上找到了上面的示例,但我不知道它是如何工作的。 Second line, isn't it shift logical left? 第二行,这不是逻辑左移吗? So why is it used like this in here, doing operation $s= B[4] * 4 instead of adding 2? 那么,为什么在这里这样使用它,执行操作$ s = B [4] * 4而不是加2? Third line, why is it adding instead of doing something like first line? 第三行,为什么要添加而不是执行第一行之类的操作? Fourth line, 0($s0)... meaning we are getting the index 0? 第四行,0($ s0)...表示我们正在获取索引0? Why? 为什么?

I was have f = h + B[g] and f = g + A[h + B[1]]. 我有f = h + B [g]和f = g + A [h + B [1]]。 I'm sorry if this is too much question but I just don't get it. 如果这个问题太多了,我很抱歉,但我不明白。

Second line, isn't it shift logical left? 第二行,这不是逻辑左移吗? So why is it used like this in here, doing operation $s= B[4] * 4 instead of adding 2? 那么,为什么在这里这样使用它,执行操作$ s = B [4] * 4而不是加2?

Performing a logical shift left by 2 bits multiplies a value by 4. While there isn't enough context here for me to say for sure, it's most likely that this is necessary because A is an array of 32-bit values, which are 4 bytes each. 执行逻辑左移2位将值乘以4。虽然这里没有足够的上下文让我确定,但是很有可能这是必需的,因为A是32位值的数组,即4每个字节。 Thus, B[4] must be multiplied by 4 to turn it from an index into an offset within A . 因此, B[4]必须乘以4才能将其从索引转换为A内的偏移量。

Fourth line, 0($s0)... meaning we are getting the index 0? 第四行,0($ s0)...表示我们正在获取索引0? Why? 为什么?

Because, at this point, $s0 is the address of A[B[4]] — in C terminology, it is the pointer &A[B[4]] . 因为在这一点上$s0A[B[4]]的地址,所以它是指针&A[B[4]] Loading the word at an offset of 0 from that address dereferences the pointer. 将该字加载到与该地址的偏移量为0的位置会取消引用指针。

The sample code you're using appears to be missing the + 2 . 您正在使用的示例代码似乎缺少+ 2 Adding that should be possible by modifying one instruction from what you have; 通过修改现有指令中的一条,应该可以添加该指令; I'll leave it to you to figure out what. 我留给你弄清楚是什么。

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

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