lw 在 C 或 C++ 中如何表示?

发布于 2024-08-08 17:35:40 字数 218 浏览 2 评论 0原文

那么,例如,会是这样的:

lw $t1, 0($t0)

lw $t2, 8($t0)

翻译为 C 或 C++ 语言?我的意思是我正在将地址中的一个字加载到寄存器中,我明白了。数组是一个类似的概念吗?或者,什么?

提前致谢。

So, for example, what would something like this:

lw $t1, 0($t0)

or

lw $t2, 8($t0)

Translate to in C or C++? I mean I'm loading a word from the address into a register, I get that. Is an array a similar concept, or, what?

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

你没皮卡萌 2024-08-15 17:35:40

这是“加载字”指令。它将 4 字节字从内存中的地址存储在寄存器 $t0 中的位置加载到寄存器 $t1 中。

c/c++ 中没有等效的构造。该指令非常流行,在大多数需要内存访问的结构中使用,例如:

int *p;
// p = ...
*p += 10;

可以翻译为类似的内容(假设 $t0 包含指针“p”)

lw $t1, 0($t0)
addi $t1, $t1, 10
sw $t1, 0($t0)

这里第一条指令将变量加载到寄存器中,第二条指令修改它第三个将其写回内存

This is "load word" instruction. It loads 4-byte word from memory at location which address is stored in register $t0, into register $t1.

There is no equivalent construction in c/c++. This instruction is very popular and used in most constructions where memory access in required, for example:

int *p;
// p = ...
*p += 10;

may be translated to something like (given $t0 contains pointer 'p')

lw $t1, 0($t0)
addi $t1, $t1, 10
sw $t1, 0($t0)

Here the first instruction loads the variable into a register, the second modifies it and the third writes it back into the memory

墟烟 2024-08-15 17:35:40

我认为你无法编写完全相同的代码。您可以执行以下操作:

int* wordAddress = ... + 8; // 8 or any offest
// assuming int is a word on MIPS wich I never worked with personally
int  word = *wordAddress;

您也可以在检索值时应用偏移量:

int* wordAddress = ... + 0;
int  word = *(wordAddress + 8);

有一个注释:您无法在语言中指定所需的register。您可以做的是提示编译器将 word 放入 register 中:

// it is just a hint. The compiler is free to put it in memory.
register int  word = *wordAddress;

I think you can't write exactly similar code. You could do the following:

int* wordAddress = ... + 8; // 8 or any offest
// assuming int is a word on MIPS wich I never worked with personally
int  word = *wordAddress;

You could apply the offset when retrieving the value also:

int* wordAddress = ... + 0;
int  word = *(wordAddress + 8);

There is a note: You can't specify the required register in the language. What you can do is to give a hint to the compiler to put word in a register:

// it is just a hint. The compiler is free to put it in memory.
register int  word = *wordAddress;
-柠檬树下少年和吉他 2024-08-15 17:35:40

对于第一行:

int t1, *t0; ... t1 = *t0t1 = t0[0] 甚至 int t0, t1; ... t1 = t0 。

对于第二个:

int t2 = t0[2],也许,或者int t2 = t0.thirdThing

struct {
   int a,b,thirdThing;
} t0;

但你无法确定。可能是 char *x, **y; x = y[2]; 如果我们了解地址如何进入寄存器,可能会更清楚地了解原始代码。

For the first line:

int t1, *t0; ... t1 = *t0 or t1 = t0[0] or even int t0, t1; ... t1 = t0.

For the second:

int t2 = t0[2], maybe, or perhaps int t2 = t0.thirdThing.

struct {
   int a,b,thirdThing;
} t0;

But you can't know for sure. It could be char *x, **y; x = y[2]; If we saw how the address got into the register it might shed more light on the original code.

心的位置 2024-08-15 17:35:40

假设您使用的是 MIPS32(因此具有 32 位内存寻址),那么它们所做的事情非常简单。

lw $t1, 0($t0)

其作用是将内存地址 t0 字节偏移 0 处的值加载到 t1 寄存器中。

lw $t2, 8($t0)

其作用是将内存地址 t0 字节偏移 8 处的值加载到 t2 寄存器中。

假设您有一个内存地址 0x12345678。那么 MIPS 程序集本质上会执行以下操作:

int t0 = 0x12345678;
// ...
int t1 = *(int*)(t0 + 0);
int t2 = *(int*)(t0 + 8);

Assuming you are using MIPS32 (and hence have 32-bit memory addressing) then its pretty easy what they do.

lw $t1, 0($t0)

What this does is load the value at byte offset 0 from memory address t0 into the t1 register.

lw $t2, 8($t0)

What this does is load the value at byte offset 8 from memory address t0 into the t2 register.

Lets assume you have a memory address 0x12345678. Then the MIPS assembly is, essentially, doing the following:

int t0 = 0x12345678;
// ...
int t1 = *(int*)(t0 + 0);
int t2 = *(int*)(t0 + 8);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文