I don't understand the solution given for a previous homework problem.
Question: Show the contents of $R3
after the following instruction is executed.
lb $R3, 13($R2)
Initial values:
$R2 = 12(decimal)
$R3 = 14(decimal)
Initial Memory Address(Decimal) -- Content(decimal):
36 -- -18
32 -- 99
28 -- 177
24 -- -14
Solution:
$R3 = OxFFFFFFFF
I don't understand how he arrived at that answer. Any help is appreciated!
If you google mips instruction set you see that lb is load byte.
lb $t, offset($s)
means
$t = MEM[$s + offset]; advance_pc (4);
assuming MEM is 32 bits wide here, but that is at the edge of the core, how it is implemented we dont care about.
The definitions you have first being decimal which may have been intentional to create learning pain. Then are all can fit in a byte sized values which could imply byte addresses.
r2 is 12 then we add 13 to get decimal address 25 which is not defined. If we go with the notion as you mention in comments that these are word sized definitions. Then 24 = -14 means at address 0x18 (which BTW is a valid 8 bit, 16 bit, 32 bit and 64 bit aligned address) we have the value 0xFFFFFFF2 for address 0x18, we are after 0x19. So depending on the endianness that could be the 0xooFFoooo byte or 0xooooFFoo byte in either case that is an 0xFF.
Then you need to know that lb sign extends. The msbit of 0xFF is a one so the rest of the bits will be ones we load 0xooooooFF and then sign extend that to 0xFFFFFFFF. Then save that in r3. (and increment the program counter to the next instruction).
Looking at this the other way, since you gave us the answer of 0xFFFFFFFF, that is not a byte value so lb must be sign extending and the value read must have been 0xFF. If it didnt sign extend we would expect 0x000000XX where XX is the byte we are looking for. And or xx is positive from a byte sized twos complement perspective and then it wouldnt be obvious there was a sign extension.
The initial value of r3 is not relevant, just there to possibly trip you up.