如何在不使用伪指令的情况下加载内存地址?
我正在尝试使用MARS模拟器自学MIPS汇编语言。
出于教学原因,我限制自己不使用伪指令。
在尝试将某些数据的地址存入寄存器时,我遇到了问题,因为我无法使用la
。
我尝试将 lui
与 ori
结合使用,就像我直接加载数字一样,但无济于事:
.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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
要回答修改后的问题“在没有伪指令的情况下专门使用 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.
您需要参考 lui 和 ori 指令中数据部分的标签。这适用于 gnu 汇编器(as):
%hi
和%lo
指令告诉链接器发生了什么,以便它可以放置标签“lab1”的地址” 在机器代码中。使用
lui
;addiu
而不是lui
;ori
与%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):
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 thanlui
;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 likeaddiu
, unlikeori
.您的 ori 指令还需要另一个操作数才能工作,据我查看您的代码,“mem”不是现有标签。
试试这个:
如果它不起作用,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:
If it doesn't work, MARS likely won't be able to get label content without pseudo instructions.