10/3/04

A student writes:

     Dr Patt,
     I was reviewing material for the test today, and came across the
     explanation for what the LDR instruction does. I don't understand
     why it would be advantageous to use LDR, wouldn't it simply be
     simpler to say "Computer, load from this address in memory, to

That is exactly what we want to do! We want to say: "Computer, load the contents of this address in memory into this register."

The issue is: how do we tell the computer WHICH address we are talking about? There are three ways, and there are three instructions available to us, depending on which way makes sense at the particular instance: LD, LDR, and LDI.

     this register"  instead of saying "Computer, read this register,
     add X to it, then take that new value, read it from memory, and
     put it into this register"? is there another type of LD instruction
     (LD or LDI?) that will do exactly what I  stated, if not, why not?

I think the best way to answer this is to briefly tell you why I might suggest each of the three ways. I thought I did that in the book. I guess I need to do it more clearly ...another reason for a 3rd edition, I guess.

The first thing to note is that I do not have enough bits in the instruction to specify the exact location in memory, since instructions consist of 16 bits, and I need 4 for the opcode and 3 for the register I will load into. I certainly could make the load instruction 4 + 3 + 16 bits long, but having each instruction have a different number of bits would make the hardware design very, very ugly. MUCH nicer to have all instructions the same length. In our case, 16 bits. For example, it allows us to update PC with "PC is replaced with PC+1" during the FETCH phase of the instruction cycle.

Now, then, which of the three (LD, LDR, LDI) should I choose in a given instance.

If I know that the location of memory containing the data I want to load into the register is close to the location in memory containing the instruction that is telling the computer to do the loading: Use LD. 4 bits for opcode, 3 bits for register, 9 bits left over to specify how far away from the load instruction the location of the data is. The computer sign-extends those 9 bits to 16 bits and adds that to the PC to know where to get the data from. This only works, however, if the location is close enough.

If the location is further away, I have two choices. I need to put the actual address somewhere that I can tell the computer to look to find the address of the data. The LDI uses a location close to the location containing the load instruction. The computer uses the 9 bits in the same way as with LD, but this time when it looks in the computed location, it finds the 16 bit address of the data, rather than the data itself. This allows us to use a location close to the instruction to contain the full 16 bit address. Such a location is called a pointer, because it "points" to the data.

The LDR effectively uses one of the registers as a pointer. That is, the LDR tells the computer to look in the pointer register (i.e., BaseR) and add the sign-extended offset (which gives additional useful capability, which you will see several weeks from now) and that gives the location of the data we want to load. For now, we make that offset 0. The BIG advantage of that is that it allows us to load from a different memory location each time the computer comes across this LDR instruction by simply changing the value in BaseR. THAT is very, very useful. In fact, it is particulary useful in the example we started in class on Wednesday and will finish on Monday.

OK?

Incidentally, we refer to the three choices as "addressing modes" and every computer's ISA has several addressing modes, for exactly the reasons discussed above: at different times it is appropriate to instruct the computer where to look for the data in different ways. Each choice is an addressing mode.

Good luck.

Yale Patt

     Thanks!
     << name withheld ... >>