Okay so I am looking at an example program in LC-3 and one line of code is as follows:
0010 0 10 011111100 ; R2 <- M[x3100]
So I understand that the code is taking the contents at address x3100 and storing them into R2, however I am confused as to how they acquired the offset part of the binary code. When I look up 01111110 in binary, it's 252 with hex number being FC. So how does this relate to the x3100?
The immediate, referred to as the PCOffset9
in some texts, is a pc-relative offset. Thus, the more complete program is probably something like this:
.ORIG x3000
3000 ...
3001 ...
3002 ...
3003 LD R2, #252
...
...
3100
.END
When this instruction executes, the PC holds the value x3003, which is used to fetch the instruction LD R2, #252
from memory. During that instruction's execution, first the PC is used to fetch the instruction, then it is incremented, and next, the "effective address" is computed as ea = PC + PCOffset9
. Here PC is x3004 (was x3003 then +1), and the immediate is xFC, so their sum is x3100. That same offset #252 will refer to a different memory location, if used in another instruction (i.e. at a location other than x3003). Note that the PCOffset9
is a 9-bit field that is signed: before the above addition to the PC, it is sign extended to 16-bits. Being signed it can reach forwards & backwards, so the range is -256 to +255.