ARM Data-Processing Instructions
In questa pagina 5
In questa pagina 4
General form: OP{cond}{S} Rd, Rn, Operand2. They work only on registers and constants (see The ARM Core ArchitectureThe 32-bit ARM core as the reference microcontroller core: registers r0-r15 with SP, LR and PC, the status register CPSR with flags NZCV, processor modes, the load/store model, conditional execution, the flexible second operand, Thumb, and the main differences of 64-bit ARMv8.The ARM Core Architecture →).
Arithmetic and logical instructions
Definition (ARM data-processing instructions)
Instruction Effect ADD Rd, Rn, Op2ADC Rd, Rn, Op2(add with carry) SUB Rd, Rn, Op2SBC Rd, Rn, Op2RSB Rd, Rn, Op2(reverse subtract) AND Rd, Rn, Op2bitwise AND ORR Rd, Rn, Op2bitwise OR EOR Rd, Rn, Op2bitwise XOR BIC Rd, Rn, Op2(clears the indicated bits) MOV Rd, Op2MVN Rd, Op2MUL Rd, Rm, Rs(low 32 bits of the product) MLA Rd, Rm, Rs, Rn
Example. With r1=10 and r2=3: SUB r0, r1, r2 gives , RSB r0, r1, r2 gives (0xFFFFFFF9) and BIC r0, r1, #2 gives .
UMULL/SMULL RdLo, RdHi, Rm, Rs give the full 64-bit product, unsigned / signed. Multiplication does not accept constants: the operands are registers.
Second operand
Definition (second operand of ARM instructions)
- Immediate
#n: 8 bits rotated by an even number of positions. For arbitrary constants use the pseudo-instructionLDR r0, =0x12345678, which the assembler translates into a load from a constant table, or the pairMOVW/MOVT(low and high 16 bits).- Scaled register:
Rm, LSL #n(logical shift left),LSR #n(logical right),ASR #n(arithmetic right),ROR #n(rotate); the shift amount can also be held in a register (Rm, LSL Rs). The shift is free: it happens in the same cycle, in the barrel shifter before the arithmetic unit.
Example. With r1=6, ADD r0, r1, r1, LSL #2 computes in a single instruction.
Multiplications by constants without MUL:
ADD r0, r1, r1, LSL #2 @ r0 = r1 + 4*r1 = 5*r1
RSB r0, r1, r1, LSL #3 @ r0 = 8*r1 - r1 = 7*r1
MOV r0, r1, ASR #1 @ r0 = r1 / 2 rounded toward -infinity (signed)Flags
Definition (flag update: S, CMP and TST)
- With the suffix S the instruction updates N, Z, C, V:
SUBS r0, r0, #1decrements and sets Z when it reaches 0.CMP Rn, Op2computes and updates the flags without saving the result;CMNdoes the same with .TST Rn, Op2does the AND and updates N and Z (bit test);TEQdoes the XOR (equality).
Example. With r0=0x7FFFFFFF, ADDS r1, r0, #1 gives r1=0x80000000 with , , (no unsigned carry), (two positives give a negative).
Translating expressions
Variables a-e in r0-r4.
a = (b + c) - (d - e);ADD r5, r1, r2 @ r5 = b + c
SUB r6, r3, r4 @ r6 = d - e
SUB r0, r5, r6 @ a = r5 - r6a = b * 10 + (c & 0xFF);MOV r5, #10
MUL r5, r1, r5 @ r5 = b * 10
AND r6, r2, #0xFF @ r6 = c & 0xFF
ADD r0, r5, r6Property (bit operations with ORR, BIC and EOR). Set bit 3 of
r0:ORR r0, r0, #8; clear it:BIC r0, r0, #8; invert it:EOR r0, r0, #8; extract byte 1:AND r1, r0, #0xFF00thenMOV r1, r1, LSR #8.
Example. With r0=5 (0101) ORR r0, r0, #8 gives ; with r0=13 (1101) BIC r0, r0, #8 gives and EOR r0, r0, #8 gives .
Typical mistakes
- Writing
MUL r0, r1, #10: multiplication needs registers. - Forgetting the
Sbefore a conditional branch that must look at the result. - Using
LSRto divide a negative number:ASRis needed.
Versione ripasso
Form: OP{cond}{S} Rd, Rn, Operand2, registers and constants only (The ARM Core ArchitectureThe 32-bit ARM core as the reference microcontroller core: registers r0-r15 with SP, LR and PC, the status register CPSR with flags NZCV, processor modes, the load/store model, conditional execution, the flexible second operand, Thumb, and the main differences of 64-bit ARMv8.The ARM Core Architecture →).
Instructions
ADD, SUB; ADC (); SBC (); RSB (); AND, ORR, EOR; BIC ( AND NOT , clears bits); MOV, MVN (NOT); MUL Rd,Rm,Rs (low 32 bits); MLA (); UMULL/SMULL (64-bit product). MUL needs registers, not constants.
Second operand
- Immediate: 8 bits rotated by an even number of positions; any constant with
LDR r0,=0x12345678orMOVW/MOVT. - Scaled register:
LSL,LSR,ASR,ROR, free in the barrel shifter. ADD r0,r1,r1,LSL #2;RSB r0,r1,r1,LSL #3.
Flags
- Suffix S updates N, Z, C, V.
CMPandCMNdo not save the result;TSTandTEQupdate N and Z. ADDS r1,r0,#1withr0 = 0x7FFFFFFF0x80000000, N = 1, Z = 0, C = 0, V = 1.
Translating expressions
a = (b + c) - (d - e): ADD r5,r1,r2; SUB r6,r3,r4; SUB r0,r5,r6. a = b*10 + (c & 0xFF): MOV r5,#10; MUL r5,r1,r5; AND r6,r2,#0xFF; ADD r0,r5,r6.
Bit 3 of r0: ORR r0,r0,#8 sets it, BIC r0,r0,#8 clears it, EOR r0,r0,#8 inverts it.
Typical mistakes: MUL r0,r1,#10 (registers needed); forgetting S before a conditional branch; LSR on a negative (use ASR).