简单的MIPS问题,关于加载字节

发布于 2024-09-29 13:38:51 字数 343 浏览 0 评论 0原文

我这里有以下问题

.data
a: .asciiz "2021"
x: .byte 7,2,12
.text
main: addi $t2, $0, 1
lb $t3, a($t2)

有人可以向我解释一下 $t3 的值是 48 吗?

谢谢 编辑这是另一个类似的问题,并且令人困惑。

.data
a: .word 12,-5,4,0
x: .byte 5
.text
main: addi $t1, $0, 8
lw $t2, a($0)
lw $t3, a($t1)

当“a”的长度为 4 时,如何从索引 8 加载单词?

I have the following question here

.data
a: .asciiz "2021"
x: .byte 7,2,12
.text
main: addi $t2, $0, 1
lb $t3, a($t2)

Can someone explain to me, HOW the value of $t3 is 48?

thanks
EDIT this is an another question which is similiar, and is confusing.

.data
a: .word 12,-5,4,0
x: .byte 5
.text
main: addi $t1, $0, 8
lw $t2, a($0)
lw $t3, a($t1)

How will you load word, from index 8, when 'a' has a length of 4?

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

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

发布评论

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

评论(2

‖放下 2024-10-06 13:38:52

是的,当您添加 $01 时,您会得到 1,它被放入 $t2 中。

然后,当您计算 a($t2) 时,这是 a 的第二个字节(偏移量 1,因为它基于偏移量 0),即“0”,ASCII 代码0x3048


从各种信息来看:

ADDI -- Add immediate (with overflow)
Description:
    Adds a register and a sign-extended immediate value
    and stores the result in a register
Operation:
    $t = $s + imm; advance_pc (4);
Syntax:
    addi $t, $s, imm

LB -- Load byte
Description:
    A byte is loaded into a register from the specified address.
Operation:
    $t = MEM[$s + offset]; advance_pc (4);
Syntax:
    lb $t, offset($s)

寄存器$0始终包含硬连线值0。MIPS已经建立了一组关于如何使用寄存器的约定。这些建议只是指导方针,并非由硬件强制执行。然而,违反这些规定的程序将无法与其他软件一起正常工作。

这些小片段应该足以解释它在做什么。


而且,关于您的编辑,您错误地认为 .word 12,-5,4,0 的长度为 4 个字节。事实上,它的长度为 16 字节,因为 MIPS 中的字为 32 位(四个字节)宽。

因此,当您从偏移量 8 的字节加载时,您将获得单词 4

Yes, when you add $0 and 1, you get 1, which is put into $t2.

Then, when you evaluate a($t2), that's the second byte (offset 1 since it's based at offset 0) of a which is the "0", ASCII code 0x30 or 48.


From various pieces of information:

ADDI -- Add immediate (with overflow)
Description:
    Adds a register and a sign-extended immediate value
    and stores the result in a register
Operation:
    $t = $s + imm; advance_pc (4);
Syntax:
    addi $t, $s, imm

LB -- Load byte
Description:
    A byte is loaded into a register from the specified address.
Operation:
    $t = MEM[$s + offset]; advance_pc (4);
Syntax:
    lb $t, offset($s)

Register $0 always contains the hardwired value 0. MIPS has established a set of conventions as to how registers should be used. These suggestions are guidelines, which are not enforced by the hardware. However a program that violates them will not work properly with other software.

Those little snippets should hopefully be enough to explain what it's doing.


And, regarding your edit, you're incorrectly thinking that .word 12,-5,4,0 has a length of 4 bytes. In fact it has a length of 16 bytes since words in MIPS are 32 bits (four bytes) wide.

So when you load from byte offset 8, you will get the word 4.

一个人的夜不怕黑 2024-10-06 13:38:52

仅供参考,讲义 4 的第 11 页上有一个 ASCII 图表;)
但我不明白这个:
“当你评估 a($t2) 时,这是 a 的第二个字节(偏移量 1,因为它基于偏移量 0),即“0””
我认为“2021”的二进制表示是:
00110010001100000011001000110001
那么,当你说“哪个是“0””时,你的意思是从右边算起第二位吗?是你说的0吗?
我不明白零从何而来。

FYI there is an ASCII chart on page 11 of handout 4 ;)
But I dont get this:
"when, when you evaluate a($t2), that's the second byte (offset 1 since it's based at offset 0) of a which is the "0""
The binary representation of '2021' I believe is:
00110010001100000011001000110001
So, when you say "which is the "0"" do you mean the 2nd bit from the right? Is that the 0 you are talking about?
I dont get where the zero comes from.

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