user8026040
user8026040

Reputation:

Understanding the MIPS equivalent of this C++ code

How would you replicate:

int* a;
b = a[2];

In MIPS, without using .data?

My answer for this was: lw $t1, 4($t0), where $t0 is a & $t1 is b--but this was incorrect.

Similarly, how would you replicate:

char* a;
a[4] = b;

Once again, my answer for this was lb 4($s0), $t0, where $t0 is b & $s0 is a--but this was also incorrect.

Upvotes: 0

Views: 139

Answers (1)

MikeCAT
MikeCAT

Reputation: 75062

To replicate

int* a;
b = a[2];

a[2] is 2 elements after what is pointed at by a, so lh $t1, 4($t0) if int is 2-byte long and lw $t1, 8($t0) if int is 4-byte long.

To replicate

char* a;
a[4] = b;

The instruction to write 1-byte value to memory is sb, not lb. Therefore, sb $t0, 4($s0) should do assuming char is 1-byte long.

Upvotes: 0

Related Questions