开发者

Evaluating the operation of Assembly code

开发者 https://www.devze.com 2023-03-26 20:46 出处:网络
This is my homework assign开发者_StackOverflow中文版ment. Describe as concisely as possible what the following MIPS code

This is my homework assign开发者_StackOverflow中文版ment.

Describe as concisely as possible what the following MIPS code achieves? Assume that register $8 holds base address of array 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:

Now what the answer I came up with is,

The loop will continue to run infinitely if A[0] contains a value less than or equal to 0. Please guide me if I am wrong with it.

Thanks in advance.


Take a look at this just a bit more. What is happening on line 7 is important. What is addi $8, $8, 4 doing? This will help you with your answer. I think your current answer isn't quite sufficient.


With the help of a MIPS instruction set reference, let's translate the assembly code to an easier-to-understand form:

    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:

Note that r0 always has the value zero.

Let's translate it further:

r10 = 0
while true:
    r16 = memory[r8]
    r9 = if r16 > 0 then 1 else 0
    if r9 == 0:
        break
    r10 += 1
    r8 += 4


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.)

0

精彩评论

暂无评论...
验证码 换一张
取 消