Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clarifications on signed/unsigned load and store instructions (MIPS)

I can't seem to grasp the concept on these stuff, even with the help of Google and a textbook in my hand.

Following the format (opcode, rs, rt, offset)...

  • Do you sign extend the offset before adding it to the value of the address? Or add before extending?
  • In the case of lb and lbu, what's the difference? Does it also follow the MIPS arithmetic definition that 'unsigned' just means it won't report an overflow?
  • Why doesn't lw have an unsigned version? Even the store instructions don't have one...
like image 472
Check Avatar asked Aug 29 '11 04:08

Check


People also ask

What is the difference between signed and unsigned in MIPS?

The only difference between signed and unsigned instructions is that signed instructions can generate an overflow exception and unsigned instructions can not.

What is the difference between signed and unsigned instructions?

Variables such as integers can be represent in two ways, i.e., signed and unsigned. Signed numbers use sign flag or can be distinguish between negative values and positive values. Whereas unsigned numbers stored only positive numbers but not negative numbers.

What does lbu do MIPS?

The lbu instruction fills bits 8-31 of the register with zeros. Use this instruction when the byte is regarded as a ascii character or 8-bit unsigned integer.

When can overflow occur in MIPS signed integer subtraction?

Overflow occurs only when we add two numbers of the same sign and get a different sign for the result. — Adding two positive numbers should result in a positive number. — Adding two negative numbers should yield a negative result.


1 Answers

In the case of lb and lbu, what's the difference?

The "load byte" instructions lb and lbu load a single byte into the right-most byte of a 32-bit register. How do you set the upper 24 bits? The unsigned operation will set them to zero; the signed operation will sign-extend the loaded byte.

For example, suppose you read the byte 0xFF from memory. lbu will 0-extend this value to 0x000000FF and interpret it as 255, while lb will sign-extend it to 0xFFFFFFFF, which is interpreted as -1.

Why doesn't lw have an unsigned version? Even the store instructions don't have one...

The "load word" instruction (lw), on the other hand, loads a 32-bit quantity into a 32-bit register, so there is no ambiguity, and no need to have a special signed version.

If you are storing less than a full 32-bit word, there is nothing you can do with the extra bits in the register except throw them away (ignore them).

Does it also follow the MIPS arithmetic definition that 'unsigned' just means it won't report an overflow?

I think this convention is only for the add and subtract instructions. For the other instructions, signed/unsigned indicates whether sign-extension will be performed.

Do you sign extend the offset before adding it to the value of the address? Or add before extending?

If an offset is sign-extended, it only makes sense to do it before adding it to the base address. I think a review of two's complement arithmetic will make this clear.

like image 151
nibot Avatar answered Oct 13 '22 18:10

nibot