Load and Store Instructions
Load and Store Instructions
ARM7TDMI Microprocessor
2 of 32
Load / Store Instructions
The ARM is a Load / Store Architecture:
Does not support memory to memory data processing
operations.
Must move data values into registers before using them.
This might sound inefficient, but in practice isnt:
Load data values from memory into registers.
Process data in registers using a number of data processing
instructions which are not slowed down by memory access.
Store results from registers out to memory.
The ARM has three sets of instructions which interact with
main memory. These are:
Single register data transfer (LDR / STR).
Block data transfer (LDM/STM).
Single Data Swap (SWP).
3 of 32
Single register data transfer
The basic load and store instructions are:
Load and Store Word or Byte
LDR / STR / LDRB / STRB
ARM Architecture Version 4 also adds support for
halfwords and signed data.
Load and Store Halfword
LDRH / STRH
Load Signed Byte or Halfword - load value and sign
extend it to 32 bits.
LDRSB / LDRSH
All of these instructions can be conditionally executed by
inserting the appropriate condition code after STR / LDR.
e.g. LDREQB
Syntax:
<LDR|STR>{<cond>}{<size>} Rd, <address>
4 of 32
Load and Store Word or Byte:Base Register
The memory location to be accessed is held in a base register
STR r0, [r1] ; Store contents of r0 to location pointed to
; by contents of r1.
LDR r2, [r1] ; Load r2 with contents of memory location
; pointed to by contents of r1.
r0 Memory
Source
Register 0x5
for STR
r1 r2
Base Destination
Register 0x200 0x200 0x5 0x5 Register
for LDR
5 of 32
Instruction Format
31 28 27 26 25 24 23 22 21 20 19 16 15 12 11 0
Cond 0 1 # P U B W L Rn Rd offset
6 of 32
Offset
If # (I) = 0
12-bit immediate
If # (I) = 1
11 7 6 5 4 3 0
#shift Sh 0 Rm
7 of 32
Load and Store Word or Byte:
Offsets from the Base Register
As well as accessing the actual location contained in the base register,
these instructions can access a location offset from the base register
pointer.
This offset can be
An unsigned 12bit immediate value (i.e. 0 - 4095 bytes).
A register, optionally shifted by an immediate value
This can be either added or subtracted from the base register:
Prefix the offset value or register with + (default) or -.
This offset can be applied:
before the transfer is made: Pre-indexed addressing
optionally auto-incrementing the base register, by postfixing the
instruction with an !.
after the transfer is made: Post-indexed addressing
causing the base register to be auto-incremented.
8 of 32
Load and Store Word or Byte:Pre-indexed Addressing
Example: STR r0, [r1,#12]
r0
Memory Source
0x5 Register
for STR
Offset
12 0x20c 0x5
r1
Base
Register 0x200 0x200
9 of 32
Load and Store Word or Byte:
Post-indexed Addressing
Example: STR r0, [r1], #12 Memory
r1 Offset r0
Updated Source
Base 0x20c 12 0x20c
0x5 Register
Register for STR
0x200 0x5
r1
Original
Base 0x200
Register
10 of 32
Load and Stores with User Mode Privilege
11 of 32
Example Usage of Addressing Modes
12 of 32
Offsets for Halfword and Signed Halfword / Byte
Access
13 of 32
Instruction Format
31 28 27 25 24 23 22 21 20 19 16 15 12 11 8 7 6 5 4 3 0
Cond 000 P U # W L Rn Rd offsetH 1 S H 1 offsetL
Rn : Base Register
Rd : Source / Destination
Register
14 of 32
Offset
If # (I) = 0
11 8 3 0
imm[7:4] imm [3:0]
If # (I) = 1
11 8 3 0
0 0 0 0 Rm
15 of 32
Quiz
Write a segment of code that add together elements x to
x+(n-1) of an array, where the element x=0 is the first
element of the array.
Each element of the array is word sized (i.e.. 32 bits).
The segment should use post-indexed addressing.
At the start of your segments, you should assume that:
r0 points to the start of the array. Elements
r1 = x
r2 = n
{
x + (n - 1)
n elements
x+1
x
r0 0
16 of 32
Quiz - Sample Solution
17 of 32
Block Data Transfer (1)
The Load and Store Multiple instructions (LDM / STM) allow between 1
and 16 registers to be transferred to or from memory.
The transferred registers can be either:
Any subset of the current bank of registers (default).
Any subset of the user mode bank of registers when in a privileged mode
(postfix instruction with a ^).
31 28 27 24 23 22 21 20 19 16 15 0
18 of 32
Block Data Transfer (2)
19 of 32
Direct functionality of Block Data Transfer
20 of 32
Example: Block Copy
21 of 32
Swap and Swap Byte Instructions
Atomic operation of a memory read followed by a memory write which
moves byte or word quantities between registers and memory.
Syntax:
SWP{<cond>}{B} Rd, Rm, [Rn]
1
Rn
temp
2 3
Memory
Rm Rd
22 of 32
Example: FIR filter
C:
for (i=0, f=0; i<N; i++)
f = f + c[i]*x[i];
Assembler
; loop initiation code
MOV r0,#0 ; use r0 for I
MOV r8,#0 ; use separate index for arrays
ADR r2,N ; get address for N
LDR r1,[r2] ; get value of N
MOV r2,#0 ; use r2 for f
23 of 32
FIR filter, cont.d
24 of 32
Example: Program01
27 of 32
Coprocessor Register Transfers
These two instructions move data between ARM registers
and coprocessor registers
MRC : Move to Register from Coprocessor
MCR : Move to Coprocessor from Register
An operation may also be performed on the data as it is
transferred
For example a Floating Point Convert to Integer instruction
can be implemented as a register transfer to ARM that also
converts the data from floating point format to integer
format.
Syntax <MRC|MCR>{<cond>}
<cp_num>,<opc_1>,Rd,CRn,CRm,<opc_2>
31 28 27 26 25 24 23 22 21 20 19 16 15 12 11 8 7 5 4 3 0
28 of 32
Coprocessor Memory Transfers (1)
31 28 27 26 25 24 23 22 21 20 19 16 15 12 11 8 7 0
29 of 32
Coprocessor Memory Transfers (2)
Syntax of these is similar to word transfers between ARM and
memory:
<LDC|STC>{<cond>}{<L>} <cp_num>,CRd,<address>
PC relative offset generated if possible, else causes an error.
<LDC|STC>{<cond>}{<L>}
<cp_num>,CRd,<[Rn,offset]{!}>
Pre-indexed form, with optional writeback of the base register
<LDC|STC>{<cond>}{<L>}
<cp_num>,CRd,<[Rn],offset>
Post-indexed form
where
<L> when present causes a long transfer to be performed (N=1) else
causes a short transfer to be performed (N=0).
Effect of this is coprocessor dependant.
30 of 32
Summary
31 of 32
Thank You, Any Questions ?
32 of 32