I have an exam coming up, and one of the practice problems was:
Assume that
$t0
contains the value0x12121212
and$t1<
The lb
instruction loads a byte from memory and sign extends
to the size of the register. The lbu
instruction does the same without sign extension (unsigned).
http://en.wikipedia.org/wiki/MIPS_architecture#Integer
Most computers, MIPS included, use two's complement to represent signed values though there are other ways to encode the sign, floating point is usually represented in IEEE 754 format which uses signed magnitude. An integer signed value can be represented in any number of bits, for example
char
in C is 8-bits and can represent -128 to +127short
in C is 16-bits and can represent -32768 to +32767In two's complement the most significant bit can be used to determine the sign of the number, a '1'
means it is negative, and a '0'
means it is positive.
The number 0x88, when interpreted as an 8-bit two's complement number is negative 0x78 or -120 decimal. When represented in 32-bits two's complement this is 0xFFFFFF88. There are a couple of ways to remember how to compute the two's complement of a number:
To sign extend a 8-bit to 32-bits just look at the most significant bit (bit 7) and copy that bit to bits 8 thru 31... This follows from the definition of two's complement.
The answer would be c) 0xffffff88
. The lb
instructions sign-extends the byte into a 32-bit value. I.e. the most significant bit (msb) is copied into the upper 24 bits.
0x88 == 0b10001000, i.e. the msb is 1. So the upper 24 bits will be 0b111111111111111111111111 == 0xffffff.