Professional Documents
Culture Documents
BENC2423
ADDRESSING MODES
Exercises
Operand addressing
• There are many ways in ARM to specify the address; these are called addressing
modes.
• Two basic classification
1. Base register Addressing (1 type)
– Known as register indirect addressing mode
– Register holds the 32 bit memoryaddress
– Also called the baseaddress
– E.g: LDRr0, [r1]
i. Immediate offset
Syntax (32 bit): op{type}{cond} Rt, [Rn {, #offset}] ; immediate offset (value range: -255 to +4095)
Syntax (64 bit): opD{type}{cond} Rt, [Rn {, #offset}] ; immediate offset, two words (value range: -1020 to +1020)
[Rn, #±imm]
• Address accessed is imm more/less than the address found in Rn.
• Rn does not change.
• Example:
LDR r2, [r1, #12] ; r2:= mem32[r1+12]
1210 =C16
Before load
Address Memory
r1 0×00006000
Base address 0×00006000 0×000000BE
r2 0×12345678 0×00006004 0×000000BA
0×00006008 0×000000ED
After load
Effective address 0×0000600C 0×000000FE
r1 0×00006000
Before load
Address Memory
r0 0×00006000
Base address 0×00006000 0×000000BE
r1 0×0000000C 0×00006004 0×000000BA
0×00006008 0×000000ED
r2 0×12345678
Effective address 0×0000600C 0×000000FE
After load
r0 0×00006000
r0 and r1 do not change
r1 0×0000000C values
• Example
STRr2, [r0, r1] ; Store word from r2 to location pointed to by contents of [r0, r1]
Shifted/rotated secondoperand
• LSL #n Logical shift left n (0 n 31)
• LSR #n Logical shift right n (1 n 32)
• ASR #n Arithmetic shift right n (1 n 32)
• ROR #n Rotate right n (1 n 31)
• RRX Rotate right one bit, with extend
• Bit [0] is written toshifter_carry_out,
• Bits [31:1] shifted right onebit
• Carry flag is shifted into bit [31]
Offset: Scaled-Register (cont.)
• Example:
LDRr0, [r1, r2, LSL#2]
Before load
r0 0×12345678 Address Memory
Base address 0×00006000 0×000000BE r2×22 = C16×4 = 3016
r1 0×00006000 .
.
. r1 + 3016 = 0×00006030
r2 0×0000000C
Effective address 0×00006030 0×000000FE
After load
r0 0×000000FE Copy the value hold by effective
adress
r1 0×00006000
The value of r1 does not change
r2 0×0000000C
Offset: Scaled-Register (cont.)
• Example:
STRr0, [r1, r2, LSL#2] ; Store word from r0 to location pointed to by contents of [r1, r2, LSL#2]
r2 0×0000000C
Pre-indexed: Immediate
i. Immediate pre-indexed
Syntax (32 bit): op{type}{cond} Rt, [Rn , #offset]! ; pre-indexed (value range: -255 to
+255)
Syntax (64 bit): opD{type}{cond} Rt, [Rn , #offset]! ; pre-indexed, two words (value range:
-1020 to +1020)
[Rn, #±imm]!
• Address accessed is as with immediate offset mode, but Rn's value updates to become
the address accessed.
The exclamation “!” mark indicates that the base register needs to be
updated with the effective address after executing theinstruction.
Pre-indexed: Immediate (cont.)
• Example
after load
r0 0×02020202
• Example:
STRr0, [r5, # -4]! ; Store word from r0 to location pointed to by contents of [r5, #-4]!
[Rn, ±Rm]!
• Address accessed is as with register offset mode, but Rn's
value updates to become the addressaccessed.
The optional “!” specifies writing the effective address back into Rn at the end of the
instruction. Without it, Rn contains its original value after the instruction executes.
• This type of incrementing is useful in stepping through tables or lists, since the
base address is automatically updated for you.
Pre-indexed: Register (cont.)
• Example:
LDRr2,[r0, r1]!
Before load
Address Memory
r0 0×00006000
Base address 0×00006000 0×000000BE
r1 0×0000000C 0×00006004 0×000000BA
0×00006008 0×000000ED
r2 0×12345678
Effective address 0×0000600C 0×000000FE
After load
r0 0×0000600C The base register is updated
with the register value
r1 0×0000000C
• Example:
STRr2,[r0, r1]! ; Store word from r0 to location pointed to by contents of [r0, r1]!
r1 0×0000000C
r1 and r2 do not
r2 0×12345678 change values
Pre-indexed: Scaled-register
• Example
LDRr0, [r1, r2, LSL#2]!
Before load
r0 0×12345678 Address Memory
Base address 0×00006000 0×000000BE r2×22 = C16×4 = 3016
r1 0×00006000 .
.
. r1 + 3016 = 0×00006030
r2 0×0000000C
Effective address 0×00006030 0×000000FE
After load
r0 0×000000FE Copy the value hold by effective
adress
r1 0×00006030 The value of r1 is updated with the
effective address
r2 0×0000000C
Pre-indexed: Scaled-register (cont.)
• Example
STRr0, [r1, r2, LSL#2]!; Store word from r0 to location pointed to by contents of [r1, r2, LSL#2]!
r2 0×0000000C
Post-indexed: Immediate
i. Immediate post-indexed
Syntax (32 bit): op{type}{cond} Rt, [Rn] , #offset ; post-indexed (value range: -255 to
+255)
Syntax (64 bit): opD{type}{cond} Rt, [Rn] , #offset ; post-indexed, two words (value
range: -1020 to +1020)
[Rn], #±imm
• The address accessed is the value found in Rn, and then Rn's value is
increased/decreased by imm.
Example:
LDRr2, [r1],#4 ; loads r2 with the contents of the address pointed to by r1 and then increments r1 by 4
Before load
Address Memory
r2 0×FEEDBABE
Base address 0×8000 0×AB
r1 0×00008004
• Example:
STR r3, [r8], #4; store word from location r3 to the address pointed by r8 and then increment r8 by 4
What is the size of each memory
location? 1 byte or 1 word?
Example:
[Rn], ±Rm
• Address accessed is value found in Rn, and then Rn's value is
increased/decreased by Rm.
Post-indexed: Register (cont.)
• Example
LDRr0, [r1], r2
Before load
r0 0×12345678 Address Memory
Base address 0×00006000 0×000000BE
r1 0×00006000
0×00006004 0×000000BA
r2 0×0000000C
Post-indexed: Register (cont.)
• Example
STRr0, [r1], r2; store word from location r0 to the address pointed by r1 and then increment r1 by r2
r2 0×0000000C
Post-indexed: Scaled-register
• Example
LDRr0, [r1], r2, LSL#2
Before load
r0 0×12345678 Address Memory
Base address 0×00006000 0×000000BE r2×22 = C16×4 = 3016
r1 0×00006000 .
.
. r1 + 3016 = 0×00006030
r2 0×0000000C
Effective address 0×00006030 0×000000FE
After load
r0 0×000000BE Copy the value hold by base
adress r1
r1 0×00006030 The value of r1 is updated with the
effective address
r2 0×0000000C
Post-indexed: Scaled-register (cont.)
• Example
STRr0, [r1], r2, LSL#2
Before store Before store After store
r2 0×0000000C
Comparison of addressing modes
37
Exercise 5
(all in hex)
After
r0 r1 r2 0002 0000- 0002 0004- 0004 0000- 0004 0004-
line
0002 0003 0002 0007 0004 0003 0004 0007
1 0000 0000 0002 0000 0000 0000 1357 2468 A123 B246 0 0
2
3
4
5
6
38
Exercise 6
State ONE (1) assembly instruction to perform action as shown in the followingdiagram.
Before load
r0 0×12345678 Address Memory
0×00006000 0×000000BE
r1 0×00006000 .
.
r2 0×0000000C .
0×00006060 0×0000008F
After load
r0 0×0000008F
r1 0×00006000
r2 0×0000000C
Answer:
Answer Exercise 1
43
Answer Exercise 5
44
Answer Exercise 6