Chapter 2.3
Chapter 2.3
2
3
4
• All computer architectures support more than one of these addressing
modes.
• How does the process determine the addressing mode used in an
instruction?
• Different opcodes will use different addressing modes.
• One or more bits used in the instruction format (mode field). The
value of the mode field determines which addressing mode is to be used.
5
• Immediate Addressing
• The simplest form of addressing is immediate addressing
• The operand value is present in the instruction
• Operand = A
• Can be used to define and use constants or set initial values of variables.
• Typically, the number will be stored in twos complement form; the leftmost bit
of the operand field is used as a sign bit.
• Advantage of immediate addressing:
• No memory reference other than the instruction fetch is required to obtain
the operand
• saving one memory or cache cycle in the instruction cycle.
• Disadvantage: the size of the number is limited to the size of the address field,
which is small compared with the word length.
6
• Direct Addressing
• A very simple form of addressing
• The address field contains the effective address of the operand:
• EA = A
• Disadvantage: provides only a limited address space.
• Indirect Addressing
• With direct addressing:
• length of the address field < word length => limiting the address range
• One solution is to have the address field refer to the address of a word in memory,
• This in turn contains a full-length address of the operand.
• This is known as indirect addressing:
• EA = (A), ( ) stands for “contents of”
7
• Advantage: for a word length of N, an address space of 2N is available.
8
• If n-bits are used for the address field that references registers
• Typically n is 3 to 5 bits
• Registers are used for operands that remain in use for multiple operations. (for
example for intermediate results).
• It would be wasteful if every operand is brought into a register from main memory,
operated on once, and then returned to main memory.
9
• Register Indirect Addressing
• EA = (R)
• The advantages and limitations of register indirect addressing are the same as for
indirect addressing.
• In both cases, the address space limitation (limited range of addresses) of the
address field is overcome by having that field refer to a word length location
containing an address.
• In addition, register indirect addressing uses one less memory reference than
indirect addressing. 10
• Displacement Addressing
• A very powerful mode of addressing combines the capabilities of direct
addressing and register indirect addressing.
• It is known by a variety of names depending on the context of its use, but the
basic mechanism is the same.
• We will refer to this as displacement addressing:
• EA = A + (R)
• Displacement addressing requires that the instruction have two address fields, at
least one of which is explicit.
• The value contained in one address field (value = A) is used directly.
• The other address field, or an implicit reference based on opcode, refers to a
register whose contents are added to A to produce the effective address.
11
• Displacement Addressing
12
• Three common uses of displacement addressing:
• Relative addressing
• Base-register addressing
• Indexing
• RELATIVE ADDRESSING (also called PC-relative addressing)
• That is, the next instruction address is added to the address field to produce
the EA.
• Typically, the address field is treated as a twos complement number for this
operation.
• Thus, the effective address is a displacement relative to the address of the
current instruction. 13
• BASE-REGISTER ADDRESSING
• Interpreted as follows:
15
• An important use of indexing is to provide an efficient mechanism for performing iterative
operations.
• For example:
• The value A is stored in the instruction’s address field, and the chosen register, called an
index register, is initialized to 0.
16
• After each operation, the index register is incremented by 1.
• Because index registers are commonly used for such iterative tasks, it is typical that
there is a need to increment or decrement the index register after each reference to it.
• Because this is such a common operation, some systems will automatically do this as
part of the same instruction cycle.
18
• With preindexing, the indexing is performed before the indirection:
• EA = (A + (R))
• An address is calculated as with simple indexing.
• In this case, the calculated address contains not the operand, but the address of the
operand.
• An example of the use of this technique is to construct a multiway branch table.
• At a particular point in a program, there may be a branch to one of a number of
locations depending on conditions.
• A table of addresses can be set up starting at location A.
• By indexing into this table, the required location can be found.
19
• Stack Addressing
• Items are appended to the top of the stack so that, at any given time, the block is
partially filled.
• Associated with the stack is a pointer whose value is the address of the top of the stack.
• Alternatively, the top two elements of the stack may be in processor registers.
20
• In this case, the stack pointer references the third element of the stack.
• The machine instructions need not include a memory reference but implicitly
operate on the top of the stack.
21
ASSEMBLY LANGUAGE
• A processor can understand and execute machine instructions.
N=I+J+K
23
Cont.…
• The program starts in location 101 (hexadecimal).
• Memory is reserved for the four variables starting at location 201.
• The program consists of four instructions:
• Load the contents of location 201 into the AC.
• Add the contents of location 202 to the AC.
• Add the contents of location 203 to the AC.
• Store the contents of the AC in location 204.
• This is clearly a tedious and very error-prone process.
• A slight improvement is to write the program in hexadecimal rather than binary
notation. Shown in (b).
24
Cont.…
• We could write the program as a series of lines.
• Each line contains the address of a memory location and the hexadecimal code of
the binary value to be stored in that location.
• Then we need a program that will accept this input, translate each line into a
binary number, and store it in the specified location.
• For more improvement, we can make use of the symbolic name or mnemonic of
each instruction.
• This results in the symbolic program shown in (c).
• Each line of input still represents one memory location.
• Each line consists of three
25
LMC (Little Man Computer)
LMC Instruction Set
Example:
• List of instructions (and addresses they are stored at) to accept two number from
input, add them and output result Address Instruction
Address Instruction
00 INP
01 STA 99
02 INP
03 ADD 99
04 OUT
05 HLT
Cont.….
INP Input a number and put it in the accumulator
HLT Halt