Course: Computer Architectures · Lecture: ARM ALU Instructions and Flags
The ALU pattern
Most ALU instructions read one or two operands, calculate a result, and write a destination register:
ADD r0, r1, r2 ; r0 = r1 + r2The second operand may be a register, a shifted register or an encodable immediate. An S suffix asks the instruction to update APSR flags.
Flags
| Flag | Meaning |
|---|---|
N | result’s highest bit is one |
Z | result is zero |
C | carry after addition; no borrow after subtraction |
V | signed overflow |
These flags answer different questions. For example, 0x7FFFFFFF + 1 produces 0x80000000: V=1 because signed range was exceeded, while C=0 because unsigned carry did not leave the 32-bit range. 0xFFFFFFFF + 1 produces zero with C=1 and V=0.
CMP, CMN, TST and TEQ
CMP r0, #12performs r0-12 for the flags and leaves r0 unchanged. CMN performs an addition for flags. TST performs AND and is useful for bit masks:
TST r1, r0, LSL #4Z=1 means the selected bits were all zero. TEQ performs XOR, so Z=1 means the operands have equal bit patterns.
Addition and subtraction
ADD r0, r1, r2 ; r0 = r1+r2
ADC r0, r1, r2 ; r0 = r1+r2+C
SUB r0, r1, r2 ; r0 = r1-r2
SBC r0, r1, r2 ; r0 = r1-r2+C-1
RSB r0, r1, r2 ; r0 = r2-r1RSB is useful when the shifted operand should be the left side:
SUB r0, r1, r2, LSL #2 ; r0 = r1 - 4*r2
RSB r0, r2, r1, LSL #2 ; r0 = 4*r1 - r2For a 64-bit value stored high:low in r1:r0, add with:
ADDS r4, r0, r2
ADC r5, r1, r3The first instruction creates the carry that the second consumes. Subtraction uses SUBS followed by SBC.
Multiplication and division
MUL r0, r1, r2 ; 32-bit product
UMULL r0, r1, r2, r3 ; unsigned 64-bit product in r1:r0
SMULL r0, r1, r2, r3 ; signed 64-bit product
MLA r0, r1, r2, r3 ; r0 = r1*r2+r3
MLS r0, r1, r2, r3 ; r0 = r1*r2-r3UDIV and SDIV perform unsigned and signed division. They round toward zero when the division is not exact and do not update flags.
Logic
AND r0, r1, r2 ; keep bits set in both
BIC r0, r1, r2 ; r1 AND NOT r2
ORR r0, r1, r2 ; set bits present in either
EOR r0, r1, r2 ; set bits that differ
ORN r0, r1, r2 ; r1 OR NOT r2
MVN r0, r1 ; NOT r1A bit mask makes these operations practical. TST is the flag-setting version of an AND test; TEQ is the flag-setting XOR test.
Shifts, rotates and multiplication shortcuts
LSL r1, r0, #3 ; r1 = r0*8
LSR r1, r0, #1 ; logical right shift
ASR r1, r0, #1 ; signed right shift
ROR r1, r0, #1 ; rotate right
RRX r1, r0 ; rotate right through carryThe barrel shifter can be combined with arithmetic:
ADD r1, r0, r0, LSL #3 ; r1 = 9*r0
RSB r1, r0, r0, LSL #3 ; r1 = 7*r0This can avoid a multiplier for simple constant factors.
Worked 64-bit shifts
Let the pair r1:r0 contain:
r1 = 0x00000001
r0 = 0x80000000For a left shift by one:
LSLS r2, r0, #1
ADC r3, r1, r1The low word shifts to zero and its old top bit enters the high word, producing r3:r2 = 0x00000003:0x00000000.
For a logical right shift:
LSRS r4, r1, #1
RRX r5, r0The old low bit of r1 enters the top bit of r0, producing r4:r5 = 0x00000000:0xC0000000.
For an arithmetic right shift, use ASRS on the high word so its sign bit is replicated, then RRX on the low word.
Common confusions
CandVare not interchangeable: one is about unsigned carry/no-borrow, the other signed overflow.CMPsets flags but does not save a subtraction result.ADDandADDScalculate the same arithmetic result; they differ in flag side effects.LSRandASRdiffer only visibly when the sign bit matters.- In a 64-bit pair, the register written first is not automatically the high word; the pair convention must be stated.
Retrieval checks
Q: What does ADC add beyond ADD?
A: The current carry flag.
Q: What does SBC use from an earlier subtraction?
A: The carry/no-borrow flag through first-second+C-1.
Q: Why does RRX matter in a multiword shift?
A: It uses carry as the bit bridge between words.
Q: What does ADD r1,r0,r0,LSL #3 calculate?
A: r0 + 8*r0 = 9*r0.