Salta al contenuto
Note per Studenti ARM Data-Processing Instructions Report

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, Op2 Rd=Rn+Op2Rd = Rn + Op2
ADC Rd, Rn, Op2 Rd=Rn+Op2+CRd = Rn + Op2 + C (add with carry)
SUB Rd, Rn, Op2 Rd=Rn−Op2Rd = Rn - Op2
SBC Rd, Rn, Op2 Rd=Rn−Op2−C‾Rd = Rn - Op2 - \overline{C}
RSB Rd, Rn, Op2 Rd=Op2−RnRd = Op2 - Rn (reverse subtract)
AND Rd, Rn, Op2 bitwise AND
ORR Rd, Rn, Op2 bitwise OR
EOR Rd, Rn, Op2 bitwise XOR
BIC Rd, Rn, Op2 Rd=Rn AND NOT Op2Rd = Rn \text{ AND NOT } Op2 (clears the indicated bits)
MOV Rd, Op2 Rd=Op2Rd = Op2
MVN Rd, Op2 Rd=NOT Op2Rd = \text{NOT } Op2
MUL Rd, Rm, Rs Rd=Rm⋅RsRd = Rm \cdot Rs (low 32 bits of the product)
MLA Rd, Rm, Rs, Rn Rd=Rm⋅Rs+RnRd = Rm \cdot Rs + Rn

Example. With r1=10 and r2=3: SUB r0, r1, r2 gives 77, RSB r0, r1, r2 gives −7-7 (0xFFFFFFF9) and BIC r0, r1, #2 gives 88.

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-instruction LDR r0, =0x12345678, which the assembler translates into a load from a constant table, or the pair MOVW/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 6+4⋅6=306+4\cdot6=30 in a single instruction.

Multiplications by constants without MUL:

armasm
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, #1 decrements and sets Z when it reaches 0.
  • CMP Rn, Op2 computes Rn−Op2Rn - Op2 and updates the flags without saving the result; CMN does the same with Rn+Op2Rn + Op2.
  • TST Rn, Op2 does the AND and updates N and Z (bit test); TEQ does the XOR (equality).

Example. With r0=0x7FFFFFFF, ADDS r1, r0, #1 gives r1=0x80000000 with N=1N=1, Z=0Z=0, C=0C=0 (no unsigned carry), V=1V=1 (two positives give a negative).

Translating expressions

Variables a-e in r0-r4.

c
a = (b + c) - (d - e);
armasm
ADD r5, r1, r2          @ r5 = b + c
SUB r6, r3, r4          @ r6 = d - e
SUB r0, r5, r6          @ a = r5 - r6
c
a = b * 10 + (c & 0xFF);
armasm
MOV r5, #10
MUL r5, r1, r5          @ r5 = b * 10
AND r6, r2, #0xFF       @ r6 = c & 0xFF
ADD r0, r5, r6

Property (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, #0xFF00 then MOV r1, r1, LSR #8.

Example. With r0=5 (0101) ORR r0, r0, #8 gives 1313; with r0=13 (1101) BIC r0, r0, #8 gives 55 and EOR r0, r0, #8 gives 55.

Typical mistakes

  • Writing MUL r0, r1, #10: multiplication needs registers.
  • Forgetting the S before a conditional branch that must look at the result.
  • Using LSR to divide a negative number: ASR is 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 (Rn+Op2+CRn + Op2 + C); SBC (Rn−Op2−C‾Rn - Op2 - \overline{C}); RSB (Op2−RnOp2 - Rn); AND, ORR, EOR; BIC (RnRn AND NOT Op2Op2, clears bits); MOV, MVN (NOT); MUL Rd,Rm,Rs (low 32 bits); MLA (Rm⋅Rs+RnRm \cdot Rs + Rn); 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,=0x12345678 or MOVW/MOVT.
  • Scaled register: LSL, LSR, ASR, ROR, free in the barrel shifter.
  • ADD r0,r1,r1,LSL #2 =5r1= 5 r_1; RSB r0,r1,r1,LSL #3 =7r1= 7 r_1.

Flags

  • Suffix S updates N, Z, C, V. CMP and CMN do not save the result; TST and TEQ update N and Z.
  • ADDS r1,r0,#1 with r0 = 0x7FFFFFFF →\to 0x80000000, 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).

Teoria collegata