1
votes

I am reading Chapter 2.12 of Computer Organization and Design, trying to understand the logic of a MIPS-32 linker. I understand the concept of linking two object files and absolute reference linking. What I don't understand is the quoted paragraph below explaining the logic for calculating absolute addresses.

The load and store addresses are harder because they are relative to a base register. This example uses the global pointer as the base register. Figure 2.13 shows that $gp is initialized to 1000 8000hex. To get the address 1000 0000hex (the address of word X), we place 8000hex in the address field of lw at address 40 0000hex. Similarly, we place 8020hex in the address field of sw at address 40 0100hex to get the address 1000 0020hex (the address of word Y).
Page 128

According to the MIPS-32 instruction set for instructions lw and sw, the immediate offset operand is added to the register operand.

But
1000 8000hex + 8000hex = 1001 0000hex != 1000 0000hex
1000 8000hex + 8020hex = 1001 0020hex != 1000 0020hex

So maybe I am misunderstanding something and the offsets are being subtracted? Even if that was the case:

1000 8000hex - 8000hex = 1000 0000hex == 1000 0000hex
1000 8000hex - 8020hex = FFFF FE0hex != 1000 0020hex

What is happening here?

Figure 2.13:

MIPS-32 Memory Allocation

1

1 Answers

2
votes

The 16-bit offsets found in the instruction words are sign-extended to 32 bits before being added to the base address.

So if the offset found in the instruction word is 0x8000, you get 0x10008000 + 0xFFFF8000 == 0x10000000 (when the result is truncated to 32 bits). And similarly for the second example.