sections in this module | City
College of San Francisco - CS270 Computer Architecture Module: Simple Machine |
module list |
Addresses and Pointers
As we discussed, one nice thing about the Simple machine is its single data type - a 16-bit integer. Another is that all programs start at 0 and use contiguous addresses. These two facts mean, as we have seen, that we can view memory as an array of shorts, and the address of each datum is the same as its index in the memory array. As we will see later, this is a big simplfication.
In the Handout SimpleMachine, we examined the program copy0:
0x1003 # (address 0) LOAD SOURCE load word to copy from address 3
0x2004 # (address 1) STORE DEST store it in new location (address 4)
0x0000 # HALT
0x4040 # (address 3) SOURCE: the word to copy
0x0000 # (address 4) DEST: the location to copy to
This simple program has three instructions and two words of data. It occupies 5 words (shorts) in the memory array. The data at address 0 is 0x1003. The data at address 3 is 0x4040.
We always access data by its address. In our simple program the address of the data is encoded into the LOAD and STORE instructions. That's why the instruction LOAD SOURCE has the value 0x1003. 3 is the address of the datum named SOURCE, or, simply, the index of SOURCE in the memory array. Easy.
This method of encoding the address of the data into the instruction is called MEMORY (or memory direct) addressing. Instructions that support memory addressing (that is, instructions for which this addressing mode is allowed) are seldom seen today because memory addresses are so large. Instead, some other way of obtaining the address is used. One way is to calculate the address of the datum and place the calculated address in a register. Then the value (an address) in the register is used to get the data. A simple way to do this on our Simple machine is to use the LA instruction
LA SOURCE # loads the address of SOURCE into the accumulator
Once the address is in the accumulator we can use the address to get the data using a load indirect instruction (LIA). This sequence
is common on all machines. We will see it all the time later on MIPS.
We have one problem left when doing this on our Simple machine, however
- the address is in the wrong register. Simple Machine's indirect
instructions require the address to be in the special address register (the areg). The next instruction is then
MVAA # move accumulator to areg
Now we are ready to use our address (in the areg) to get the data
LIA # load the data at address areg and place it in the accumulator
Recap:
The instruction sequence
LOAD SOURCE
can also be done like this
[ NOTE: Later in this course we will be using C syntax for addresses and pointers. In C, the address of SOURCE is specified using &SOURCE. ]
You might immediately ask: "Why?" Well, in this example, it's silly. But consider the problem of getting the value ARRAY[I]. In this case, you cannot use a LOAD instruction because you don't know the address of ARRAY[I]. The address must be calculated.
Similar to the LIA instruction is the SIA instruction, which uses the address in the areg to store a value at that address. (The value stored is the value in the accumulator.)
Can you come up with a method for updating ARRAY[I]? We will verify your answer in the next section.
Pointers
A pointer is an address that is stored in memory. (You could also consider the value (the address) that we loaded into the areg in the last example as a pointer, but registers are hidden from most programmers, making that pointer implicit.) In this section we will speak of pointers that are available to the programmer from the level of C (or C++).
In the last example, the address of SOURCE was 3. Thus when we executed
LA SOURCE
we loaded the constant 3 into the accumulator.
The constant 3 could also be placed in memory. Let's assume we have a value 3 in our Simple Machine program in a variable PTR. To get 3 into the accumulator we could then use
LOAD PTR
and follow it with the same sequence
[NOTE: In C, this is called dereferencing the pointer and the syntax here is *PTR ]
Pointers are incredibly useful. We can calculate an address, save it in a pointer, and use it later. Every object in C++ and Java is accessed by a pointer.
Once again, here are our three mechanisms for accessing a piece of data:
It is important to master these three mechanisms now. It will help the confusion later when addresses become more complicated and data has various types.
Before we go on , can you write an assignment statement for this
Simple Machine code? The answer is at the top of the next section.
Prev | This page was made entirely
with free software on linux: Kompozer, the Mozilla Project and Openoffice.org |
Next |