MIPS 程序集加载和存储内存地址

MIPS assembly loading and storing memory address

我正在学习 MIPS 汇编并且想知道如何在 MIPS 中加载和存储内存地址。

例如假设我有这个 C 代码:

int i;

i = 0xFFFF0000;

然后我将如何存储地址?例如我想将 i 的值存储到地址 0x2000A000.

LUI V0, 0xFFFF         << load 0xffff0000 into v0
LUI A0, 0x2000
ORI A0, A0, 0xA000     << load address to A0
SW V0, (A0)            << Stores V0 at address held in A0

或者,如果您的汇编程序支持宏操作码(我想现在几乎所有人都支持)

LUI V0, 0xFFFF
LI A0, 0x2000A0000
SW V0, (A0) 

注意 A0 是 WORD/32bit 对齐的。

让编译器告诉你:

#define SOME_ADDRESS (*((volatile unsigned int *) 0x2000A000  ))
void fun ( void )
{
    SOME_ADDRESS = 0xFFFF0000;
}

编译反汇编

Disassembly of section .text:

00000000 <fun>:
   0:   3c022000    lui ,0x2000
   4:   3442a000    ori ,,0xa000
   8:   3c03ffff    lui ,0xffff
   c:   ac430000    sw  ,0()
  10:   03e00008    jr  
  14:   00000000    nop

正如评论中指出的那样,有一个优化遗漏,但这不是这个答案的重点。 1) 学习 asm 2) 学习优化 asm...在未来。使用编译器,您将看到其中一些优化。但在这种情况下不是。

#define SOME_ADDRESS (*((volatile unsigned int *) 0x20004000  ))
void fun ( void )
{
    SOME_ADDRESS = 0xFFFF0000;
}

00000000 <fun>:
   0:   3c022000    lui ,0x2000
   4:   3c03ffff    lui ,0xffff
   8:   ac434000    sw  ,16384()
   c:   03e00008    jr  
  10:   00000000    nop

是的,它是为 mips32 构建的,目的是让 -march=mips1 和 -march=mips32r6 产生相同的代码。如评论所述。