You are on page 1of 37

ADDRESSING MODES

Dr.H.S.Nagendraswamy, DoS in
CS, UoM, MGM 1
The term addressing mode refers to the way the operand
of an instruction is specified. Different types of
addressing modes are:

 Immediate
 Direct
 Indirect
 Register
 Register Indirect
 Displacement (Indexed)
 Auto Increment and Auto Decrement
 Stack Dr.H.S.Nagendraswamy, DoS in
CS, UoM, MGM 2
Immediate Addressing Mode

 Operand is part of instruction


 Operand = address field
 e.g. ADD 5
 Add 5 to contents of accumulator
 5 is operand
 No memory reference to fetch data
 Fast
 Limited range
 This mode is used in specifying address and data
constants in programs.
Dr.H.S.Nagendraswamy, DoS in
CS, UoM, MGM 3
Immediate Addressing Mode Diagram

Instruction

Opcode Operand

Dr.H.S.Nagendraswamy, DoS in
CS, UoM, MGM 4
Direct Addressing Mode (Absolute Mode)

 Address field contains address of operand


 Effective address (EA) = address field (A)
 e.g. ADD A
 Add contents of cell A to accumulator
 Look in memory at address A for operand
 Single memory reference to access data
 No additional calculations to work out effective
address
 Limited address space
Dr.H.S.Nagendraswamy, DoS in
CS, UoM, MGM 5
Direct Addressing Mode Diagram

Instruction
Opcode Address A
Memory

Operand

Dr.H.S.Nagendraswamy, DoS in
CS, UoM, MGM 6
Indirect Addressing Mode

 Memory cell pointed to by address field contains the


address of (pointer to) the operand
 EA = (A)
 Look in A, find address (A) and look there for
operand
 e.g. ADD (A)
 Add contents of cell pointed to by contents of A to
accumulator

Dr.H.S.Nagendraswamy, DoS in
CS, UoM, MGM 7
 Large address space
 2n where n is the word length
 May be nested, multilevel, cascaded
 e.g. EA = (((A)))
 Multiple memory accesses to find operand
 Hence slower

Dr.H.S.Nagendraswamy, DoS in
CS, UoM, MGM 8
Through a memory location Through a general purpose
register

Add (A) , R0 Add (R1) , R0

A B B Operand
Main Memory

B Operand R1 B
Register
Dr.H.S.Nagendraswamy, DoS in
CS, UoM, MGM 9
Indirect Addressing Mode Diagram

Instruction
Opcode Address A
Memory

Pointer to operand

Operand

Dr.H.S.Nagendraswamy, DoS in
CS, UoM, MGM 10
Example program for indirect addressing

Address Contents

Move N, R1
Move #NUM1, R2 Initialization
Clear R0
LOOP Add (R2), R0
Increment R2
Decrement R1
Branch > 0 LOOP
Move R0, SUM

Dr.H.S.Nagendraswamy, DoS in
CS, UoM, MGM 11
Register Addressing Mode

 Operand is held in register named in address field


 EA = R
 Limited number of registers
 Very small address field needed
 Shorter instructions
 Faster instruction fetch
 No memory access
 Very fast execution
Dr.H.S.Nagendraswamy, DoS in
CS, UoM, MGM 12
 Very limited address space
 Multiple registers helps performance
 Requires good assembly programming or compiler
writing
 C programming
register int a;

Dr.H.S.Nagendraswamy, DoS in
CS, UoM, MGM 13
Register Addressing Mode Diagram

Instruction
Opcode Register Address R
Registers

Operand

Dr.H.S.Nagendraswamy, DoS in
CS, UoM, MGM 14
Register Indirect Addressing Mode

 Indirect addressing
 EA = (R)
 Operand is in memory cell pointed to by contents of
register R
 Large address space (2n)
 One fewer memory access than indirect addressing

Dr.H.S.Nagendraswamy, DoS in
CS, UoM, MGM 15
Register Indirect Addressing Mode Diagram

Instruction
Opcode Register Address R
Memory

Registers

Pointer to Operand Operand

Dr.H.S.Nagendraswamy, DoS in
CS, UoM, MGM 16
Index (Displacement) Addressing Mode

 Aeff = X + (R)
 Address field hold two values
 X = base value
 R = register that holds displacement
 or vice versa

Dr.H.S.Nagendraswamy, DoS in
CS, UoM, MGM 17
Index (Displacement) Addressing Diagram

Instruction
Opcode Register R Address A
Memory

Registers

Pointer to Operand + Operand

Dr.H.S.Nagendraswamy, DoS in
CS, UoM, MGM 18
Address Contents

Move LIST, R0
Move N, R1
Initialization
Clear R2
Clear R3
LOOP Add 2(R0), R2
Add 3(R0), R3
Add #4, R0
Decrement R1
Branch > 0 LOOP
Move R2, SUM2
Move R3, SUM3

Indexed addressing used to access


Dr.H.S.Nagendraswamy, DoS in test scores
CS, UoM, MGM 19
Autoincrement and Autodecrement Mode

Autoincrement mode: The effective address of the operand is the


contents of a register specified in the instruction. After accessing the
operand, the contents of this register are incremented to the nest item
in a list.

(R4)+

Autodecrement mode: The contents of a register specified in the


instruction are decremented. These contents are then used as the
effective address of the operand
Dr.H.S.Nagendraswamy, DoS in
-(R4)
CS, UoM, MGM 20
Stack Addressing Mode

 Operand is (implicitly) on top of stack


Example:
ADD Pop top two items from stack
and add

Dr.H.S.Nagendraswamy, DoS in
CS, UoM, MGM 21
Instruction Formats

 Layout of bits in an instruction


 Includes opcode
 Includes (implicit or explicit) operand(s)
 Usually more than one instruction format in an
instruction set

Dr.H.S.Nagendraswamy, DoS in
CS, UoM, MGM 22
Instruction Length

 Affected by and affects:


 Memory size
 Memory organization
 Bus structure
 CPU complexity
 CPU speed
 Trade off between powerful instruction repertoire
and saving space

Dr.H.S.Nagendraswamy, DoS in
CS, UoM, MGM 23
Assembly Language
 Assembly Level Language uses mnemonics
 The set of rules for using mnemonics in the
specification of complete instructions and programs
is called the syntax of the language.
MOVE R0, SUM
ADD #5, R3
ADDI 5, R3
MOVE #5, (R2)
MOVEI 5, (R2)
Dr.H.S.Nagendraswamy, DoS in
CS, UoM, MGM 24
Assembler Commands
In addition to providing a mechanism for representing
instructions in a program, the assembly language allows the
programmer to specify other information needed to translate the
source program into the object program. Suppose that the name
SUM is used to represent the value 200. This fact may be
conveyed to the assembler program through a statement such as
SUM EQU 200
The above statement is not an instruction that will be executed
when the object program is run. It merely informs the assembler
that the name SUM should be replaced by the value 200
whenever it appears in the program. Such statements, called
assembler commands, are used by the assembler while it
Dr.H.S.Nagendraswamy, DoS in
translates a source program intoUoM,
CS, anMGM
object program. 25
Stacks and Queues
A stack is a list of data elements, usually words or bytes, with the
accessing restriction that elements can be added or removed at
one end of the list only. This end is called the top of the stack and
the other end is called the bottom. The structure is sometimes
referred to a pushdown stack. The terms push and pop are used to
describe placing new item on the stack and removing the top
item from the stack respectively.
Data stored in the main memory of a computer can be organized
as a stack with successive elements occupying successive
memory locations.
The following figure shows a stack of data items in the main
memory of a computer. It contains numerical values with 43 at
Dr.H.S.Nagendraswamy, DoS in
the bottom and -28 at the top.CS, UoM, MGM 26
A stack in the main memory

Stack
pointer
register
Current top
SP -28
element
17
739
Stack

Bottom
BOTTOM 43
element

2k - 1
Dr.H.S.Nagendraswamy, DoS in
CS, UoM, MGM 27
A CPU register is used to keep track of the address of the element of
the stack that is at the top at any given time. This register is called the
stack pointer. It could be one of the general-purpose registers or a
register dedicated to this function. The push operation can now be
implemented with the following instructions, where SP represents the
stack pointer:
Decrement SP
MOVE NEWITEM, (SP)

The pop operation can be performed with


MOVE (SP), ITEM
Dr.H.S.Nagendraswamy, DoS in
Increment SP
CS, UoM, MGM 28
Effect of stack operations on the stack

SP 19
-28 -28
17 SP 17
Stack 739 739

43 43

NEWITEM 19 NEWITEM 19

ITEM ITEM -28

(a) After push from NEWITEM


Dr.H.S.Nagendraswamy, DoS in
(b) After pop into ITEM
CS, UoM, MGM 29
Queue Data Structure
Another useful data structure that is similar to the stack is called a
queue. Data are stored in and retrieved from a queue on a first-in-
first-out (FIFO) basis. Thus, if we assume that the queue grows in the
direction of increasing addresses in the memory, new data are added
at the back (high-address end) and retrieved from the front (low-
address end) of the queue. Two pointers are needed to keep track of
the two ends of the queue. Both the ends of a queue move to higher
addresses as data are added at the back and removed from the front.

Dr.H.S.Nagendraswamy, DoS in
CS, UoM, MGM 30
Subroutines
In a given problem, it is often necessary to perform a particular task
many times on different data values. A repeated task is normally
implemented as a subroutine.
A set of instructions that constitute a subroutine is placed in the main
memory and any program that requires the use of the subroutine
simply branches to its starting location. When a program branches to
a subroutine we say that it is calling the subroutine. The instruction
that performs this branch operation is called a Call-subroutine
instruction.
Dr.H.S.Nagendraswamy, DoS in
CS, UoM, MGM 31
Call_subroutine instruction is a special branch instruction that
performs the following operations.
 Store the contents of the PC in the link register.
 Branch to the target address specified by the instruction
After a subroutine has been executed, the calling program must
resume execution, continuing immediately after the instruction that
called the subroutine. The subroutine is said to return to the program
that called it. The location where the calling program resumes
execution is the location pointed to by the PC while the
Dr.H.S.Nagendraswamy, DoS in
Call_subroutine instruction is being executed.
CS, UoM, MGM 32
Hence, the contents of the PC must be saved by the Call_subroutine
instruction to enable correct return to the calling program.
The way in which a computer makes it possible to call and return
from subroutine is referred to as its subroutine linkage method. The
simplest subroutine linkage method is to save the return address in a
specific location, which may be a register dedicated to this function.
Such a register is called the link register. The subroutine then returns
to the calling program by branching indirectly through the link
register.
Dr.H.S.Nagendraswamy, DoS in
CS, UoM, MGM 33
The Return_from_subroutine instruction perform operation.

 Branch to the address contained in the link register.

Memory Calling Memory


Subroutine SUB
location program location

200 Call_subroutine SUB 1000 first instruction


201 next instruction
Return_from_subroutine
Dr.H.S.Nagendraswamy, DoS in
CS, UoM, MGM 34
Before After After
Call Call Return

PC 201 1000 201

Link
Register 201 201 201

Subroutine linkage using a link register

Dr.H.S.Nagendraswamy, DoS in
CS, UoM, MGM 35
Subroutine Nesting
Memory Main Subroutine Subroutine
location program FACT MULT

first instruction
first instruction
200 Call FACT
201 next instruction Call MULT
next instruction

Return
Return

250 END

Dr.H.S.Nagendraswamy, DoS in
Illustration of subroutine nesting
CS, UoM, MGM 36
Program to illustrate Subroutine

Label Instructions Remarks


Calling program
Move N, R1 : R1 used as a counter
Move NUM1, R2 : R2 used as pointer
Clear R0 : R0 initialized to 0
Call ARRAY_ADD : Call subroutine
Move R0, SUM : Store the result in memory

END
Subroutine
ARRAY_ADD Add (R2)+, R0 : Add entry from the list to
Decrement R1 : previous sum in R0 till all
Branch > 0 ARRAY_ADD : the numbers are added
Return Dr.H.S.Nagendraswamy, DoS in: Return to the calling program
CS, UoM, MGM 37

You might also like