Instruction Set of ARM
Instruction Set of ARM
Instruction Set of ARM
By:
Dr. Karmjit Singh Sandha
Assistant Professor, ECED
Reference Books:
Arithmetic
Branch
Load and Store
Logical
Move
Arithmetic Instructions
ADD Add
SUB Subtract
MUL Multiply
CMP Compare
B Branch
BL Branch with link
Load and Store Instructions
LDR Load word
LDRB Load byte
LDRSB Load signed byte
LDRH Load half word
LDRSH Load signed half word
LDM Load multiple
LDM sp! Pop
STR Store word
STRB Store byte
STRH Store half word
STM Store multiple
STM sp! Push
Logical Instructions:
AND AND
EOR Exclusive OR
ORR OR
BIC Bit clear
TST Test
TEQ Test equivalence
Move Instructions
MOV Move
MVN Move and negate
SWP Swap
SWPB Swap byte
MRS Move program status register to register
MSR Move register to program status register
Arithmetic Instruction
Add
Syntax: ADD{cond}{S} Rd, Rn, Operand2
Usage: Adds the value in Rn to Operand2 and places the sum in Rd.
Condition flags: If S is specified then all flags are updated according to the result.
Examples:
ADD r7, r4, #99 ;adds 99 to the value in r4 and places the sum in r7
ADD r1, r2, r3 ;adds the value in r3 to the value in r2 and places the sum in r1
Add with carry
Syntax: ADC{cond}{S} Rd, Rn, Operand2
Elements inside curly brackets are optional.
Usage: Adds the value in Rn to Operand2 and adds another 1 if the carry flag is
set. The sum is placed in Rd.
Condition flags: If S is specified then all flags are updated according to the result.
Examples:
ADC r7, r4, #99 ;adds 99 to the value in r4 and adds another 1 if the carry flag is
set. Places the sum in r7
ADC r1, r2, r3 ;adds the value in r3 to the value in r2 and adds 1 if the carry flag
is set. Places the sum in r1
ADCCSS r1, r2, r3
Subtract
Syntax: SUB{cond}{S} Rd, Rn, Operand2
Usage: Subtracts Operand2 from the value in Rn and places the difference in
Rd.
Condition flags: If S is specified then all flags are updated according to the
result.
Examples:
SUB r7, r4, #99 ;subtracts 99 from the value in r4 and places the result in r7
SUB r1, r2, r3 ;subtracts the value in r3 from the value in r2 and places the
difference in r1
Subtract with carry
Usage: Subtracts Operand2 from the value in Rn and subtracts another 1 if the
carry flag is clear. Places the difference in Rd.
Condition flags: If S is specified then all flags are updated according to the result.
Examples:
Usage: Subtracts the value in Rn from Operand2 and places the difference in
Rd.
Condition flags: If S is specified then all flags are updated according to the
result.
Examples:
RSB r7, r4, #99 ;subtracts the value in r4 from 99 and places the result in r7
RSB r1, r2, r3 ;subtracts the value in r2 from the value in r3 and places the
difference in r1
Reverse subtract with carry
Syntax: RSC{cond}{S} Rd, Rn, Operand2
Usage: Subtracts the value in Rn from Operand2 and subtracts another 1 if the
carry flag is clear. Places the difference in Rd.
Condition flags: If S is specified then all flags are updated according to the
result.
Examples:
Usage: Adds the value in Rn to the product of the values in Rm and Rs and places the
Condition flags: If S is specified then the N and Z flags are updated according to the
result, the V flag is not affected and the C flag is unpredictable for the ARM7 and earlier
processors.
Example:
MLA r5, r3, r9, r6 ;multiply the values in r3 and r9, add the product to the value in r6
Example:
UMULL r6, r5, r3, r9 ;multiply the values in r3 and r9 and places the result
in r5 and r6
Multiply and accumulate - unsigned long
Syntax: UMLAL{cond}{S} RdLo, RdHi, Rm, Rs
Elements inside curly brackets are optional.
Usage: Multiples the values (as unsigned integers) in registers Rm and Rs and
adds the 64 bit product to the unsigned 64 bit value in registers RdLo (least
significant 32 bits) and RdHi (most significant 32 bits).
Condition flags: If S is specified then the N and Z flags are updated according
to the result and the V and C flags are unpredictable for the ARM7 and earlier
processors.
Example:
UMLAL r6, r5, r3, r9 ;multiply the values in r3 and r9 and add the product to
the values in r5 and r6
Multiply - signed long
Syntax: SMULL{cond}{S} RdLo, RdHi, Rm, Rs
Usage: Multiples the values (as two's complement signed integers) in registers
Rm and Rs and places the least significant 32 bits of the product in register
RdLo and the most significant 32 bits of the product in register RdHi.
Condition flags: If S is specified then the N and Z flags are updated according to
the result and the V and C flags are unpredictable for the ARM7 and earlier
processors.
Example:
SMULL r6, r5, r3, r9 ;multiply the values in r3 and r9 and places the result in r5
and r6
Multiply and accumulate - signed long
Usage: Multiples the values (as two's complement signed integers) in registers Rm
and Rs and adds the 64 bit product to the two's complement signed 64 bit value in
registers RdLo (least significant 32 bits) and RdHi (most significant 32 bits).
Condition flags: If S is specified then the N and Z flags are updated according to the
result and the V and C flags are unpredictable for the ARM7 and earlier processors.
Example:
SMLAL r6, r5, r3, r9 ;multiply the values in r3 and r9 and add the product to the
values in r5 and r6
Compare
Syntax: CMP{cond} Rn, Operand2
Usage: Subtracts Operand2 from the value in Rn and updates the flags
Examples:
CMP r1, #9 ;set the flags as if 9 was subtracted from the value in r1.
CMP r6, r2 ;set the flags for the result of (r6 - r2) but discard the result
Compare negative
Syntax: CMN{cond} Rn, Operand2
Usage: Add Operand2 to the value in Rn and updates the flags accordingly.
The result is discarded.
Examples:
CMN r1, #9 ;set the flags as if 9 was added to the value in r1.
CMN r6, r2 ;set the flags for the result of (r6 + r2) but discard the result
Data Movement
• Operations are:
• Syntax:
CF Destination 0
ASR #5 = divide by 32
MOV R0, R1, LSR #2 Sign bit shifted in
MOV R0, R1, LSR R2
Barrel Shifter - Rotations
Rotate Right (ROR) Rotate Right
• Similar to an ASR
but the bits wrap around as CF
they leave the LSB and Destination
appear as the MSB.
e.g. ROR #5
• Note the last bit rotated is
also used as the Carry Out.
• Pre-execution
• r0 = 0x00000000 , r1 = 0x02040608, r2 = 0x10305070
• Post-execution
• r0 = ?
Example
• This example shows a more complicated logical
instruction called BIC, which carries out a logical bit clear.
• PRE
r1 = 0b1111
r2 = 0b0101
BIC r0, r1, r2
• POST
r0 = 0b1010
• This is equivalent to
Rd = Rn AND NOT(N)
Example
• This example shows a logical OR operation between registers
r1 and r2. r0 holds the result.
• Pre-execution
• r0 = 0x00000000 , r1 = 0x02040608, r2 = 0x10305070
• Post-execution
• r0 = 0x12345678
Branch Instructions
• A branch instruction changes the flow of execution or is used to call a routine. This
type of instruction allows programs to have subroutines, if-then-else structures,
and loops.
• Syntax:
• B{<cond>} label
• BL{<cond>} label
• BX{<cond>} Rm
• BLX{<cond>} label | Rm
Branch Instructions
Example of forward and backward unconditional branch
B forward
ADD r1, r2, #4
ADD r0, r6, #2
ADD r3, r7, #4
forward
SUB r1, r2, #4
backward
ADD r1, r2, #4
SUB r1, r2, #4
ADD r4, r6, r7
B backward
Branch Instructions
The branch with link, or BL, instruction is similar to the B instruction but overwrites the
link register lr with a return address. It performs a subroutine call. This example shows
a simple fragment of code that branches to a subroutine using the BL instruction. To
return from a subroutine, you copy the link register to the pc.
Cond 0 0 0 1 0 B 0 0 Rn Rd 0 0 0 0 1 0 0 1 Rm
Swap
Load/Store Byte/Word
Cond 0 1 I P U B W L Rn Rd
Offset Load/Store Multiple
Cond 1 0 0 P U S W L Rn Register
List Halfword transfer : Immediate offset (v4 only)
Cond 0 0 0 P U 1 W L Rn Rd Offset1 1 S H 1 Offset2
Halfword transfer: Register offset (v4 only)
Cond 0 0 0 P U 0 W L Rn Rd 0 0 0 0 1 S H 1
Rm
Branch
Cond 1 0 1 L Offset Branch Exchange (v4T only)
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 Coprocessor data transfer
Cond 1 1 0 P U N W L Rn CRd CPNum Offset Coprocessor data operation
Cond 1 1 1 0 Op1 CRn CRd CPNum Op2 0 CRm Coprocessor register transfer
Cond 1 1 1 0 Op1 L CRn Rd CPNum Op2 1 CRm Software interrupt
Cond 1 1 1 1 SWI Number
Conditional Execution
• Most instruction sets only allow branches to be executed
conditionally.
• However by reusing the condition evaluation hardware, ARM
effectively increases number of instructions.
– All instructions contain a condition field which determines whether the
CPU will execute them.
– Non-executed instructions soak up 1 cycle.
• This removes the need for many branches, which stall the
pipeline (3 cycles to refill).
– Allows very dense in-line code, without branches.
– The Time penalty of not executing several conditional instructions is
frequently less than overhead of the branch or subroutine call that
would otherwise be needed.
The Condition Field
31 28 24 20 16 12 8 4 0
Cond
31 28 27 25 24 23 0
Cond 1 0 1 L Offset