如何在不使用伪指令的情况下加载内存地址?

发布于 2024-12-02 07:00:10 字数 532 浏览 2 评论 0原文

我正在尝试使用MARS模拟器自学MIPS汇编语言。

出于教学原因,我限制自己不使用伪指令。

在尝试将某些数据的地址存入寄存器时,我遇到了问题,因为我无法使用la

我尝试将 luiori 结合使用,就像我直接加载数字一样,但无济于事:

  .data
arr:
  .byte 0xa1
  .byte 0xb2
  .byte 0xc3
  .byte 0xd4
  .byte 0xe5
  .byte 0xf6
  .byte 0x7a
  .byte 0x8b
  .byte 0x9c
  .byte 0xad

.text

  lui $s0, mem # <--- mars just gives me errors here :(
  ori $s0, mem # ?? ... 

这是否可以使用 专门的 MARS 来实现,而不需要伪指令?如何?

提前致谢!

I'm trying to learn MIPS assembly language by myself using MARS simulator.

For didactic reasons I'm limiting myself to not using pseudo-instructions.

While trying to get the address of some data into a register, I ran into a problem because I cannot use la.

I tried using lui in combination with ori, the same as if I was to load a number directly, to no avail:

  .data
arr:
  .byte 0xa1
  .byte 0xb2
  .byte 0xc3
  .byte 0xd4
  .byte 0xe5
  .byte 0xf6
  .byte 0x7a
  .byte 0x8b
  .byte 0x9c
  .byte 0xad

.text

  lui $s0, mem # <--- mars just gives me errors here :(
  ori $s0, mem # ?? ... 

Is this doable using specifically MARS, without pseudo-instructions? How?

Thanks in advance!

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

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

发布评论

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

评论(3

梦一生花开无言 2024-12-09 07:00:10

要回答修改后的问题“在没有伪指令的情况下专门使用 MARS 是否可行?”:通过快速浏览 MARS 文档,似乎不行。 MARS 似乎有意限制用于教学目的。

如果您想在完整的 MIPS 模拟器上尝试此操作,该模拟器将模拟在 MIPS 上运行的 Linux 操作系统并运行使用 gnu 工具链构建的代码,请查看 OVP 模拟器。这是免费的,可以在 Linux 和 Windows 上运行,但它可能比您需要的多很多。

To answer the modified question "is this doable using specifically MARS, without pseudo-instructions?": From a quick scan of the MARS documentation, it appears not. MARS appears to be intentionally restricted for pedagogical purposes.

If you want to try this on a full MIPS simulator that will simulate the Linux OS running on MIPS and run code built with the gnu toolchain, take a look at the OVP Simulator. This is free and runs on Linux and Windows, but it's probably a lot more than you need.

救赎№ 2024-12-09 07:00:10

您需要参考 lui 和 ori 指令中数据部分的标签。这适用于 gnu 汇编器(as):

    .data
lab1: .byte 0xa1
...
.text
    lui   $s0,      %hi(lab1)
    addiu $s0, $s0, %lo(lab1)
    lb    $s2, 0($s0)
...

%hi%lo 指令告诉链接器发生了什么,以便它可以放置标签“lab1”的地址” 在机器代码中。

使用luiaddiu 而不是 luiori%hi/%lo%hi 的扩展取决于 %lo如果设置了其高位,则将 code> 视为负数。

这允许 lui $t0, %hi(sym)lb $t1, %lo(sym)($t0) 使用 %lo 作为加载中的直接偏移量,无需单独的指令来具体化完整地址在寄存器中。加载和存储将它们的立即数视为有符号,如 addiu,与 ori 不同。

You need to refer to a label in the data section in the lui and ori instructions. This works for gnu assembler (as):

    .data
lab1: .byte 0xa1
...
.text
    lui   $s0,      %hi(lab1)
    addiu $s0, $s0, %lo(lab1)
    lb    $s2, 0($s0)
...

The %hi and %lo directives tell the linker what is going on, so that it can put the address of the label "lab1" in the machine code.

Use lui ; addiu rather than lui ; ori with %hi/%lo: the expansion of %hi depends on %lo being treated as negative if its high bit is set.

This allows lui $t0, %hi(sym) ; lb $t1, %lo(sym)($t0) to work using the %lo as the immediate offset in the load, without a separate instruction to materialize the full address in a register. Loads and stores treat their immediate as signed like addiu, unlike ori.

嗫嚅 2024-12-09 07:00:10

您的 ori 指令还需要另一个操作数才能工作,据我查看您的代码,“mem”不是现有标签。
试试这个:

.data 0x10000000 #or choose any other location
        #pointer section
        .word arr
        #...

        #pointed section
arr:    .byte #...  only as tip, you can separate multiple values with comma
              #behind .byte so that you don't need multiple .byte directives 
        #...

.text
        #...
        lui $s0, 0x1000
        lw $t0, 0($s0)           #get the value of "arr"
        #...

如果它不起作用,MARS 可能无法在没有伪指令的情况下获取标签内容。

Your ori instructions needs still another operand to work and as far as I looked over your code, "mem" is no existing label.
Try this one:

.data 0x10000000 #or choose any other location
        #pointer section
        .word arr
        #...

        #pointed section
arr:    .byte #...  only as tip, you can separate multiple values with comma
              #behind .byte so that you don't need multiple .byte directives 
        #...

.text
        #...
        lui $s0, 0x1000
        lw $t0, 0($s0)           #get the value of "arr"
        #...

If it doesn't work, MARS likely won't be able to get label content without pseudo instructions.

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