Anda di halaman 1dari 32

Addressing Modes

• Program operates on data that reside in the


computer memory.
• The data can be organized in variety of ways.
• Programmers use data structures to organize
data like lists, queues, arrays etc.
• A compiler uses the facilities provided by ISA
to translate a high level language into
assembly code.
• The different ways in which the location of
an operand is specified in an instruction are
referred as addressing modes
A. Implementation of variables and
constants.
• These are simplest data types.
• A variable is represented by allocating a
register or memory location.
• Accessed by specifying the name of the
register or the address of memory location
where the operand is located.
• Two addressing modes (for variable)used are:
Register Mode and Absolute mode.
• One addressing mode for constant: Immediate
1. Register Mode-
• The operand is the contents of a processor
register, the name of the register is given in the
instruction.
2. Absolute mode/ Direct mode
• The operand is in a memory location, the
address of this location is given explicitly in
the instruction.
Example: Move LOC, R2
3. Immediate Mode: The operand is given
explicitly in the instruction.
Move 200 immediate, R0.
OR
Move #200, R0.
Write ALP for the instruction.
A=B+6
Move B, R1
Add #6, R1
Move R1, A
B. Implementation of Indirection and
Pointers.
• Sometimes the register/variable that holds the
address of the operand specified .
4. Indirect Mode: The Effective Address(EA) of
the operand is the contents of a register or
memory location whose address
• EA- The address of the data to be manipulated
or accessed.
• Specified by placing parenthesis around the
register or memory operand.
Example
Program: To add N numbers and store result in
SUM.
Move N, R1
Move #NUM1, R2 ---- R2 stores address of NUM1
Clear R0
LOOP Add (R2), R0
Add #4,R2
Decrement R1
Branch>0 LOOP
Move R0,SUM
C. Indexing and Arrays
• To access arrays the base address is necessary,
this is stored in index register.
5. Index Mode- The EA of the operand is
generated by adding a constant value to the
contents of a register.
• The register used is referred as index register.
• Symbolically denoted as: X(Ri).
• X denotes the constant value in the instruction.
• Ri is the register involved.
• EA= X+ [Ri]
Index Addressing with X representing the offset.
Example Program:
Consider the list of scores, beginning at location LIST, every
student record is stored in 4 words. There are N students in
the class. Memory is byte addressable and word length is
32bits. Write ALP to find sum of test scores and store in
SUM1, SUM2, SUM3.
Move #list, R0
Clear R1
Clear R2
Clear R3
Move N, R4
Loop Add 4(R0), R1,
Add 8(R0), R2,
Add 12(R0), R3,
Add #16,R0
Decrement R4
Branch >0 Loop
Move R1, SUM1
Move R2, SUM2
Move R3, SUM3
Variants of Index Addressing Mode
1. The offset may be stored in another register.
This can be denoted as: (Ri, Rj)
The EA= [Ri]+[Rj]
2. Two register and a constant. This is denoted
as: X(Ri, Rj)
The EA= [Ri]+[Rj]+ X
D. Relative Addressing – Indexing relative to
PC
• Using address stored in PC to compute the EA.
6. Relative Mode- The effective address is
determined by the index mode using PC in
place of general purpose(index) register.
• Represented as X(PC)
• EA = X+ [PC]
984 Move N, R1
988 Move #NUM1, R2
992 Clear R0
LOOP 1000 Add (R2), R0
1004 Add #4,R2
1008 Decrement R1
1012 Branch>0 LOOP PC:1016
If Branch condition is true then
1016 Move R0,SUM PC should be loaded with 1000
i.e, -16(PC)
Additional Modes
7. Autoincrement/ Autodecrement mode- The EA
of the operand is contents of a register
specified in the instruction.
• After accessing the operand the contents of
this register are automatically incremented to
point to next item in the list.
• Denoted as: (Ri)+ and - (Ri)
• This mode increments the contents of the
register by a value that corresponds to the size
of accessed operand.
984 Move N, R1
988 Move #NUM1, R2
992 Clear R0
LOOP 1000 Add (R2)+, R0
1004 Decrement R1
1008 Branch>0 LOOP
1012 Move R0,SUM
Summary
EA’s
1. Register - All effective addresses (both source
and destination) are registers, named in the
operand.
2. Direct - The effective address is a memory
address specified in the operand.
3. Immediate - The effective address is the
address of instruction's operand.
4. Indirect - The effective address is stored in a
register (register indirect) or memory location
(memory indirect) specified by the operand -
essentially a pointer.
5. Indexed - The effective address is a
combination of a specified address or register
contents and a second register (the index).
6. Relative - The effective address is found by
adding the operand treated as a signed offset to
the program counter.
7. Displacement - The effective address is a
combination of address stored in a named
register (or registers) and a hard coded offset
value.
Assembly Language
• Machine instructions are represented by
patterns of 0’s and 1’s – Difficult to write.
• Symbolic names are used to represent the
patterns in Assembly level languages.
• For a specific computers mnemonics are
provided such as MOV, ADD, INC, ADDI etc.
• A complete set of mnemonics and rules for
their use(Syntax) constitute a programming
language.
• Programs written in ALP are translated to
machine codes using Assembler.
• A assembler is a system software and collection
of utility programs.
• Every mnemonics represents a binary pattern or
opcode.
• The assembler translates the mnemonic to the
opcode.
• The opcode is followed by one blank space and is
followed by the operands.
• Using various available addressing modes the
operand location is specified in the instruction.
Assembler Directives
1. The assembly language allows the programmer
to specify other information needed to translate
source program to object program.
2. For example: SUM EQU 200. (SUM is replaced
by 200)
3. Such instructions will not be assembled and do
not appear in object code.
Defn 1: Commands used by assembler to translate a
source program to object code.
Defn 2:Assembler directives are instructions that
direct the assembler to do something.
Various Assembler directives:
1. EQU(Equate)- Assigns a value(expression) to
a symbol. (e.g., TWENTY EQU 20 )
2. ORIGIN- Sets Program Origin or Data
Origin.
3. DATAWORD – Defines Constant words.
4. DATABYTE – Defines constant bytes
5. RESERVE- Reserves memory in Bytes.
6. END – Tells where the program ends.
7. START - Tells the beginning of the program.
MEMORY ADDRESSES FOR INSTRUCTION AND DATA

100 Move N, R1

104 Move #NUM1, R2


108 Clear R0
112LOOP Add (R2), R0
116 Add #4,R2 CODE
120 Decrement R1
124 Branch>0 LOOP
128 Move R0,SUM

SUM 200
N 204 100 DATA
NUM1 208 10
NUM2 212 20
. .
. .
.
NUMn 604 .
SOURCE PROGRAM TO ADD N NUMBERS

Memory Addressing or
Address data
Label Operation information
Assembler
Directives SUM EQU 200
ORIGIN 204
N DATAWORD 100
NUM1 RESERVE 400
ORIGIN 100

Statements that
generate
machine
instructions START MOVE N,R1
MOVE #NUM1, R2
CLR R0
LOOP ADD (R2),R0
ADD #4,R2
DEC R1
BGTZ LOOP
MOVE R0,SUM
RETURN
END START
ALP to add N numbers.
• A program is written by specifying all the
information needed to generate the
corresponding object program.
• Assumption- each instruction occupies one
word and the memory is byte addressable.
• EQU informs about the SUM.
• The first ORIGIN directive tells the assembler
about the data that has to be placed in the
memory.(Here it is 204)
• To load a value 100 into a memory location
DATAWORD directive is used.
N DATAWORD 100
value 100 is placed in the memory at address
204 and is labelled as N.
• RESERVE directs assembler to reserve 400
bytes of data and name NUM1 is to be
associated with address 208.
• The second ORIGIN directive specifies that
instructions of object program are loaded in
memory starting at address 100.
• The END directive tells assembler it is end of
the source program, it includes label START.
• RETURN assembler directive tells the point
where the execution of the program must be
terminated.
• There are 5 labels in the program- SUM, N,
NUM1, START, LOOP.
Assembly and Execution of Programs
• A source program written in an assembly
language is assembled to object program by
assembler.
• The assembler replaces all symbols and
addressing modes with binary codes, all labes
and names with actual values/ addresses.
• The data block is given the addresses.
• Inserts constants given in DATAWORD.
• Reserves the memory as specified by RESERVE
directive.
Data structures used by assembler.
• Symbol Table(SYMTAB)- to store addresses
assigned to the labels.
• Operation Code Table(OPTAB)- lookup
mnemonic operation codes and translate
them to machine language equivalents.
Number Notation
Decimal: operand is preceeded by #.
e.g., ADD #93, R1
Binary: Binary value of operand is preceded by
#%
e.g., ADD #%1011101, R1
Hexadecimal: Hexadecimal value of operand is
preceeded by #$
e.g., ADD #$5D, R1

Anda mungkin juga menyukai