Upload
chinnu-chintus
View
244
Download
0
Embed Size (px)
Citation preview
8/8/2019 Addressing Modes New
1/37
ADDRESSING MODES
8/8/2019 Addressing Modes New
2/37
Immediate
Direct
Indirect
Register
Register Indirect
Displacement (Indexed)
Auto Increment and Auto Decrement
Stack
The term addressing mode refers to the way the operand
of an instruction is specified. Different types of
addressing modes are:
8/8/2019 Addressing Modes New
3/37
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.
8/8/2019 Addressing Modes New
4/37
Immediate Addressing Mode Diagram
OperandOpcode
Instruction
8/8/2019 Addressing Modes New
5/37
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
8/8/2019 Addressing Modes New
6/37
Direct Addressing Mode Diagram
Address AOpcode
Instruction
Memory
Operand
8/8/2019 Addressing Modes New
7/37
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
8/8/2019 Addressing Modes New
8/37
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
8/8/2019 Addressing Modes New
9/37
Add (A) , R0
B
OperandB
A
Add (R1) , R0
OperandB
BR1
Main Memory
Register
Through a memory location Through a general purpose
register
8/8/2019 Addressing Modes New
10/37
Indirect Addressing Mode Diagram
Address AOpcode
Instruction
Memory
Operand
Pointer to operand
8/8/2019 Addressing Modes New
11/37
Address Contents
Move N, R1
Move #NUM1, R2
Clear R0
LOOP Add (R2), R0
Increment R2
Decrement R1Branch > 0 LOOP
Move R0, SUM
Initialization
Example program for indirect addressing
8/8/2019 Addressing Modes New
12/37
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 fetchNo memory access
Very fast execution
8/8/2019 Addressing Modes New
13/37
Very limited address space
Multiple registers helps performance
Requires good assembly programming or compiler
writing
C programming
register int a;
8/8/2019 Addressing Modes New
14/37
Register Addressing Mode Diagram
Register Address ROpcode
Instruction
Registers
Operand
8/8/2019 Addressing Modes New
15/37
Register Indirect Addressing Mode
Indirect addressing
EA = (R)
Operand is in memory cell pointed to by contents ofregister R
Large address space (2n)
One fewer memory access than indirect addressing
8/8/2019 Addressing Modes New
16/37
Register Indirect Addressing Mode Diagram
Register Address ROpcode
Instruction
Memory
OperandPointer to Operand
Registers
8/8/2019 Addressing Modes New
17/37
Index (Displacement) Addressing Mode
Aeff= X + (R)
Address field hold two values
X = base value
R = register that holds displacement
or vice versa
8/8/2019 Addressing Modes New
18/37
Index (Displacement) Addressing Diagram
Register ROpcode
Instruction
Memory
OperandPointer to Operand
Registers
Address A
+
8/8/2019 Addressing Modes New
19/37
Address Contents
Move LIST, R0
Move N, R1
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
Initialization
Indexed addressing used to access test scores
8/8/2019 Addressing Modes New
20/37
Autoincrement and Autodecrement Mode
Autoincrement Autoincrement modemode: 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 itemin a list.
(R4)+
Autodecrement Autodecrement modemode: The contents of a register specified in the
instruction are decremented. These contents are then used as the
effective address of the operand
-(R4)
8/8/2019 Addressing Modes New
21/37
Stack Addressing Mode
Operand is (implicitly) on top of stack
Example:
ADD Pop top two items from stackand add
8/8/2019 Addressing Modes New
22/37
8/8/2019 Addressing Modes New
23/37
Instruction Length
Affected by and affects:
Memory size
Memory organization Bus structure
CPU complexity
CPU speed
Trade off between powerful instruction repertoireand saving space
8/8/2019 Addressing Modes New
24/37
Assembly Language
Assembly Level Language uses mnemonics
The set of rules for using mnemonics in the
specification of complete instructions and programsis called the syntax of the language.
MOVE R0, SUM
ADD #5, R3
ADDI 5, R3
MOVE #5, (R2)
MOVEI 5, (R2)
8/8/2019 Addressing Modes New
25/37
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
translates a source program into an object program.
8/8/2019 Addressing Modes New
26/37
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 successivememory locations.
The following figure shows a stack of data items in the main
memory of a computer. It contains numerical values with 43 at
the bottom and -28 at the top.
8/8/2019 Addressing Modes New
27/37
BOTTOM
2k - 1
SP
43
-28
17
739
Stack
pointer
register
0
Stack
Current top
element
Bottom
element
A stack in the main memory
8/8/2019 Addressing Modes New
28/37
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 beimplemented 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
Increment SP
8/8/2019 Addressing Modes New
29/37
SP
43
-28
17739
19
19NEWITEM
ITEM
StackSP
43
-28
17739
19
-28
NEWITEM
ITEM
(a) After push from NEWITEM (b) After pop into ITEM
Effect of stack operations on the stack
8/8/2019 Addressing Modes New
30/37
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.
Queue Data Structure
8/8/2019 Addressing Modes New
31/37
SubroutinesIn 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.
8/8/2019 Addressing Modes New
32/37
Call_subroutine instruction is a special branch instruction that
performs the following operations.
j Store the contents of the PC in the link register.
j 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
Call_subroutine instruction is being executed.
8/8/2019 Addressing Modes New
33/37
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 returnfrom 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.
8/8/2019 Addressing Modes New
34/37
The Return_from_subroutine instruction perform operation.
j
Branch to the address contained in the link register.
Memory
location
Calling
program
Memory
locationSubroutine SUB
200 Call_subroutine SUB 1000 first instruction
201 next instruction
Return_from_subroutine
8/8/2019 Addressing Modes New
35/37
201
201
PC
Link
Register
Before
Call
201
201
Subroutine linkage using a link register
1000
201
After
Call
After
Return
8/8/2019 Addressing Modes New
36/37
Memory
location
Main
program
Subroutine
FACT
Subroutine
MULT
200 Call FACT
201 next instruction
Subroutine Nesting
first instruction
Call MULT
next instruction
Return
first instruction
Return
END250
Illustration of subroutine nesting
8/8/2019 Addressing Modes New
37/37
Program to illustrate Subroutine
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
SubroutineARRAY_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 : Return to the calling program
Label Instructions Remarks