简体   繁体   English

sll 的目的是什么,并在翻译后的 MIPS 代码中添加?

[英]What is the purpose of sll and add in the translated MIPS code?

I was reading some MIPS notes regarding translating C statement to MIPS.我正在阅读一些关于将 C 语句翻译为 MIPS 的 MIPS 注释。

C code of swap function below: C 交换代码 function 如下:

swap ( int v[], int k) {
    int temp;
    temp = v[k];
    v[k] = v[k + 1];
    v[k + 1] = temp;
}

I was told that k maps to $5, base address of v[ ] is mapped to $4 and temp is mapped to $15.有人告诉我 k 映射到 $5,v[ ] 的基地址映射到 $4,temp 映射到 $15。 The swap function takes in the arguments k = 3 and assume that base address of v is 2000.交换 function 采用 arguments k = 3 并假设 v 的基地址为 2000。

Simplified MIPS version below:简化的 MIPS 版本如下:

swap:

    sll $2, $5, 2
    add $2, $4, $2
    lw  $15, 0($2)
    lw  $16, 4($2)
    sw  $16, 0($2)
    sw  $15, 4($2)

The confusion here is why is there sll and add in the translated MIPS?这里的困惑是为什么翻译后的 MIPS 中有slladd

This comes from being byte addressable , as most modern machines are.这来自字节可寻址,就像大多数现代机器一样。

Byte addressing means that the processor can access each individual byte of memory — in such a system, incrementing a valid byte address value by 1 means referring to the next sequential byte of memory.字节寻址意味着处理器可以访问 memory 的每个单独字节——在这样的系统中,将有效字节地址值递增 1 意味着引用 memory 的下一个连续字节。

Words, used by the int datatype, are 32-bits wide, so require 4 bytes of memory. int数据类型使用的字是 32 位宽,因此需要 4 个字节的 memory。 Thus, a[0] occupies bytes addr+0 , addr+1 , addr+2 , and addr+3 , where addr is a byte address of a .因此, a[0]占用字节addr+0addr+1addr+2addr+3 ,其中addra的字节地址。 a[1] is offset from a[0] by 4 bytes, at addr+4 ! a[1]a[0]偏移 4 个字节,在addr+4

C knows that the elements of "arrays of int " require 4 bytes each so 4 bytes separation, and thus it knows that a+i under the hood means a + i * 4 , which will refer to 4 consecutive addresses for each element of the array, starting at i*4 , which leaves room for all the lower elements. C 知道“ int数组”的元素每个需要 4 个字节,因此需要 4 个字节分隔,因此它知道引擎盖下的 a+ a+i意味着a + i * 4 ,这将引用每个元素的 4 个连续地址数组,从i*4开始,为所有较低的元素留出空间。

The computation i * 4 is sometimes called "scaling".计算i * 4有时称为“缩放”。 Whereas in C we can refer to the index position directly a[i] , in assembly we have to scale the index explicitly.而在 C 我们可以直接引用索引 position a[i] ,在汇编中我们必须明确地缩放索引。

The sll is an efficient way to multiply by 4 — the constant 2 in the sll means 2 digits to the left (aka: * 100 2 , ie * 100 in binary). sll是乘以 4 的有效方法—— sll中的常数 2 表示向左 2 位(又名: * 100 2 ,即 * 100 二进制)。

The add sums the array base with the scaled index to make the actual address of the desired word for the array reference. add 将数组基数与缩放索引相加,以生成数组引用所需字的实际地址。

We can also see the generated code using constants 0 or 4. Because these constants are also scaled, the 4 refers to the next element of the array beyond what was computed.我们还可以看到使用常量 0 或 4 生成的代码。因为这些常量也是按比例缩放的,所以 4 指的是数组中超出计算值的下一个元素。 Which is to say that if 0($2) refers to a[k] then 4($2) refers to a[k+1] .也就是说,如果0($2)指的是a[k]那么4($2)指的是a[k+1]

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

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