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

3

Good! I started to study at 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 as $ 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
    
asked by anonymous 27.03.2014 / 01:27

1 answer

3

By the type of statements used ( ld and dadd ), I believe this is MIPS-64 ( font that says dadd is present in MIPS-64).

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

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

This code begins by declaring three variables: A, B, and C with the initial values 10 , 8 , and 0 . These variables are declared the size of a word. I'm not sure if this is 32 or 64 bits, but the code that follows only makes sense if they are 64-bit.

Log reads $zero ( r0 ) give the value 0 and written in this log are ignored. ld X, Y(Z) loads in register X the value in position Y + Z (this works when accessing constant indexes of arrays). Since Z is zero, load the position directly.

What this does is:

  • Load global variable A into r4 .
  • Load global variable B into r5 .
  • Add the two variables and save the result to r3 .
  • Save value in r3 in global variable C.
  • Stop execution.
  • That is, C = A + B . Therefore, C ends with 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
    }
    
        
    27.03.2014 / 03:26