$zero - What do you do and how does it work?

Asked

Viewed 291 times

3

Good! I started studying a little Assembly for MIPS. Can anyone explain to me how this line of code works, Ld R4,A(R0), where R0 has the same meaning of $zero?

.data

A:  .word 10
B:  .word 8
C:  .word 0

.text
main:
        ld r4,A(r0)
        ld r5,B(r0)
        dadd r3, r4, r5
        sd r3,C(r0)
        halt

1 answer

3

Type of instructions used (ld and dadd), I believe this is MIPS-64 (source who says that the dadd is present in MIPS-64).

I have no experience with the MIPS-64 and I have worked little with the MIPS-32 Assembly, but this is my code reading:

ld is load (64-bit) and dadd is signed add (64-bit).

This code starts by declaring three variables: A, B and C with initial values 10, 8 and 0. These variables are declared with the size of a word. I’m not sure if that’s 32 or 64 bits, but the code that follows only makes sense if it’s 64 bits.

Log readings $zero (r0) give the value 0 and written in this record are ignored. ld X, Y(Z) click on the log X the value at the position Y + Z (this is handy when you are making accesses to constant arrays indexes). Since Z is zero, carries the position directly.

What this does is:

  1. Load the global variable A in the log r4.
  2. Load the global variable B in the record r5.
  3. Add the two variables and save the result in r3.
  4. Store the value in r3 in global variable C.
  5. Stop the execution.

That is to say, C = A + B. So C kills the value 18.

The equivalent C code is:

#include <stdint.h>

int64_t A, B, C;

int main()
{
    C = A + B;
    // Ou o seguinte, que é equivalente:
    // (&C)[0] = (&A)[0] + (&B)[0];

    halt(); // Talvez implementado como for (;;) {}
            // Embora imagino que o halt seja mais eficiente
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.