












Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!













Virgil Bistriceanu Illinois Institute of Technology
The topic of this chapter are the addressing modes, the different ways the address of an operand in memory is specified and calculated. Although the computer's world offers a large variety of addressing modes, we will discuss only about the basic ones, those that are used the heaviest in programs.
We begin with a discussion about memory addressing: our special concern in this section is how is data (especially if we allow different data sizes) retrieved from memory and what problems appear.
A major decision a designer faces is how is the memory to be addressed.
Example 4.1 MEMORY ACCESS:
You have to design a 16 bit architecture: show how you can access the memory if you allow byte and half-word addressing or only half-word. Answer:
0 16 bit data 0 8 bit data 8 bit data 1 a) only half word b) byte and half-word
4 Addressing modes
Virgil Bistriceanu Illinois Institute of Technology
If we have a n-bit wide architecture, and we always read/write from/to memory only n-bit wide data (in our case 16-bit architecture and read/write only half-words as in example 4.1a) then there are not very many hardware problems; an address always means a half-word. However, a problem appears when we try to manipulate characters (a character nicely fits a byte):
In the case we should have a byte-addressable memory, as in (4.1b), then each address will specify a byte in the memory, and it is a matter of organization if we want to have two banks of memory, 8 bit each, or only one, 16 bit wide, from which we select the proper byte.
The problem of multiple data representation is unavoidable in a general- purpose computer:
The above list doesn't include other data types like pointers, etc.
Efficient access to data of various sizes is so important that modern machines (32 bit machines) provide access to byte (8 bits), half-word ( bits), word (32 bits), and some provide also access to double-words ( bits).
Accessing data of various sizes rises some problems in memory organization. If the memory organization only allows byte-access, as in Figure 4.1, then one byte requires one memory access, a half-word (
4 Addressing modes
Virgil Bistriceanu Illinois Institute of Technology
at a higher address) will be located in memory bank 1. The address bits AD (^) n-1 - AD 1 are the same for the two bytes; hence a single access suffices to get both bytes.
When the 16 bit data starts at an odd address (like in Figure 4.4.b), then two accesses are necessary to read/write it: the addresses of the low and high bytes differ in more than the least significant bit (AD0) so that the memory banks must get different addresses to retrieve the proper data.
Example 4.2 DATA AND ADDRESS:
Suppose you have the memory from Figure 4.3. and the data configuration in Figure 4.4.b. What data will be read if the address is 15 and B/H = 0? But if the address is 16?
Answer: When the address is 15 and B/H = 0 the read data will be the half word starting at address 14. As can be seen in figure 4.3, during a read, the least significant bit of the address, AD0 is used only to select the proper byte if B/H=1. The two banks receive as address the binary configuration 0..00111, which are the bits ADn-1-AD1 of the address.
When the address is 16 and B/H=0 then the read data will be the half word starting at address 16. Both memory banks receive the same binary configuration as address 0..
By now it should be clear why many systems require data to be aligned: in our case 16 bit values must be stored at even addresses. If data is aligned in memory then it is possible to access that data with a simple memory design and without wasting time.
Note that the alignment network in Figure 4.3 increases the response time of the memory (accesses to memory take longer). Instead of using an alignment network some machines prefer to always read whole words (for 32 bit machines) and then to shift data inside the CPU in the cases alignment is needed; nonword accesses are slower but the common case (word access) is not affected and can be optimized to work at maximum speed. As we shall see in next chapter the alignment problem is really critical in the case an alignment network must be placed between the cache and the CPU.
More generally, if a data object needs up to 2N^ bytes to be represented, then aligning it means placing it in the memory starting with an address that has the least significant N bits zero. Another problem related to memory addressing is the byte ordering inside a data that is longer than a byte (half word, word, etc.).
4.1 Interpreting memory address
Virgil Bistriceanu Illinois Institute of Technology
Address n
FIGURE 4.1 A byte-organized memory
n Address
B/H Significance Half-word Byte
FIGURE 4.2 A memory that can be byte and half-word accessed.
4.1 Interpreting memory address
Virgil Bistriceanu Illinois Institute of Technology
Alignment network
Memory bank 1 Memory bank 0
AD (^) n-1 ...AD (^) n
FIGURE 4.3 Internal organization of the memory in figure 4.
4 Addressing modes
Virgil Bistriceanu Illinois Institute of Technology
The first convention presented in example 4.4 is called Big Endian (most significant byte stored at the lowest address in the word); the second convention presented is called Little Endian (the most significant byte of the object is stored at the highest address for that object).
Is there any of the two approaches better than the other? Unfortunately no, and this is the reason a debate between the advocates of two possibilities has been around for many time. The names Big Endian and Little Endian come from a paper of Cohen[1981], which draws an analogy between the arguments over how to number bytes and each end of the egg should be broken as described in Gulliver's Travels.
b) 16 bit data starts at odd address
a) 16 bit data starts at even address
FIGURE 4.4 Aligned and not aligned 16 bit data.
4 Addressing modes
Virgil Bistriceanu Illinois Institute of Technology
Before considering in some detail the basic addressing modes, let us recall that we have already discussed about this topic (even though not in terms of addressing modes) when we approached the n-address machine subject.
Using examples, we saw that the operand can be in a register (we call this to be the register addressing mode ) or fully included in the instruction (we call this direct addressing mode ), and we tried to figure out the impact over the code length and execution speed. We also discussed about the PC-relative addressing and the use of displacements.
The most used addressing modes are presented below; a left arrow means assignment, and M stands for memory. We use an array notation for memory because we can view the memory as an array of bytes (or half- words or words whichever you prefer, but the significance of the notation must be very clear).
Register
ex: ADD r1, r2, r means: r1 r2 + r comment:used when a value is in a register.
Immediate (or literal)
ex: ADD r1, r2, 1 means: r1 r2 + 1 comment:used when a constant is needed.
Direct ex: ADD r1, r2, (100) means: r1 r2 + M[100] comment: used to access static data; the address of the operand is include in the instruction; space must be provided to accommodate a whole address.
Register indirect (or register deferred)
ex: ADD r1, r2, (r3) means: r1 r2 + M[r3] comment: the register (r3 in this example) contains the address of a memory location.
4.2 A classification of addressing modes
Virgil Bistriceanu Illinois Institute of Technology
Displacement
ex: ADD r1, r2, 100(r3) means: r1 r2 + M[r3 + 100] comment: the address is the sum of the content of the register (the base) and a constant from the instruction (the displacement); used to access local variables on a stack or data structures. If the displacement is zero, then it is the same as register indirect.
Memory indirect (or memory deferred)
ex: ADD r1, r2, @r means: r1 r2 + M[M[r3]] comment: used in pointer addressing; if r3 contains the address of a pointer p, then M[M[r3]] yields *p.
Indexed
ex: ADD r1, r2, (r3)[r4] means: r1 r2 + M[r3 + size*r4] comment: two registers are added to get a memory address, used in array addressing with one register the base address of the array and the other one the offset from the base to the desired element in the array.
Autoincrement
ex: ADD r1, r2,(r3)+ means: r1 r2 + M[r3] r3 r3 + s comment: used to step through arrays, the first time it is used r3 points to the beginning of the array; each access increments r2 with the size s of an array’s element (s = 1 for byte, s = 2 for half-word, etc.)
Autodecrement
ex: ADD r1, r2, -(r3) means: r3 r3 - s r1 r2 + M[r3] comment: can be used like autoincrement, but to step through arrays in reverse order. Together with the autoincrement mode it can be used to implement a stack.
This list is far from being complete and you may wonder:
4.4 Displacement Addressing
Virgil Bistriceanu Illinois Institute of Technology
Again there is a tradeoff in deciding the maximum size immediate operands should have in the instruction set:
Displacements are used in two ways:
Measurements show a wide distribution of displacements for the first category, while for the second one most displacements are short (4 - 8 bits), mainly due to the fact that usually the target of a branch (jump) is close to the actual instruction. The tradeoffs in deciding the length of the displacement are similar to those in deciding the length of immediate operands.
Example 4.6 ADDRESSING MODES:
An array of two integers (an integer is 32 bits wide) is placed in memory starting with address 100. Show how to increment the elements of the array using displacement addressing. The memory is byte addressable.
Answer: LOAD r1, 100# in r1 base LOAD r2, 0(r1)# load the first element of array ADD r2, r2, 1 STORE 0(r1), r LOADr2, 4(r1)# second word starts at 104 ADDr2, r2, 1 STORE 4(r1),r
4 Addressing modes
Virgil Bistriceanu Illinois Institute of Technology
In indirect addressing an address is considered to be the address of an address, rather than the address of a value. Suppose we have the following declarations in C:
int num, *ptr;
which declares num to be of type integer and ptr to be of type pointer to integer. The following statement:
ptr = #
assigns the address of num to ptr. The logical structure created by the above assignment is:
ptr num
Example 4.7 ADDRESSING MODES:
An integer is stored somewhere in the memory; a pointer to this integer is at address 200. Use memory indirect addressing to increment the number.
Answer: The relation between the pointer and the number is shown below:
If the machine supports the base address (200) to be in memory we have: LOAD r2, 1 ADD r2, r2, @(200) STORE@(200), r
the integer
200
4 Addressing modes
Virgil Bistriceanu Illinois Institute of Technology
Indexed addressing is provided in many machines for the convenience of accessing arrays.
Example 4.9 ADDRESSING MODES
An array of two integers (each integer = 32 bits) is placed in memory starting with address 100. Show how to increment the elements of the array using indexed addressing.
Answer: LOAD r1, 100 # in r1 base; LOAD r2, 0 # the first index in array; LOAD r3, 1 ADD r4, r3, (r1)[r2] STORE (r1)[r2] ADD r2, r2, 1 #index = index + 1 ADD r4, r3, (r1)[r2] STORE (r1)[r2]
What is the price we have to pay to include this addressing mode in the instruction set? Again we take into account how the addressing mode effects the instruction length and the execution time as compared with other addressing modes. We shall consider the instruction encoding and the execution time as the parameters for this discussion.
Instruction encoding
For a three address machine (our examples so far are for such a machine) we need four fields to encode the registers to be used:
For a 32 bit machine with fixed instruction size (one word), and a set of 32 registers, the necessary fields to encode the four registers require 4*5 = 20 bits. If we allow 6 bits for the opcode, then we still have 32 - (20+6) = 6 bits in the instruction. Do we need them? Of course we need them because we have to encode two more things in the instruction:
As long as we are concerned with the first problem we face yet another
4.6 Indexed addressing
Virgil Bistriceanu Illinois Institute of Technology
problem, we didn’t discuss so far:
If the answer is yes, then we have to specify, besides the addressing mode, to which operand does this addressing mode apply (we discuss about a register-memory machine, not about a memory-memory machine, which allows all operands to have their own addressing mode, an even more complicated problem). For a three address machine, this requires two more bits in the instruction.
In the case of a no answer, we have to decide to which operand the addressing mode applies (in our examples, it was second source operand); in this case we do not need extra encoding space, because the operand to which the addressing mode applies is implicitly specified, and thus can be hardwired.
To conclude this, we have to encode the addressing mode(s), and possibly to which operand(s) it applies.
When we finally come to the second problem, specifying the size, we realize that we need two bits because arrays can be formed with the following data sizes:
Certainly these two bits are needed, it would be much too restrictive not to allow certain arrays of the basic data types. By now the following bits are available:
32 - (20 + 6) - 2 = 4 bits
four bits with which we can try to encode the addressing mode and, possibly, the operand to which it applies. A possible solution could be to use two bits for encoding the addressing mode, thus allowing four addressing modes, and the other two to specify to which of the three operands applies. Should we allow more addressing modes, so we restrict the number of operands to which that mode applies.
A final note: if we decide on fixed size instructions, then we have to drop some addressing modes; for instance, the direct mode which requires a whole absolute address to be provided in the instruction (unless the instruction is wider than an address).
4.6 Indexed addressing
Virgil Bistriceanu Illinois Institute of Technology
same number of clock-cycles.
Obviously this addressing mode is more expensive in clock-cycles than a simpler addressing mode, like register deferred; indexed addressing can be simulated using simpler addressing modes as the example 4.11 shows:
Example 4.11 ADDRESSING MODES
An array of two integers (each integer = 32 bits) is placed in memory starting with address 100. Show how to increment each element of the array using register deferred addressing mode.
Answer: LOAD r1, 100# the base LOAD r2, 1 # 1 will be used for increment ADD r3, r2, (r1) STORE (r1), r ADD r1, r1, 4# the next array element is at 104 ADD r3, r2, (r1) STORE (r1), r
Compare this example with example 4.9.
We conclude this session by underlining again the basic design principle: make the common case fast. If this addressing mode is not very often used (and indeed it is not, at least in common programs), it is probably wise to dispose it, and try to optimize the common case(s) (which seems to be immediate, displacement, register deferred).
4 Addressing modes
Virgil Bistriceanu Illinois Institute of Technology
4.1 Design a memory module with an 1 Mx16 organization, to be accessed by half word, using 1Mx1 DRAM.
4.2 Design a memory module with an 1Mx32 organization, to be accessed by byte, half word and word. You will need two control lines to specify the type of access:
4.3 Design in detail the alignment described in Figure 4.3.
B1 B0 Significance 0 0 not used 0 1 byte 1 0 half word 1 1 word