MIPS 汇编 - 标签值修改

发布于 2024-09-30 00:56:02 字数 200 浏览 8 评论 0原文

MIPS 中是否可以在执行期间更改标签的值,或者创建具有特定值的标签?

我问这个问题是因为当使用指令lw $a0, label($s0)时,我想每次循环时增加标签+4的值,指示数组的新内存地址。我知道我可以执行 lw $a0, label+4($s0) 但不会存储 label 的新值。

有什么建议吗?

Is it possible in MIPS to change during execution the value of a label, or to create a label with certain value?

I ask this because when using the instruction lw $a0, label($s0) i want to increment the value of label +4 every time we loop, indicating the new memory address of the array. I am aware I can do lw $a0, label+4($s0) but the new value of label will not be stored.

Any advise?

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

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

发布评论

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

评论(2

随遇而安 2024-10-07 00:56:02

不可以。在 MIPS 中,取消引用时,括号外必须有一个常量(措辞不当)。如果可以改变标签的值,那么它就不再是恒定的。为了解决这个问题,你可以做类似的事情,

la $t1, label          #t1 holds address of label
add $t1, $t1, $s0      #t1 now holds address of label + s0 offset
lw $a0, 0($t1)         #load word from t1's location

addi $t1, $t1, 4       #t1 was incremented by 4 bytes now
lw $a0, 0($t1)         #load the next word

如果 s0 总是非负的,建议使用 addu 。

编辑:您无法更改标签的值。它只是内存中某个位置的别名。在文本部分中,它是后续指令位置的别名。在数据部分中,它是后续空间的内存位置的别名。

No. In MIPS you must have a constant outside the parentheses when dereferencing (poor wording). If it were possible to change the value of the label, then it would no longer be constant. To get around this, you could instead do something like

la $t1, label          #t1 holds address of label
add $t1, $t1, $s0      #t1 now holds address of label + s0 offset
lw $a0, 0($t1)         #load word from t1's location

addi $t1, $t1, 4       #t1 was incremented by 4 bytes now
lw $a0, 0($t1)         #load the next word

It might be advisable to use addu if s0 will always be non-negative.

EDIT: You cannot change the value of a label. It is solely an alias for a location in memory. In the text section, it's an alias for the position of the following instruction. In the data section, it's an alias for the location in memory of the following space.

诺曦 2024-10-07 00:56:02

我确实认为答案应该澄清并重新回答“是”。我认为“地址”和“价值”之间存在沟通不畅和混淆。

我目前有一个数组,我想导航它来进行冒泡排序。为了知道何时停止,我需要一个索引值来与持久的数组长度进行比较。

我创建了 arrayLength 标签,在看到这个答案的“否”后,我被困了 4 个小时,试图用之前用户的值更改 arrayLength 的值我记得存储词sw

从技术上讲,是的,您无法更改数组的基地址,但您可以读取后面的下一个地址。

您可以执行以下操作来获取数组长度,以使用读取的整数迭代数组:

li $v0, 5
syscall

.data
.word
.assign 2
arrayLength: 0

.text
sw $v0, arrayLength
lw $t0, arrayLength

此时,由用户定义的 arrayLength 取自 $v0,放入arrayLength,然后从arrayLength存储到$t0中,并可用于比较以迭代数组。

换句话说,为了回答您的问题,arrayLength 中的 0 值被覆盖(对于我们的示例,假设为 10)。所以是的,您可以整天覆盖标签。您不能做的是修改该标签的地址。

据我所知,一旦创建了标签,它就会被分配给一个地址,并且根据您识别它的方式分配以下地址(.byte.half、<代码>.word等)。从那里开始,除非有某种方法可以删除标签并重新创建它,否则您无法修改地址。这会导致内存管理出现很多问题,并且无缘无故地变得非常低效和复杂。


现在继续。请注意,如果您不知道,可以使用 .space [number] 预定义一个数组。我认为默认情况下每个值都是 32 位,即 4 个字节(一个字)。因此,如果您想要 5 个项目(单词),您将执行 5 x 4,因此 .space 20 并且您可以将 5 个单词存储到数组中。

在这种情况下,我们现在假设 array 已经创建并从 arrayLength10 填充,并且我们正在打印每个索引处的值,如下所示:

add $t1, $zero, $zero  #index value 0, our base address index

loop:

li $v0, 1
lw $a0, array($t1)
syscall

addi $t1, $t1, 4 #increase the address index by 4 (each index is 4 bytes long)
addi $t3, 1, $zero #increase the index identifier by 1
blt $t3, arrayLength, loop

#exit

因此地址索引 ($t1) 是文字地址位置。每个索引,即:字,长度为 4 个字节(32 位)。因此,我们的 $t3 是一个标识符(例如),“嘿,我们位于索引位置 2 of 10”,但实际上,我们位于地址位置 array + 8 out of 数组+40

您可能可以丢弃索引标识符,然后将 $t3 设为 arrayLength x 4 的值,然后执行 blt $t1, $t3, Loop。

希望这有帮助。

I do think the answer should be clarified and reanswered to "Yes". I think there was a miscommunication and a mix-up between "address" and "value".

I currently have an array that I want to navigate to do a bubble sort. In order to know when to stop, I need an index value to compare with an array length that is persistent.

I created the arrayLength label, and after seeing "no" on this answer, I was stuck for 4 hours trying to change the value of arrayLength with a value from the user before I remembered store word sw.

Technically, yes, you can't change the base address of an array, but you can read the next address that follows.

Here is what you can do to get an array length to iterate over an array with a read integer:

li $v0, 5
syscall

.data
.word
.assign 2
arrayLength: 0

.text
sw $v0, arrayLength
lw $t0, arrayLength

At this point, arrayLength, defined by the user, is taken from $v0, put in arrayLength, then stored in $t0 from arrayLength, and can be used to compare to iterate over the array.

In other words to answer your question, the value of 0 in arrayLength was overwritten (for our example, lets say 10). So yes, you can overwrite labels all day long. What you cannot do is modify the address of that label.

To my knowledge, once a label is created, it is assigned to an address and the following addresses are allocated depending on how you identify it (.byte, .half, .word, etc.). From there, unless there is some way to delete a label and re-create it, you cannot modify the address. That would cause a LOT of issues with memory management and be very inefficient and complex for no reason.


Now to continue. As a note, if you didn't know, you can predefine an array with .space [number]. I think each value is by default 32 bits, so 4 bytes (a word). So if you wanted, say 5 items(words), you would do 5 x 4, so .space 20 and you can store 5 words into the array.

In this case, we now assume array is already created and filled in from our arrayLength of 10 and we are printing the value at each index, as follows:

add $t1, $zero, $zero  #index value 0, our base address index

loop:

li $v0, 1
lw $a0, array($t1)
syscall

addi $t1, $t1, 4 #increase the address index by 4 (each index is 4 bytes long)
addi $t3, 1, $zero #increase the index identifier by 1
blt $t3, arrayLength, loop

#exit

So address index ($t1) is the literal address location. Each index, i.e: word, is 4 bytes (32 bits) long. So our $t3 is an identifier to say (for example), "Hey, we are at index location 2 of 10," but in reality, we are in address location array + 8 out of array + 40.

You could probably chuck the index identifier and just make $t3 the value of arrayLength x 4 and do blt $t1, $t3, loop.

Hope this helps.

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