I was wondering if in MIPS 32, the two following are identical:
add $t1 $t1 $t1
sll $t1 $t1 1
Does sll handle a possible overflow differently than add? I can't seem to find a good explanation online.
Thank you!
I was wondering if in MIPS 32, the two following are identical:
add $t1 $t1 $t1
sll $t1 $t1 1
Does sll handle a possible overflow differently than add? I can't seem to find a good explanation online.
Thank you!
Yes, you suspect correctly: sll won't ever trigger an overflow exception, whereas add will trigger overflow when interpreting the numbers as signed integers whose sum won't fit in 32-bits.
As Jester says, addu will also never trigger overflow so is more equivalent to your sll.
Most instruction sets have some duplication/overlap like this, as it would take both design & implementation effort to prevent, arguably for little or no return on investment.
We should always use addu for pointer arithmetic because we don't want a signed overflow check. Pointers are unsigned, and addu stands for add unsigned — but it performs the same arithmetic as add, just omitting overflow checking. You might think it checks for unsigned overflow but it doesn't, so it is a bit of an oddity to call it addu. We should also use addiu rather than addi with pointers for the same reason — to forgo a signed integer overflow check. Adjustment of the stack pointer should be done with addiu, but you'll often see people use addi by mistake.
RISC V decided to remove the overflow checking entirely, so if you want an overflow check you have to use more instructions; the regular instruction is just add and works for both signed or unsigned integers (the additional instructions you would add to check for overflow would be specific to whether signed or unsigned).