Let’s say I have $t0, and I’d like to divide its integer contents by two, and store it in $t1.
My gut says: srl $t1, $t0, 2
… but wouldn’t that be a problem if… say… the right-most bit was 1? Or does it all come out in the wash because the right-most bit (if positive) makes $t0 an odd number, which becomes even when divided?
Teach me, O wise ones…
Use instruction sra: Shift right arithmetic !!
Divides the content of $t0 by the first power of 2.
Why is this important? Check this simple program that divides an integer number (program’s input) by 2.
Compile
Test drives:
See what happens? The srl instruction is shifting the sign bit
if we shift one bit to the right, we get 0x7fffffff
Of course this is not a problem when the number is a positive integer, because the sign bit is 0.