Address
Modes
The
MARIE supports only three addressing modes: immediate, direct, and indirect.
We
are now going to discuss addressing modes in general. Most computers support quite
a few of these modes. We shall discuss
both direct and indirect addressing.
We
begin with the idea of an effective
address, or EA. This is the address in memory of
the actual operand (variable) used in the operation.
We
use the term “EA” loosely when it is not strictly appropriate, as in the
following three
addressing modes, none of which reference memory at all.
Immediate mode addressing
Register addressing
Stack addressing.
We
begin by jumping ahead and discussing direct addressing. We then use this to define
and illustrate the three addressing modes mentioned above.
Direct and
Immediate Addressing
The
MARIE supports a Add instruction, with opcode 0011 =
0x3
It also supports Add Indirect, with opcode 1011 = 0xB.
The expanded version supports Add Immediate, with opcode 1110 = 0xE.
Consider
the following memory map (Figure 5.3 in the textbook). Let the AC = 50.
Let
X have the value 0x800 and assume the table uses
hexadecimal addresses.
Add X 0x3800 //
Go to address 800 and add its contents to the AC.
//
The value of the AC is now 950. AC ¬ (AC) + M[X]
AddM X 0xE800 //
Add the value 800 to the AC. AC ¬ (AC) + X
98%
of Load Immediate and Add Immediate use the values – 1, 0, and + 1.
Stack and
Register Addressing
In
stack addressing, the operands are
on the stack. We illustrate.
Push A // Direct addressing. A is the address of the value to be pushed.
Push B // Direct addressing. B is the address of the value to be pushed.
Add //
Stack addressing. Values to be added are on the stack.
Pop C // Again, this is direct addressing. Place result in address C.
In
register addressing, the operands
are in registers. We illustrate this
mode
using notation for a Load–Store RISC machine with two registers: R1 and R2.
Load R1,
A // Direct addressing. A is the address of the value to be
//
loaded into register R1. Use of a
register does not change this.
Load R2,
B // Again direct addressing, even
if the value goes into a register.
Add R1,
R2 // Register addressing. Values
to be added are in the registers.
Store R1,
C // Again, this is direct
addressing.
Calculation
of Effective Address
We
now step back and think about how to calculate an effective address. Having done this,
we return to direct addressing and see how it fits in.
Again,
we are speaking about computers in general.
This is not an issue for the MARIE.
We
begin with the format of a generic instruction referencing memory
Opcode |
Register Fields |
Address
Mode Flags |
Address Field |
In general, the only parts of
the instruction that are used in computing the EA are
the
Address Field this contains an address, which may be
modified.
the
Mode Flags these indicate how to
modify the given address in
order
to compute the EA (Effective Address).
the
Register Fields used in indexed addressing and in register indirect.
The
order of the fields shown here is generic and probably does not apply to all
computers.
No
Addressing
We
first handle the very simple case in which the operator does not use an
address.
The
MARIE has several such instructions: Input, Output, Halt, and Clear.
The
opcode for Halt is binary 0000 or hexadecimal 0.
The
machine code for a halt would be written as 0000, as it needs 4 hexadecimal
digits.
Question: What is the address
associated with the machine code 0000?
Answer: There
is none. The instruction does not use an
address.
Question: What is the address associated with the machine code 0777?
Answer: There
is none. The instruction does not use an
address.
One wonders how
the last three hexadecimal digits were set,
but they are
ignored.
Note: One might view this
as a variant of immediate mode addressing,
but such an
approach is probably not helpful.
Direct and
Indirect Addressing
Opcode |
Registers |
Mode
Flags |
Address
Field |
In each of direct and
indirect addressing, the computation is simple:
Just copy the address field
itself.
In
direct addressing, the address field
contains the address of the argument.
In
indirect addressing, the address
field contains the address of a pointer
to the argument.
Put another way, the contents of the memory indicated by the address field
contain the
address of the argument.
Direct and
Indirect Addressing (Example)
Consider
again the memory map taken from the textbook.
Let X refer
to the value 0x800.
Load
X this loads the AC from
address 0x800. The value 900 goes into
the AC.
EA = X AC ¬ M[X]
Load
Indirect X Go to address 0x800. Its contents are 0x900.
Load the
AC from address 0x900. The value 1000
goes into the AC.
EA = M[X] AC ¬ M[ M[X] ]
NOTE: The complexity of notation such as “AC ¬ M[ M[X] ]” is one reason we
use
the Effective Address
idea. Break the address computation into
small steps.
Register and
Register–Indirect Addressing
Opcode |
Registers |
Mode
Flags |
Address
Field |
In each of register and
register–indirect addressing, the address field is ignored.
Conventionally, it is set at all zeroes.
In register addressing, a general–purpose register contains the
argument.
In
register–indirect addressing, a general–purpose register contains
the address of the argument.
The
MARIE does not support either addressing mode.
Register and
Register–Indirect Addressing (Example)
Consider
again the memory map taken from the textbook.
We assume the MARIE has
an AC and a register R1. Assume (R1) =
0x800.
Register addressing: Load AC, R1
The accumulator is loaded from the register. AC = 0x800 AC
¬ (R1)
Register Indirect addressing
The accumulator is loaded from the address pointed to by
the register.
Here AC = 0x900. AC ¬ M[ (R1) ], the memory
addresses by the contents of R1.
Indexed
Addressing
This
is used to support array addressing in all computers. Languages such as C and
C++ implement this directly using “zero based” arrays.
The
contents of the register are added to the address field to form the effective
address.
EA
= (Address Field) + (Register)
Indexed
Addressing (Example)
Consider
again the memory map taken from the textbook.
We assume the MARIE has
an AC and one registers: R1.
Use
R1 in the address calculations. Suppose
X = 0x200.
Load X[R1] //
Syntax is made up to look reasonable. AC
is loaded.
EA = X + (R1) = 0x200 + 0x800 = 0xA00.
AC ¬ M[0xA00], whatever that is.
Another
Indexed Addressing Example
Suppose
now that X refers to address 0x800 and that register R1 contains
a small number, say (R1) = 0x02.
Consider
the instruction Load X[R1]
The
effective address is EA = X + (R1) = 0x800 + 0x02 = 0x802.
This
can be viewed as X[2] where X is a zero–based
array. Its memory
layout appears similar to the figure below.
Memory
Map |
0x800 |
0x801 |
0x802 |
0x803 |
|
X[0] |
X[1] |
X[2] |
X[3] |
Indexed–Indirect
Addressing
This
is a combination of both modes.
Question: Which is done first.
Preindexed–indirect. The indexing is done first. We have an array of pointers.
Postindexed–indirect. The indirect is done first. We have a pointer to an array.
Sample
Problem (from CPSC 5155)
This
question references the following memory map.
Address |
10 |
11 |
12 |
13 |
14 |
15 |
Contents |
13 |
7 |
4 |
86 |
46 |
77 |
If the symbol
X stands for address 12 and (%R6) == – 1,
what is the effective address for
a) STR %R2, X direct
addressing
b) STR %R2, *X indirect
addressing
c) STR %R2, X, 6 indexed
by register %R6.
d) STR %R2, *X, 6 pre-indexed indirect, indexed by %R6.
Analysis: First a few comments on the problem and then
we start.
1. Question: Are the addresses decimal or hexadecimal?
Answer: This is ambiguous, but not important. We need to compute (12 – 1),
which is 11 in
each of decimal and hexadecimal.
2. Question: What does STR do? Answer:
We don’t care. We just want the address.
Sample
Problem (Page 2)
Address |
10 |
11 |
12 |
13 |
14 |
15 |
Contents |
13 |
7 |
4 |
86 |
46 |
77 |
The symbol X stands for address 12 and (%R6)
== – 1,
a) STR %R2, X direct addressing
The
label X refers to address 12. In direct
addressing that is the address. EA = 12.
PLEASE
NOTE: The contents of address 12 are
4; M[12] == 4.
In a
higher level language, we would say “X is 4”, because the
memory
location contains that value.
In this context,
we say “X references the address 12”.
b) STR %R2, *X indirect addressing
The
label X refers to address 12. For
indirect addressing, the address is M[X],
here
M[12] or 4. Note that contents of
address 4 are not specified. However, EA = 4.
Sample
Problem (Page 3)
Address |
10 |
11 |
12 |
13 |
14 |
15 |
Contents |
13 |
7 |
4 |
86 |
46 |
77 |
The symbol X stands for address 12 and (%R6)
== – 1,
c) STR %R2, X, 6 indexed
by register %R6.
The
effective address is X + (%R6), which is 12 + (–1) or 12 – 1 = 11. EA =
11.
NOTE: Do not add the contents of the address
associated with X.
This
would be another addressing mode.
d) STR %R2, *X, 6 pre-indexed indirect, indexed by %R6.
The
effective address is M[X + (%R6)].
Do
the indexing first. X + (%R6) is 12 +
(–1) = 12 – 1 = 11.
The
effective address is M[11] = 7. EA = 7.