Addressing Modes

Download Report

Transcript Addressing Modes

Addressing Modes
1.
Immediate

2.
Register Direct


3.
5.
6.
(An)
(An)+
the address of the data is in the address register
Predecrement

(xxx).W or (xxx).L
data is in a memory location
the address of the data is the address of the memory
location
Address Register Indirect
Postincrement

Dn or An
data is in a register
the address of the data is the register number
Memory Direct


4.
data is part of the instruction
#<data> or Imm
-(An)
the address of the data is the contents of the address
register minus the decrement
Address Register Indirect
7.
… variations
Address register indirect with displacement d(An)
 operand is written as d(An)
where d = 16 bit displacement
 the address of the data is the sum of the
address register and the displacement
instruction
d An
registers
memory
address
data
displacement
Address Register Indirect w/ displacement
Rows
Cols
Loop
Table
EQU
EQU
…
LEA
MOVE.B
…
ADDA.L
CMPA.L
BNE
…
DC.B
DC.B
DC.B
3
4
Table,A0
3(A0),D0
#Cols,A0
#Table+Rows*Cols,A0
Loop
15,14,13,12
9,8,7,6
4,3,2,1
Address Register Indirect
8.
… variations
Address register indirect with indexing d(An,Rn)
 operand is written as d(An,Ri{.W|.L})
where d = 8 bit displacement and
Ri is an index register (W or L)
 the address of the data is the sum of the
address register, the index register, and the
displacement
instruction
registers
memory
address
data
d Ri An
index
displacement
Address Register Indirect w/ indexing
Rows
Cols
EQU
EQU
3
4
…
Loop
LEA
Table,A0
MOVE.W #0,D1
MOVE.B 3(A0,D1.W),D0
…
ADDI.W #Cols,D1
CMPI.W #Rows*Cols,D1
BNE
Loop
Table
…
DC.B
DC.B
DC.B
15,14,13,12
9,8,7,6
4,3,2,1
SOURCE ONLY – except for BTST, branches, jumps
Program Counter Relative
9.
… variations
Program counter relative w/ displacement d(PC)
 operand is written as d(PC)
where d = 16 bit displacement
 the address of the data is the sum of the
program counter and the displacement
instruction
d PC
registers
memory
address
data
displacement
Program Counter Relative
9.
… variations
Program counter relative w/ displacement d(PC)
 operand is written as label(PC)
where label refers to a label in the program
 when reading code, treat as if (PC) not there
 the use of a label reference causes the
assembler to calculate an offset to the PC
instruction
label PC
label - PC
= offset
registers
instruction
address
memory
data
Program Counter Relative
e.g.
MOVE.W
$50(PC),D2
e.g.
MOVEA.L
…
DC.L
WK(PC),A0
JMP
…
MOVEQ.B
NEXT(PC)
WK
e.g.
NEXT
WK_CASE
#1,D0
… examples
If instruction located at $2050,
then
<ea> = $2052+$50 =$20A2
Move contents from memory
location $20A2 to D2.
If instruction located at $1000 and
WK located at $1508, then
offset = $1508 - $1002 = $0506
<ea> = $1002 + $0506 = $1508
Move address WK_CASE to A0.
If instruction located at $3000 and
NEXT located at $3052, then
offset = $3052 - $3002 = $0050
<ea> = $3002 + $0050 = $3052
Jump to NEXT.
Program Counter Relative
10.
… variations
Program counter relative w/ indexing d(PC,Rn)
 operand is written as d(PC,Ri{.W|.L})
where d = 8 bit displacement
 the address of the data is the sum of the PC,
the index register, and the displacement
instruction
d Ri PC
registers
memory
PC
data
Ri
displacement
Program Counter Relative
10.
… variations
Program counter relative w/ indexing d(PC,Rn)
 operand is written as label(PC,Ri{.W|.L})
where label refers to a label in the program
 the use of a label reference causes the
assembler to calculate an offset to the PC
instruction
label PC Ri
registers
Ri
PC
label - PC
= offset
memory
data
Program Counter Relative
… example
e.g. print an appropriate error msg from a return code in D0
…. D0 is 2
LSL.W
MOVEA.L
JMP
…
…
#2,D0
CASES(PC,D0.W),A0
(A0)
* CASEx are the names of code segments to handle the
various situations
CASES
DC.L
DC.L
DC.L
DC.L
CASE0
CASE1
CASE2
CASE3
Position Independent Code

code can be loaded and executed from
any starting address in memory after it
has been assembled; address assigned at
run time
 static
→ after assembly, e.g. shared library,
ROM based program
 dynamic
→ can be moved after it has started
execution, e.g. virtual memory system
Position Independent Code



no absolute addresses except for hardware
device addresses
PC-relative addressing is used wherever possible
for branches within modules
where PC-relative addressing cannot be used,
indirect addressing through a linkage table is
used for accesses to global variables, for intermodule procedure calls, for branches and literal
accesses

The dynamic loader fills in procedure and data linkage
tables with the absolute virtual addresses of the routines
and data in a shared library at run time (known as
binding) .
Relocatable vs NonRelocatable Code

Relocatable Code
 code
can be moved to different starting
address but requires reassembly
 code contains symbols but no explicit
addresses (except hardware)
 addresses assigned at link time

NonRelocatable Code
 code
requires reprogramming to be moved to
a different memory location
 code contains actual addresses
 addresses assigned during programming
What is the code type?
For the following examples,
TABLE
DS.B
100
is at address $100E
e.g.
MOVEA.L
#$100E,A0
e.g.
MOVEA.L
#TABLE,A0
e.g.
LEA
TABLE(PC),A0
LEA
ADDA.L
*+0,A0
#(TABLE-START),A0
e.g.
START
Reading:

M68000 Assembly Language [92p; N. Znotinas]
 review operation of address register instructions: CMPA, ADDA, SUBA,
MOVEA, LEA

Addressing Modes [© Stephen J Kuyath, UNCC, 2007]
- has good single step animations for the various addressing modes
- best way to use the animations is to (1) look at the instruction he is
executing, (2) figure out the actions, (3) determine the final result, and
(4) then single step through the animation to verify
- for PC relative, he is showing the PC after the instruction fetch

Position Independent Code from “Assembly Language and Systems
Programming for the M68000 Family” William Ford, William R. Topp
- many good examples, some later examples use the M68020 program
counter memory indirect addressing mode which has a scale
(multiplication) factor and some additional fields; in M68000, you have to
shift (multiply) the index explicitly
Expectations:



I do not expect you to be able to write code using program relative
addressing modes but you should be able to read code that contains
program relative addressing modes
you should be able to use all of the address register indirect variations
you should be able to classify the code type for a given sample of code