1

这是我的家庭作业。

尽可能简洁地描述以下 MIPS 代码实现了什么?假设寄存器 $8 保存数组 A 的基地址。

addi $10, $0, 0
L1:
lw $16, 0($8)
slt $9, $0, $16
beq $9, $0, L2
addi $10, $10, 1
addi $8, $8, 4
j L1
L2:

现在我想出的答案是,

如果 A[0] 包含小于或等于 0 的值,则循环将继续无限运行。如果我错了,请指导我。

提前致谢。

4

3 回答 3

1

Here is the full, straight answer to the original question - "what does the code do?":

  • We assume that r8 points to the start of an array of signed 32-bit integers.
  • The code searches forward in the array for an element that is non-positive and stops if and only if it finds such an element. If it stops, then these properties hold:
    • r10 is equal to the array index of the found element. (Which is 0, 1, 2, or etc.)
    • r8 is equal to the address of the found element. (Which is the original value of r8 plus 4*r10.)
    • r16 is equal to the value of the found element (which is zero or negative).
    • r9 is equal to 0. (Condition code)

(The explanation is in my other answer post.)

于 2011-08-10T16:22:56.290 回答
1

再看看这个。第 7 行发生的事情很重要。在addi $8, $8, 4做什么?这将帮助您回答问题。我认为您当前的答案还不够充分。

于 2011-08-09T18:53:41.857 回答
1

借助MIPS 指令集参考,让我们将汇编代码转换为更易于理解的形式:

    Compute r0 + 0 and store to r10.
L1:
    Load word at address r8 + 0 to r16.
    If r0 is less than r16, then set r9 to 1, else set r9 to 0.
    If r9 equals r0 then goto L2.
    Compute r10 + 1 and store to r10.
    Compute r8 + 4 and store to r8.
    Goto L1.
L2:

请注意,r0 的值始终为零。

让我们进一步翻译:

r10 = 0
while true:
    r16 = memory[r8]
    r9 = if r16 > 0 then 1 else 0
    if r9 == 0:
        break
    r10 += 1
    r8 += 4
于 2011-08-09T19:07:09.143 回答