You are on page 1of 10

Handout

The ARM Instruction Set

Main features of the


ARM Instruction Set
* All instructions are 32 bits long.
* Most instructions execute in a single cycle.

ARM
Advanced RISC Machines
*
*
Every instruction can be conditionally executed.
A load/store architecture
• Data processing instructions act only on registers
– Three operand format
– Combined ALU and shifter for high speed bit manipulation
The ARM Instruction Set • Specific memory access instructions with powerful auto-indexing
addressing modes.
– 32 bit and 8 bit data types
 and also 16 bit data types on ARM Architecture v4.

– Flexible multiple register load and store instructions


* Instruction set extension via coprocessors

The ARM Instruction Set - ARM University Program - V1.0 1 The ARM Instruction Set - ARM University Program - V1.0 2

Processor Modes The Registers


* The ARM has six operating modes:
* ARM has 37 registers in total, all of which are 32-bits long.
• User (unprivileged mode under which most tasks run)
• 1 dedicated program counter
• FIQ (entered when a high priority (fast) interrupt is raised)
• 1 dedicated current program status register
• IRQ (entered when a low priority (normal) interrupt is raised)
• 5 dedicated saved program status registers
• Supervisor (entered on reset and when a Software Interrupt instruction is
executed) • 30 general purpose registers
• Abort (used to handle memory access violations) * However these are arranged into several banks, with the accessible
bank being governed by the processor mode. Each mode can access
• Undef (used to handle undefined instructions)
• a particular set of r0-r12 registers
* ARM Architecture Version 4 adds a seventh mode:
• a particular r13 (the stack pointer) and r14 (link register)
• System (privileged mode using the same registers as user mode)
• r15 (the program counter)
• cpsr (the current program status register)
and privileged modes can also access
• a particular spsr (saved program status register)

The ARM Instruction Set - ARM University Program - V1.0 3 The ARM Instruction Set - ARM University Program - V1.0 4

Register Example:
Register Organisation
User to FIQ Mode
General registers and Program Counter Registers in use Registers in use
User32 / System FIQ32 Supervisor32 Abort32 IRQ32 Undefined32 User Mode FIQ Mode
r0 r0
r0 r0 r0 r0 r0 r0
r1 r1
r1 r1 r1 r1 r1 r1
r2 r2
r2 r2 r2 r2 r2 r2
r3 r3
r3 r3 r3 r3 r3 r3
r4 r4
r4 r4 r4 r4 r4 r4
r5 r5
r5 r5 r5 r5 r5 r5
r6 r6
r6 r6 r6 r6 r6 r6
r7 r7
r7 r7 r7 r7 r7 r7
r8 r8_fiq r8 r8 r8 r8
r8 r8_fiq EXCEPTION r8 r8_fiq
r9 r9_fiq r9 r9_fiq
r9 r9_fiq r9 r9 r9 r9
r10 r10_fiq r10 r10_fiq
r10 r10_fiq r10 r10 r10 r10
r11 r11_fiq r11 r11_fiq
r11 r11_fiq r11 r11 r11 r11
r12 r12_fiq r12 r12_fiq
r12 r12_fiq r12 r12 r12 r12
r13 (sp) r13_fiq r13 (sp) r13_fiq
r13 (sp) r13_fiq r13_svc r13_abt r13_irq r13_undef
r14 (lr) r14_fiq r14 (lr) r14_fiq
r14 (lr) r14_fiq r14_svc r14_abt r14_irq r14_undef
r15 (pc) r15 (pc)
r15 (pc) r15 (pc) r15 (pc) r15 (pc) r15 (pc) r15 (pc)
Return address calculated from User mode
cpsr
PC value and stored in FIQ mode LR cpsr
Program Status Registers spsr_fiq spsr_fiq
cpsr cpsr cpsr cpsr cpsr cpsr
sprsr_fiq
spsr_fiq spsr_svc spsr_abt sprsr_fiq
spsr_irq spsr_undef
sprsr_fiq User mode CPSR copied to FIQ mode SPSR

The ARM Instruction Set - ARM University Program - V1.0 5 The ARM Instruction Set - ARM University Program - V1.0 6

1
Handout
The ARM Instruction Set

Accessing Registers using The Program Status Registers


ARM Instructions (CPSR and SPSRs)
* No breakdown of currently accessible registers.
• All instructions can access r0-r14 directly. 31 28 8 4 0

• Most instructions also allow use of the PC. N Z CV I F T Mode


* Specific instructions to allow access to CPSR and SPSR.
* Note : When in a privileged mode, it is also possible to load / store the Copies of the ALU status flags (latched if the
(banked out) user mode registers to or from memory. instruction has the "S" bit set).
• See later for details. * Condition Code Flags * Interrupt Disable bits.
N = Negative result from ALU flag. I = 1, disables the IRQ.
Z = Zero result from ALU flag. F = 1, disables the FIQ.
C = ALU operation Carried out
V = ALU operation oVerflowed * T Bit (Architecture v4T only)
T = 0, Processor in ARM state
* Mode Bits T = 1, Processor in Thumb state
M[4:0] define the processor mode.

The ARM Instruction Set - ARM University Program - V1.0 7 The ARM Instruction Set - ARM University Program - V1.0 8

Condition Flags The Program Counter (R15)


Logical Instruction Arithmetic Instruction * When the processor is executing in ARM state:
• All instructions are 32 bits in length
Flag
• All instructions must be word aligned
Negative No meaning Bit 31 of the result has been set • Therefore the PC value is stored in bits [31:2] with bits [1:0] equal to
(N=‘1’) Indicates a negative number in zero (as instruction cannot be halfword or byte aligned).
signed operations
* R14 is used as the subroutine link register (LR) and stores the return
Zero Result is all zeroes Result of operation was zero address when Branch with Link operations are performed,
(Z=‘1’) calculated from the PC.
* Thus to return from a linked branch
Carry After Shift operation Result was greater than 32 bits
(C=‘1’) ‘1’ was left in carry flag • MOV r15,r14
or
oVerflow No meaning Result was greater than 31 bits
(V=‘1’) Indicates a possible corruption of • MOV pc,lr
the sign bit in signed
numbers

The ARM Instruction Set - ARM University Program - V1.0 9 The ARM Instruction Set - ARM University Program - V1.0 10

Exception Handling
The Instruction Pipeline
and the Vector Table
* The ARM uses a pipeline in order to increase the speed of the flow of
* When an exception occurs, the core: instructions to the processor.
0x00000000 Reset
• Copies CPSR into SPSR_<mode> • Allows several operations to be undertaken simultaneously, rather than
0x00000004 Undefined Instruction
• Sets appropriate CPSR bits serially.
0x00000008 Software Interrupt
‹ If core implements ARM Architecture 4T and is ARM
0x0000000C Prefetch Abort
currently in Thumb state, then
0x00000010 Data Abort PC FETCH Instruction fetched from memory
„ ARM state is entered.

0x00000014 Reserved
‹ Mode field bits

0x00000018 IRQ
‹ Interrupt disable flags if appropriate.
PC - 4 DECODE Decoding of registers used in instruction
• Maps in appropriate banked registers 0x0000001C FIQ

• Stores the “return address” in LR_<mode>


Register(s) read from Register Bank
• Sets PC to vector address PC - 8 EXECUTE
Shift and ALU operation
* To return, exception handler needs to: Write register(s) back to Register Bank
• Restore CPSR from SPSR_<mode> * Rather than pointing to the instruction being executed, the
• Restore PC from LR_<mode> PC points to the instruction being fetched.

The ARM Instruction Set - ARM University Program - V1.0 11 The ARM Instruction Set - ARM University Program - V1.0 12

2
Handout
The ARM Instruction Set

ARM Instruction Set Format


Conditional Execution
31 2827 1615 87 0 Instruction type
Cond 0 0 I Opcode S Rn Rd Operand2 Data processing / PSR Transfer * Most instruction sets only allow branches to be executed conditionally.
Cond 0 0 0 0 0 0 A S Rd Rn Rs 1 0 0 1 Rm Multiply
* However by reusing the condition evaluation hardware, ARM effectively
Cond 0 0 0 0 1 U A S RdHi RdLo Rs 1 0 0 1 Rm Long Multiply (v3M / v4 only) increases number of instructions.
Cond 0 0 0 1 0 B 0 0 Rn Rd 0 0 0 0 1 0 0 1 Rm Swap
• All instructions contain a condition field which determines whether the
Cond 0 1 I P U B W L Rn Rd Offset Load/Store Byte/Word CPU will execute them.
Cond 1 0 0 P U S W L Rn Register List Load/Store Multiple • Non-executed instructions soak up 1 cycle.
Cond 0 0 0 P U 1 W L Rn Rd Offset1 1 S H 1 Offset2 Halfword transfer : Immediate offset (v4 only)
– Still have to complete cycle so as to allow fetching and decoding of
Cond 0 0 0 P U 0 W L Rn Rd 0 0 0 0 1 S H 1 Rm Halfword transfer: Register offset (v4 only)
following instructions.
Cond 1 0 1 L Offset Branch
* This removes the need for many branches, which stall the pipeline (3
Cond 0 0 0 1 0 0 1 0 1 1 1 1 1 1 1 1 1 1 1 1 0 0 0 1 Rn Branch Exchange (v4T only) cycles to refill).
Cond 1 1 0 P U N W L Rn CRd CPNum Offset Coprocessor data transfer
• Allows very dense in-line code, without branches.
Cond 1 1 1 0 Op1 CRn CRd CPNum Op2 0 CRm Coprocessor data operation
• The Time penalty of not executing several conditional instructions is
Cond 1 1 1 0 Op1 L CRn Rd CPNum Op2 1 CRm Coprocessor register transfer frequently less than overhead of the branch
Cond 1 1 1 1 SWI Number Software interrupt or subroutine call that would otherwise be needed.

The ARM Instruction Set - ARM University Program - V1.0 13 The ARM Instruction Set - ARM University Program - V1.0 14

The Condition Field Using and updating the


Condition Field
28 24 20 16 4 0
31 12 8
* To execute an instruction conditionally, simply postfix it with the
Cond appropriate condition:
• For example an add instruction takes the form:
– ADD r0,r1,r2 ; r0 = r1 + r2 (ADDAL)
0000 = EQ - Z set (equal) 1001 = LS - C clear or Z (set unsigned
lower or same) • To execute this only if the zero flag is set:
0001 = NE - Z clear (not equal)
0010 = HS / CS - C set (unsigned
1010 = GE - N set and V set, or N clear – ADDEQ r0,r1,r2 ; If zero flag set then…
and V clear (>or =) ; ... r0 = r1 + r2
higher or same)
1011 = LT - N set and V clear, or N clear
0011 = LO / CC - C clear (unsigned and V set (>) * By default, data processing operations do not affect the condition flags
lower) (apart from the comparisons where this is the only effect). To cause the
1100 = GT - Z clear, and either N set and
0100 = MI -N set (negative)
V set, or N clear and V set (>) condition flags to be updated, the S bit of the instruction needs to be set
0101 = PL - N clear (positive or by postfixing the instruction (and any condition code) with an “S”.
1101 = LE - Z set, or N set and V clear,or
zero)
N clear and V set (<, or =) • For example to add two numbers and set the condition flags:
0110 = VS - V set (overflow)
1110 = AL - always – ADDS r0,r1,r2 ; r0 = r1 + r2
0111 = VC - V clear (no overflow) ; ... and set flags
1111 = NV - reserved.
1000 = HI - C set and Z clear
(unsigned higher)
The ARM Instruction Set - ARM University Program - V1.0 15 The ARM Instruction Set - ARM University Program - V1.0 16

Branch instructions (1) Branch instructions (2)


* Branch : B{<cond>} label * When executing the instruction, the processor:
* Branch with Link : BL{<cond>} sub_routine_label • shifts the offset left two bits, sign extends it to 32 bits, and adds it to PC.
31 28 27 25 24 23 0
* Execution then continues from the new PC, once the pipeline has been
refilled.
Cond 1 0 1 L Offset
* The "Branch with link" instruction implements a subroutine call by
Link bit 0 = Branch writing PC-4 into the LR of the current bank.
1 = Branch with link
Condition field • i.e. the address of the next instruction following the branch with link
(allowing for the pipeline).
* The offset for branch instructions is calculated by the assembler: * To return from subroutine, simply need to restore the PC from the LR:
• By taking the difference between the branch instruction and the • MOV pc, lr
target address minus 8 (to allow for the pipeline).
• Again, pipeline has to refill before execution continues.
• This gives a 26 bit offset which is right shifted 2 bits (as the
bottom two bits are always zero as instructions are word – * The "Branch" instruction does not affect LR.
aligned) and stored into the instruction encoding. * Note: Architecture 4T offers a further ARM branch instruction, BX
• This gives a range of ± 32 Mbytes. • See Thumb Instruction Set Module for details.

The ARM Instruction Set - ARM University Program - V1.0 17 The ARM Instruction Set - ARM University Program - V1.0 18

3
Handout
The ARM Instruction Set

Data processing Instructions Arithmetic Operations


* Largest family of ARM instructions, all sharing the same instruction * Operations are:
format. • ADD operand1 + operand2
* Contains: • ADC operand1 + operand2 + carry
• Arithmetic operations • SUB operand1 - operand2
• Comparisons (no results - just set condition codes) • SBC operand1 - operand2 + carry -1
• Logical operations • RSB operand2 - operand1
• Data movement between registers • RSC operand2 - operand1 + carry - 1
* Remember, this is a load / store architecture * Syntax:
• These instruction only work on registers, NOT memory. • <Operation>{<cond>}{S} Rd, Rn, Operand2
* They each perform a specific operation on one or two operands. * Examples
• First operand always a register - Rn • ADD r0, r1, r2
• Second operand sent to the ALU via barrel shifter. • SUBGT r3, r3, #1
* We will examine the barrel shifter shortly. • RSBLES r4, r5, #5

The ARM Instruction Set - ARM University Program - V1.0 19 The ARM Instruction Set - ARM University Program - V1.0 20

Comparisons Logical Operations


* The only effect of the comparisons is to * Operations are:
• UPDATE THE CONDITION FLAGS. FLAGS Thus no need to set S bit. • AND operand1 AND operand2
* Operations are: • EOR operand1 EOR operand2
• CMP operand1 - operand2, but result not written • ORR operand1 OR operand2
• CMN operand1 + operand2, but result not written • BIC operand1 AND NOT operand2 [ie bit clear]
• TST operand1 AND operand2, but result not written * Syntax:
• TEQ operand1 EOR operand2, but result not written • <Operation>{<cond>}{S} Rd, Rn, Operand2
* Syntax: * Examples:
• <Operation>{<cond>} Rn, Operand2 • AND r0, r1, r2
* Examples: • BICEQ r2, r3, #7
• CMP r0, r1 • EORS r1,r3,r0
• TSTEQ r2, #5

The ARM Instruction Set - ARM University Program - V1.0 21 The ARM Instruction Set - ARM University Program - V1.0 22

Data Movement The Barrel Shifter


* Operations are: * The ARM doesn’t have actual shift instructions.
• MOV operand2
• MVN NOT operand2 * Instead it has a barrel shifter which provides a mechanism to carry out
Note that these make no use of operand1. shifts as part of other instructions.
* Syntax:
• <Operation>{<cond>}{S} Rd, Operand2 * So what operations does the barrel shifter support?
* Examples:
• MOV r0, r1
• MOVS r2, #10
• MVNEQ r1,#0

The ARM Instruction Set - ARM University Program - V1.0 23 The ARM Instruction Set - ARM University Program - V1.0 24

4
Handout
The ARM Instruction Set

Barrel Shifter - Left Shift Barrel Shifter - Right Shifts


* Shifts left by the specified amount (multiplies by powers of two) e.g. Logical Shift Right
Logical Shift Right
LSL #5 = multiply by 32 •Shifts right by the
specified amount
(divides by powers of ...0 Destination CF
two) e.g.
LSR #5 = divide by 32

Logical Shift Left (LSL)


Arithmetic Shift Right Arithmetic Shift Right
•Shifts right (divides by
powers of two) and
CF Destination 0 preserves the sign bit, Destination CF
for 2's complement
operations. e.g. Sign bit shifted in
ASR #5 = divide by 32

The ARM Instruction Set - ARM University Program - V1.0 25 The ARM Instruction Set - ARM University Program - V1.0 26

Using the Barrel Shifter:


Barrel Shifter - Rotations
The Second Operand
Rotate Right (ROR) Rotate Right Operand Operand * Register, optionally with shift
• Similar to an ASR but the 1 2 operation applied.
bits wrap around as they * Shift value can be either be:
leave the LSB and appear as Destination CF
the MSB. • 5 bit unsigned integer
• Specified in bottom byte of
e.g. ROR #5 Barrel another register.
• Note the last bit rotated is Shifter
also used as the Carry Out. * Immediate value
• 8 bit number
Rotate Right Extended (RRX) • Can be rotated right through
Rotate Right through Carry an even number of
• This operation uses the ALU positions.
CPSR C flag as a 33rd bit. • Assembler will calculate
• Rotates right by 1 bit. Destination CF rotate for you from
Encoded as ROR #0. constant.
Result
The ARM Instruction Set - ARM University Program - V1.0 27 The ARM Instruction Set - ARM University Program - V1.0 28

Second Operand : Second Operand :


Shifted Register Using a Shifted Register
* The amount by which the register is to be shifted is contained in * Using a multiplication instruction to multiply by a constant means first
either: loading the constant into a register and then waiting a number of
internal cycles for the instruction to complete.
• the immediate 5-bit field in the instruction
* A more optimum solution can often be found by using some combination
– NO OVERHEAD of MOVs, ADDs, SUBs and RSBs with shifts.
– Shift is done for free - executes in single cycle. • Multiplications by a constant equal to a ((power of 2) ± 1) can be done in
• the bottom byte of a register (not PC) one cycle.
– Then takes extra cycle to execute * Example: r0 = r1 * 5
– ARM doesn’t have enough read ports to read 3 registers at Example: r0 = r1 + (r1 * 4)
once. Õ ADD r0, r1, r1, LSL #2
– Then same as on other processors where shift is * Example: r2 = r3 * 105
separate instruction. Example: r2 = r3 * 15 * 7
* If no shift is specified then a default shift is applied: LSL #0 Example: r2 = r3 * (16 - 1) * (8 - 1)
Õ RSB r2, r3, r3, LSL #4 ; r2 = r3 * 15
• i.e. barrel shifter has no effect on value in register. Õ RSB r2, r2, r2, LSL #3 ; r2 = r2 * 7

The ARM Instruction Set - ARM University Program - V1.0 29 The ARM Instruction Set - ARM University Program - V1.0 30

5
Handout
The ARM Instruction Set

Second Operand : Second Operand :


Immediate Value (1) Immediate Value (2)
* There is no single instruction which will load a 32 bit immediate constant * This gives us:
into a register without performing a data load from memory. • 0 - 255 [0 - 0xff]
• All ARM instructions are 32 bits long • 256,260,264,..,1020 [0x100-0x3fc, step 4, 0x40-0xff ror 30]
• ARM instructions do not use the instruction stream as data. • 1024,1040,1056,..,4080 [0x400-0xff0, step 16, 0x40-0xff ror 28]
* The data processing instruction format has 12 bits available for • 4096,4160, 4224,..,16320 [0x1000-0x3fc0, step 64, 0x40-0xff ror 26]
operand2 * These can be loaded using, for example:
• If used directly this would only give a range of 4096. • MOV r0, #0x40, 26 ; => MOV r0, #0x1000 (ie 4096)
* Instead it is used to store 8 bit constants, giving a range of 0 - 255. * To make this easier, the assembler will convert to this form for us if
* These 8 bits can then be rotated right through an even number of simply given the required constant:
positions (ie RORs by 0, 2, 4,..30). • MOV r0, #4096 ; => MOV r0, #0x1000 (ie 0x40 ror 26)
• This gives a much larger range of constants that can be directly loaded, * The bitwise complements can also be formed using MVN:
though some constants will still need to be loaded
from memory. • MOV r0, #0xFFFFFFFF ; assembles to MVN r0, #0
* If the required constant cannot be generated, an error will
be reported.
The ARM Instruction Set - ARM University Program - V1.0 31 The ARM Instruction Set - ARM University Program - V1.0 32

Loading full 32 bit constants Multiplication Instructions


* Although the MOV/MVN mechansim will load a large range of constants * The Basic ARM provides two multiplication instructions.
into a register, sometimes this mechansim will not generate the required * Multiply
constant.
• MUL{<cond>}{S} Rd, Rm, Rs ; Rd = Rm * Rs
* Therefore, the assembler also provides a method which will load ANY 32
bit constant: * Multiply Accumulate - does addition for free
• LDR rd,=numeric constant • MLA{<cond>}{S} Rd, Rm, Rs,Rn ; Rd = (Rm * Rs) + Rn
* If the constant can be constructed using either a MOV or MVN then this * Restrictions on use:
will be the instruction actually generated. • Rd and Rm cannot be the same register
* Otherwise, the assembler will produce an LDR instruction with a PC- – Can be avoid by swapping Rm and Rs around. This works because
relative address to read the constant from a literal pool. multiplication is commutative.
• LDR r0,=0x42 ; generates MOV r0,#0x42 • Cannot use PC.
• LDR r0,=0x55555555 ; generate LDR r0,[pc, offset to lit pool]
These will be picked up by the assembler if overlooked.
* As this mechanism will always generate the best instruction for a given
case, it is the recommended way of loading constants. * Operands can be considered signed or unsigned
• Up to user to interpret correctly.

The ARM Instruction Set - ARM University Program - V1.0 33 The ARM Instruction Set - ARM University Program - V1.0 34

Multiplication Implementation Extended Multiply Instructions


* The ARM makes use of Booth’s Algorithm to perform integer * M variants of ARM cores contain extended multiplication
multiplication. hardware. This provides three enhancements:
* On non-M ARMs this operates on 2 bits of Rs at a time. • An 8 bit Booth’
Booth’s Algorithm is used
• For each pair of bits this takes 1 cycle (plus 1 cycle to start with). – Multiplication is carried out faster (maximum for standard
• However when there are no more 1’s left in Rs, the multiplication will instructions is now 5 cycles).
early-terminate. • Early termination method improved so that now completes
* Example: Multiply 18 and -1 : Rd = Rm * Rs multiplication when all remaining bit sets contain
– all zeroes (as with non-M ARMs), or
Rm 18 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 1 0 18 Rs – all ones.
Rs -1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 -1 Rm Thus the previous example would early terminate in 2 cycles in
both cases.
17 cycles 4 cycles • 64 bit results can now be produced from two 32bit operands
– Higher accuracy.
* Note: Compiler does not use early termination criteria to
decide on which order to place operands. – Pair of registers used to store result.

The ARM Instruction Set - ARM University Program - V1.0 35 The ARM Instruction Set - ARM University Program - V1.0 36

6
Handout
The ARM Instruction Set

Multiply-Long and
Load / Store Instructions
Multiply-Accumulate Long
* Instructions are * The ARM is a Load / Store Architecture:
• MULL which gives RdHi,RdLo:=Rm*Rs • Does not support memory to memory data processing operations.
• MLAL which gives RdHi,RdLo:=(Rm*Rs)+RdHi,RdLo • Must move data values into registers before using them.
* However the full 64 bit of the result now matter (lower precision * This might sound inefficient, but in practice isn’t:
multiply instructions simply throws top 32bits away) • Load data values from memory into registers.
• Need to specify whether operands are signed or unsigned • Process data in registers using a number of data processing
* Therefore syntax of new instructions are: instructions which are not slowed down by memory access.
• UMULL{<cond>}{S} RdLo,RdHi,Rm,Rs • Store results from registers out to memory.
• UMLAL{<cond>}{S} RdLo,RdHi,Rm,Rs * The ARM has three sets of instructions which interact with main
• SMULL{<cond>}{S} RdLo, RdHi, Rm, Rs memory. These are:
• SMLAL{<cond>}{S} RdLo, RdHi, Rm, Rs • Single register data transfer (LDR / STR).
* Not generated by the compiler. • Block data transfer (LDM/STM).
• Single Data Swap (SWP).
Warning : Unpredictable on non-M ARMs.
The ARM Instruction Set - ARM University Program - V1.0 37 The ARM Instruction Set - ARM University Program - V1.0 38

Load and Store Word or Byte:


Single register data transfer
Base Register
* The basic load and store instructions are: * The memory location to be accessed is held in a base register
• Load and Store Word or Byte • STR r0, [r1] ; Store contents of r0 to location pointed to
– LDR / STR / LDRB / STRB ; by contents of r1.
* ARM Architecture Version 4 also adds support for halfwords and signed • LDR r2, [r1] ; Load r2 with contents of memory location
data. ; pointed to by contents of r1.
• Load and Store Halfword r0 Memory
– LDRH / STRH Source
Register 0x5
• Load Signed Byte or Halfword - load value and sign extend it to 32 bits. for STR
– LDRSB / LDRSH
* All of these instructions can be conditionally executed by inserting the
r1 r2
appropriate condition code after STR / LDR. Base Destination
Register 0x200 0x200 0x5 0x5 Register
• e.g. LDREQB
for LDR
* Syntax:
• <LDR|STR>{<cond>}{<size>} Rd, <address>
The ARM Instruction Set - ARM University Program - V1.0 39 The ARM Instruction Set - ARM University Program - V1.0 40

Load and Store Word or Byte: Load and Store Word or Byte:
Offsets from the Base Register Pre-indexed Addressing
r0
* As well as accessing the actual location contained in the base register, * Example: STR r0, [r1,#12] Memory Source
these instructions can access a location offset from the base register 0x5 Register
for STR
pointer.
Offset
* This offset can be
12 0x20c 0x5
• An unsigned 12bit immediate value (ie 0 - 4095 bytes).
r1
• A register, optionally shifted by an immediate value Base
Register 0x200 0x200
* 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-
Pre-indexed addressing
* To store to location 0x1f4 instead use: STR r0, [r1,#-12]
– optionally auto-
auto-incrementing the base register, by postfixing the
instruction with an ‘!’. * To auto-increment base pointer to 0x20c use: STR r0, [r1, #12]!
• after the transfer is made: Post-
Post-indexed addressing * If r2 contains 3, access 0x20c by multiplying this by 4:
– causing the base register to be auto-
auto-incremented.
incremented • STR r0, [r1, r2, LSL #2]

The ARM Instruction Set - ARM University Program - V1.0 41 The ARM Instruction Set - ARM University Program - V1.0 42

7
Handout
The ARM Instruction Set

Load and Store Word or Byte: Load and Stores


Post-indexed Addressing with User Mode Privilege
Memory
* Example: STR r0, [r1], #12
* When using post-indexed addressing, there is a further form of
r1 Offset r0
Updated Source Load/Store Word/Byte:
Base 0x20c 12 0x5 Register
Register
0x20c
for STR
• <LDR|STR>{<cond>}{B}T Rd, <post_indexed_address>

0x5
* When used in a privileged mode, this does the load/store with user mode
0x200
r1 privilege.
Original
Base 0x200 • Normally used by an exception handler that is emulating a memory
Register access instruction that would normally execute in user mode.
* To auto-increment the base register to location 0x1f4 instead use:
• STR r0, [r1], #-12
* If r2 contains 3, auto-incremenet base register to 0x20c by multiplying
this by 4:
• STR r0, [r1], r2, LSL #2

The ARM Instruction Set - ARM University Program - V1.0 43 The ARM Instruction Set - ARM University Program - V1.0 44

Example Usage of Offsets for Halfword and


Addressing Modes Signed Halfword / Byte Access
* Imagine an array, the first element of which is pointed to by the contents * The Load and Store Halfword and Load Signed Byte or Halfword
of r0. instructions can make use of pre- and post-indexed addressing in much
Memory
* If we want to access a particular element, element Offset
the same way as the basic load and store instructions.
then we can use pre-indexed addressing: * However the actual offset formats are more constrained:
• r1 is element we want. • The immediate value is limited to 8 bits (rather than 12 bits) giving an
• LDR r2, [r0, r1, LSL #2] 3 12 offset of 0-255 bytes.
Pointer to 2 8 • The register form cannot have a shift applied to it.
start of array
* If we want to step through every 1 4
element of the array, for instance r0 0 0
to produce sum of elements in the
array, then we can use post-indexed addressing within a loop:
• r1 is address of current element (initially equal to r0).
• LDR r2, [r1], #4
Use a further register to store the address of final element,
so that the loop can be correctly terminated.

The ARM Instruction Set - ARM University Program - V1.0 45 The ARM Instruction Set - ARM University Program - V1.0 46

Effect of endianess Endianess Example


* The ARM can be set up to access its data in either little or big r0 = 0x11223344
31 24 23 16 15 87 0
endian format.
11 22 33 44
* Little endian:
• Least significant byte of a word is stored in bits 0-
0-7 of an addressed
word.
STR r0, [r1]
* Big endian:
• Least significant byte of a word is stored in bits 24-
24-31 of an
addressed word. 31 24 23 16 15 87 0 31 24 23 16 15 87 0
* This has no real relevance unless data is stored as words and then r1 = 0x100 11 22 33 44 Memory 44 33 22 11 r1 = 0x100
accessed in smaller sized quantities (halfwords or bytes).
• Which byte / halfword is accessed will depend on the endianess of Little-endian LDRB r2, [r1] Big-endian
the system involved. 31 24 23 16 15 87 0 31 24 23 16 15 87 0

00 00 00 44 00 00 00 11

r2 = 0x44 r2 = 0x11

The ARM Instruction Set - ARM University Program - V1.0 47 The ARM Instruction Set - ARM University Program - V1.0 48

8
Handout
The ARM Instruction Set

Block Data Transfer (1) Block Data Transfer (2)


* The Load and Store Multiple instructions (LDM / STM) allow betweeen * Base register used to determine where memory access should occur.
1 and 16 registers to be transferred to or from memory. • 4 different addressing modes allow increment and decrement inclusive or
* The transferred registers can be either: exclusive of the base register location.
• Any subset of the current bank of registers (default). • Base register can be optionally updated following the transfer (by
• Any subset of the user mode bank of registers when in a priviledged appending it with an ‘!!’.
mode (postfix instruction with a ‘^
^’). • Lowest register number is always transferred to/from lowest memory
31 28 27 24 23 22 21 20 19 16 15 0
location accessed.
Cond 1 0 0 P U S W L Rn Register list * These instructions are very efficient for
• Saving and restoring context
Condition field Base register Each bit corresponds to a particular – For this useful to view memory as a stack.
register. For example:
Up/Down bit Load/Store bit
0 = Down; subtract offset from base 0 = Store to memory
• Bit 0 set causes r0 to be transferred.
• Bit 0 unset causes r0 not to be transferred.
• Moving large blocks of data around memory
1 = Up ; add offset to base 1 = Load from memory
At least one register must be
Pre/Post indexing bit Write- back bit transferred as the list cannot be empty. – For this useful to directly represent functionality of the instructions.
0 = Post; add offset after transfer, 0 = no write-back
1 = Pre ; add offset before transfer 1 = write address into base
PSR and force user bit
0 = don’t load PSR or force user mode
1 = load PSR or force user mode

The ARM Instruction Set - ARM University Program - V1.0 49 The ARM Instruction Set - ARM University Program - V1.0 50

Stacks Stack Operation


* A stack is an area of memory which grows as new data is “pushed” onto * Traditionally, a stack grows down in memory, with the last “pushed”
the “top” of it, and shrinks as data is “popped” off the top. value at the lowest address. The ARM also supports ascending stacks,
* Two pointers define the current limits of the stack. where the stack structure grows up through memory.
• A base pointer * The value of the stack pointer can either:
– used to point to the “bottom” of the stack (the first location). • Point to the last occupied address (Full stack)
• A stack pointer – and so needs pre-decrementing (ie before the push)
– used to point the current “top” of the stack. • Point to the next occupied address (Empty stack)
– and so needs post-decrementing (ie after the push)
PUSH
{1,2,3} POP * The stack type to be used is given by the postfix to the instruction:
SP 3 Result of
• STMFD / LDMFD : Full Descending stack
2 SP 2 pop = 3 • STMFA / LDMFA : Full Ascending stack.
SP
1 1 • STMED / LDMED : Empty Descending stack
BASE BASE
BASE • STMEA / LDMEA : Empty Ascending stack
* Note: ARM Compiler will always use a Full descending stack.
The ARM Instruction Set - ARM University Program - V1.0 51 The ARM Instruction Set - ARM University Program - V1.0 52

Stack Examples Stacks and Subroutines


STMFD sp!, STMED sp!, STMFA sp!, STMEA sp!,
{r0,r1,r3-r5} {r0,r1,r3-r5} {r0,r1,r3-r5} {r0,r1,r3-r5}
* One use of stacks is to create temporary register workspace for
0x418 subroutines. Any registers that are needed can be pushed onto the stack
SP r5 SP at the start of the subroutine and popped off again at the end so as to
r4 r5 restore them before return to the caller :
r3 r4 STMFD sp!,{r0-r12, lr} ; stack all registers
r1 r3 ........ ; and the return address
r0 r1 ........
Old SP Old SP r5 Old SP Old SP r0 0x400 LDMFD sp!,{r0-r12, pc} ; load all the registers
r5 r4 ; and return automatically
r4 r3
* See the chapter on the ARM Procedure Call Standard in the SDT
r3 r1
Reference Manual for further details of register usage within
r1 r0 subroutines.
SP r0 SP
0x3e8 * If the pop instruction also had the ‘S’ bit set (using ‘^’) then the transfer
of the PC when in a priviledged mode would also cause the SPSR to be
copied into the CPSR (see exception handling module).

The ARM Instruction Set - ARM University Program - V1.0 53 The ARM Instruction Set - ARM University Program - V1.0 54

9
Handout
The ARM Instruction Set

Direct functionality of
Example: Block Copy
Block Data Transfer
* When LDM / STM are not being used to implement stacks, it is clearer to • Copy a block of memory, which is an exact multiple of 12 words long
specify exactly what functionality of the instruction is: from the location pointed to by r12 to the location pointed to by r13. r14
• i.e. specify whether to increment / decrement the base pointer, before or points to the end of block to be copied.
after the memory access.
* In order to do this, LDM / STM support a further syntax in addition to ; r12 points to the start of the source data
the stack one: ; r14 points to the end of the source data
• STMIA / LDMIA : Increment After ; r13 points to the start of the destination data
r13
loop LDMIA r12!, {r0-r11} ; load 48 bytes
• STMIB / LDMIB : Increment Before
STMIA r13!, {r0-r11} ; and store them r14 Increasing
• STMDA / LDMDA : Decrement After Memory
CMP r12, r14 ; check for the end
• STMDB / LDMDB : Decrement Before BNE loop ; and loop until done

r12
• This loop transfers 48 bytes in 31 cycles
• Over 50 Mbytes/sec at 33 MHz

The ARM Instruction Set - ARM University Program - V1.0 55 The ARM Instruction Set - ARM University Program - V1.0 56

Swap and Swap Byte


Software Interrupt (SWI)
Instructions
31 28 27 24 23 0

* Atomic operation of a memory read followed by a memory write Cond 1 1 1 1 Comment field (ignored by Processor)
which moves byte or word quantities between registers and
memory.
* Syntax: Condition Field
• SWP{<cond>}{B} Rd, Rm, [Rn]
* In effect, a SWI is a user-defined instruction.
1
* It causes an exception trap to the SWI hardware vector (thus causing a
Rn
temp
change to supervisor mode, plus the associated state saving), thus causing
the SWI exception handler to be called.
2 3 * The handler can then examine the comment field of the instruction to
Memory decide what operation has been requested.
Rm Rd
* By making use of the SWI mechansim, an operating system can
* Thus to implement an actual swap of contents make Rd = Rm. implement a set of privileged operations which applications running in
user mode can request.
* The compiler cannot produce this instruction.
* See Exception Handling Module for further details.

The ARM Instruction Set - ARM University Program - V1.0 57 The ARM Instruction Set - ARM University Program - V1.0 58

10

You might also like