hakuna matata
hakuna matata

Reputation: 3327

MIPS instruction questions

Can the subu instruction in MIPS give me a negative result, or will the result always be positive since we are doing the unsigned version of the sub? Also, if I want to do arithmetic shift right, and put 1 at the 0th position of the number, should I use the sra instruction, or is there another instruction for it?

Upvotes: 1

Views: 5570

Answers (1)

Chris Dodd
Chris Dodd

Reputation: 126203

Like all machine instructions, subu will give you a binary result -- 32 bits that are stored in the destination register. These bits are just bits, they are neither positive or negative in and of themselves, it depends on how you interpret them. If you interpret them as a signed integer, the integer might be positive or negative. You could instead treat the bits as a floating pointer number -- where the bits came from is irrelevant -- though that may be non-sensical.

Note that the ONLY difference between sub and subu is that sub will trap on overflow, treating the operands as signed integers. subu will produce exactly the same result, but will not trap.

sra shifts bits down (to the right) leaving the uppermost bit (which is the sign bit, if you treat the bits in the register as a signed integer) alone, and copying it into any upper positions that are vacated

Upvotes: 4

Related Questions