Technical Support
On-Line Manuals
Assembler Reference
Conventions and feedback
Assembler command line options
ARM and Thumb Instructions
Instruction summary
Instruction width specifiers
Memory access instructions
General data processing instructions
Flexible second operand (Operand2)
Operand2 as a constant
Operand2 as a register with optional shift
Shift operations
Multiply instructions
Saturating instructions
Parallel instructions
Parallel add and subtract
Packing and unpacking instructions
Branch and control instructions
Coprocessor instructions
Miscellaneous instructions
ThumbEE instructions
Pseudo-instructions
Condition codes
ADD, SUB, RSB, ADC, SBC, and RSC
ADR (PC-relative)
ADR (register-relative)
ADRL pseudo-instruction
AND, ORR, EOR, BIC, and ORN
ASR, LSL, LSR, ROR, and RRX
B, BL, BX, BLX, and BXJ
BFC and BFI
BKPT
CBZ and CBNZ
CDP and CDP2
CHKA
CLREX
CLZ
CMP and CMN
CPS
CPY pseudo-instruction
DBG
DMB, DSB, and ISB
ENTERX and LEAVEX
ERET
HB, HBL, HBLP, and HBP
IT
LDC, LDC2, STC, and STC2
LDM and STM
LDR and STR (immediate offset)
LDR and STR (register offset)
LDR and STR, unprivileged
LDR (PC-relative)
LDR (register-relative)
LDR pseudo-instruction
LDREX and STREX
MAR and MRA
MCR, MCR2, MCRR, and MCRR2
MIA, MIAPH, and MIAxy
MOV and MVN
MOV32 pseudo--instruction
MOVT
MRC, MRC2, MRRC and MRRC2
MRS (system coprocessor register to ARM register)
MRS (PSR to general-purpose register)
MSR (ARM register to system coprocessor register)
MSR (general-purpose register to PSR)
MUL, MLA, and MLS
NEG pseudo-instruction
PKHBT and PKHTB
PLD, PLDW, and PLI
PUSH and POP
QADD, QSUB, QDADD, and QDSUB
REV, REV16, REVSH, and RBIT
RFE
SBFX and UBFX
SDIV and UDIV
SEL
SETEND
SEV, WFE, WFI, and YIELD
SMC
NOP
SMLAD and SMLSD
SMLALxy
SMLALD and SMLSLD
SMMUL, SMMLA, and SMMLS
SMUAD{X} and SMUSD{X}
SMULxy and SMLAxy
SMULWy and SMLAWy
SRS
SSAT and USAT
SSAT16 and USAT16
SUBS pc, lr
SVC
SWP and SWPB
SXT, SXTA, UXT, and UXTA
SYS
TBB and TBH
TST and TEQ
UMULL, UMLAL, SMULL, and SMLAL
UMAAL
UND pseudo-instruction
USAD8 and USADA8
VFP Programming
Directives Reference
|
Home / Assembler Reference
ADD, SUB, RSB, ADC, SBC, and RSC
ADD, SUB, RSB, ADC, SBC, and RSCAdd, Subtract, and Reverse Subtract, each with or without
Carry. Syntax
op{S}{cond} {Rd}, Rn, Operand2
op{cond} {Rd}, Rn, #imm12 ; 32-bit Thumb ADD and SUB only
where: opis one of: ADDADCSUBRSBSBCRSCReverse
Subtract with Carry (ARM only).
Sis
an optional suffix. If S is specified, the
condition code flags are updated on the result of the operation. condis an optional condition code. Rdis the destination register. Rnis the register holding the first operand. Operand2is a flexible second operand. imm12is any value in the range 0-4095.
Usage
The ADD instruction adds the values in Rn and Operand2 or imm12. The SUB instruction subtracts the value of Operand2 or imm12 from
the value in Rn. The RSB (Reverse Subtract) instruction subtracts
the value in Rn from
the value of Operand2.
This is useful because of the wide range of options for Operand2. You can use ADC, SBC, and RSC to
synthesize multiword arithmetic. The ADC (Add with Carry) instruction adds the
values in Rn and Operand2,
together with the carry flag. The SBC (Subtract with Carry) instruction subtracts
the value of Operand2 from
the value in Rn.
If the carry flag is clear, the result is reduced by one. The RSC (Reverse Subtract with Carry) instruction
subtracts the value in Rn from
the value of Operand2.
If the carry flag is clear, the result is reduced by one. In certain circumstances, the assembler can substitute one
instruction for another. Be aware of this when reading disassembly
listings. Use of PC and SP in Thumb instructions
In most of these instructions, you cannot use PC (R15)
for Rd, or any operand. The exceptions are: you can use
PC for Rn in 32-bit
Thumb ADD and SUB instructions, with a
constant Operand2 value
in the range 0-4095, and no S suffix. These instructions
are useful for generating PC-relative addresses. Bit[1] of the PC
value reads as 0 in this case, so that the base address for the
calculation is always word-aligned. you can use PC in 16-bit Thumb ADD{cond}
Rd, Rd, Rm instructions, where both registers cannot be PC.
However, the following 16-bit Thumb instructions are deprecated
in ARMv6T2 and above: ADD{cond}
PC, SP, PC
ADD{cond} SP, SP,
PC.
In most of these instructions, you cannot use SP (R13)
for Rd, or any operand.
Except that: You can use SP for Rn in ADD and SUB instructions ADD{cond} SP, SP,
SP is permitted but is deprecated in ARMv6T2 and above
ADD{S}{cond} SP, SP, Rm{,shift} and SUB{S}{cond}
SP, SP, Rm{,shift} are permitted
if shift is omitted
or LSL #1, LSL #2, or LSL #3.
Use of PC and SP in ARM instructions
You cannot use PC for Rd or
any operand in any data processing instruction that has a register-controlled
shift. With the exception of ADD and SUB,
use of PC for any operand, in instructions without register-controlled
shift, is deprecated. In SUB instructions without register-controlled
shift, use of PC is deprecated except for the following cases: In ADD instructions without register-controlled
shift, use of PC is deprecated except for the following cases: Use of PC for Rd in
instructions that do not add SP to a register Use of PC for Rn and
use of PC for Rm in
instructions that add two registers other than SP Use of PC for Rn in
the instruction ADD{cond} Rd,
Rn, #Constant.
If you use PC (R15) as Rn or Rm,
the value used is the address of the instruction plus 8. If you use PC as Rd: Execution branches to the
address corresponding to the result. If you use the S suffix,
see the SUBS pc,lr instruction.
You can use SP for Rn in ADD and
SUB instructions, however, ADDS PC, SP, #Constant and SUBS PC,
SP, #Constant are deprecated. You can use SP in ADD (register) and SUB (register)
if Rn is SP and shift is
omitted or LSL #1, LSL #2,
or LSL #3. Other uses of SP in these ARM instructions are deprecated. NoteThe deprecation of SP and PC in ARM instructions is only in
ARMv6T2 and above. Condition flags
If S is specified, these instructions
update the N, Z, C and V flags according to the result. 16-bit instructions
The following forms of these instructions are available in
Thumb code, and are 16-bit instructions: ADDS Rd, Rn,
#immimm range
0-7. Rd and Rn must
both be Lo registers.
ADDS Rd, Rn, RmRd, Rn and Rm must
all be Lo registers.
ADD Rd, Rd, RmARMv6 and earlier: either Rd or Rm,
or both, must be a Hi register. ARMv6T2 and above: this restriction
does not apply. ADDS Rd, Rd,
#immimm range
0-255. Rd must be
a Lo register.
ADCS Rd, Rd, RmRd, Rn and Rm must
all be Lo registers.
ADD SP, SP, #immimm range
0-508, word aligned.
ADD Rd,
SP, #immimm range
0-1020, word aligned. Rd must
be a Lo register.
ADD Rd,
pc, #immimm range
0-1020, word aligned. Rd must
be a Lo register. Bits[1:0] of the PC are read as 0 in this instruction.
SUBS Rd, Rn, RmRd, Rn and Rm must
all be Lo registers.
SUBS Rd, Rn,
#immimm range
0-7. Rd and Rn both
Lo registers.
SUBS Rd, Rd,
#immimm range
0-255. Rd must be
a Lo register.
SBCS Rd, Rd, RmRd, Rn and Rm must
all be Lo registers.
SUB SP, SP, #immimm range
0-508, word aligned.
RSBS Rd, Rn,
#0Rd and Rn both
Lo registers.
Examples
ADD r2, r1, r3
SUBS r8, r6, #240 ; sets the flags on the result
RSB r4, r4, #1280 ; subtracts contents of R4 from 1280
ADCHI r11, r0, r3 ; only executed if C flag set and Z
; flag clear
RSCSLE r0,r5,r0,LSL r4 ; conditional, flags set
Incorrect example
RSCSLE r0,pc,r0,LSL r4 ; PC not permitted with register
; controlled shift
Multiword arithmetic examples
These two instructions add a 64-bit integer contained in R2 and R3 to
another 64-bit integer contained in R0 and R1,
and place the result in R4 and R5.
ADDS r4, r0, r2 ; adding the least significant words
ADC r5, r1, r3 ; adding the most significant words
These instructions subtract one 96-bit integer from another:
SUBS r3, r6, r9
SBCS r4, r7, r10
SBC r5, r8, r11
For clarity, the above examples use consecutive registers
for multiword values. There is no requirement to do this. The following,
for example, is perfectly valid:
SUBS r6, r6, r9
SBCS r9, r2, r1
SBC r2, r8, r11
See also
|