tags:

views:

339

answers:

4

So, for example, what would something like this:

lw $t1, 0($t0)

or

lw $t2, 8($t0)

Translate to in C or C++? I mean I'm loading a word from the address into a register, I get that. Is an array a similar concept, or, what?

Thanks in advance.

+2  A: 

This is "load word" instruction. It loads 4-byte word from memory at location which address is stored in register $t0, into register $t1.

There is no equivalent construction in c/c++. This instruction is very popular and used in most constructions where memory access in required, for example:

int *p;
// p = ...
*p += 10;

may be translated to something like (given $t0 contains pointer 'p')

lw $t1, 0($t0)
addi $t1, $t1, 10
sw $t1, 0($t0)

Here the first instruction loads the variable into a register, the second modifies it and the third writes it back into the memory

Rom
A: 

For the first line:

int t1, *t0; ... t1 = *t0 or t1 = t0[0] or even int t0, t1; ... t1 = t0.

For the second:

int t2 = t0[2], maybe, or perhaps int t2 = t0.thirdThing.

struct {
   int a,b,thirdThing;
} t0;

But you can't know for sure. It could be char *x, **y; x = y[2]; If we saw how the address got into the register it might shed more light on the original code.

DigitalRoss
A: 

I think you can't write exactly similar code. You could do the following:

int* wordAddress = ... + 8; // 8 or any offest
// assuming int is a word on MIPS wich I never worked with personally
int  word = *wordAddress;

You could apply the offset when retrieving the value also:

int* wordAddress = ... + 0;
int  word = *(wordAddress + 8);

There is a note: You can't specify the required register in the language. What you can do is to give a hint to the compiler to put word in a register:

// it is just a hint. The compiler is free to put it in memory.
register int  word = *wordAddress;
AraK
A: 

Assuming you are using MIPS32 (and hence have 32-bit memory addressing) then its pretty easy what they do.

lw $t1, 0($t0)

What this does is load the value at byte offset 0 from memory address t0 into the t1 register.

lw $t2, 8($t0)

What this does is load the value at byte offset 8 from memory address t0 into the t2 register.

Lets assume you have a memory address 0x12345678. Then the MIPS assembly is, essentially, doing the following:

int t0 = 0x12345678;
// ...
int t1 = *(int*)(t0 + 0);
int t2 = *(int*)(t0 + 8);
Goz