2

What is the best solution for the translation of the following C code into MIPS assembly?

x = A[i];

Note that x ⇨ $t0, A[] ⇨ $s0, i ⇨ $s1.

With add:

sll $t0, $s1, 2
add $t0, $t0, $s0

or with addu:

sll $t0, $s1, 2
addu $t0, $t0, $s0

I know the difference between add and addu, but I not understand when to use one or the other.

Community
  • 1
  • 1
ᴜsᴇʀ
  • 1,109
  • 2
  • 9
  • 23

2 Answers2

3

C code won't throw an exception on integer overflow. So assembly shouldn't, either. Use addu.

Seva Alekseyev
  • 59,826
  • 25
  • 160
  • 281
0

add in MIPS throws an exception on signed overflow.
Let's assume you want to index into a large array, and the array crosses between (signed)INTPTR_MAX and (signed)INTPTR_MIN. If you use add, this will throw an exception, although you may have been well within the array bounds.
What you are more likely to need to care for in array indexing is unsigned overflow, crossing from (unsigned)UINTPTR_MAX to zero, but MIPS doesn't help you there, AFAIK.

EOF
  • 6,273
  • 2
  • 26
  • 50