Section 4.42

Condition flags (APSR)

Full name: Condition flags of the Application Program Status Register (APSR) in Arm M-profile processors.

Definition

Condition flags are single status bits that an arithmetic, logical or compare instruction sets to describe its result, so that a later instruction can act on that description. On Arm M-profile processors, the Cortex-M family, they are four bits of the Application Program Status Register (APSR): N (negative, a copy of the result’s top bit), Z (zero), C (carry: the unsigned result did not fit, or for a subtraction, no borrow occurred) and V (overflow: the signed result did not fit). Conditional branches and conditional instructions read them.

The idea

A processor adds two 32-bit registers and gets 32 bits back. The same 32 bits can be read as an unsigned number, from 0 to 232−12^{32} - 1, or as a signed number in two’s complement, the standard encoding in which the top bit counts as −231-2^{31}, giving the range −231-2^{31} to 231−12^{31} - 1. The adder does not know which reading the program intends. It produces one bit pattern, which is the correct answer under both readings whenever the true result fits. Whether the true result fits, however, depends on the reading: 0xFFFFFFFF plus 1 does not fit as unsigned (4,294,967,295+14{,}294{,}967{,}295 + 1) but fits as signed (−1+1=0-1 + 1 = 0).

So the processor reports both facts and leaves the choice to the program. C answers “did the unsigned result fit?” and V answers “did the signed result fit?”; N and Z describe the stored result. A later conditional instruction combines the flags into the comparison the programmer meant: unsigned “lower” tests C, and signed “less than” tests N and V together. Carry and overflow are not a weak and a strong version of one check. They are the same question asked about two different readings of the same bits.

How it works

The APSR holds the flags in its top bits: N in bit 31, Z in bit 30, C in bit 29 and V in bit 28. Bit 27 is the Q flag, which saturating instructions set to 1 when they had to clamp a result to the largest or smallest representable value, and which only an explicit write to the APSR clears. Processors with the DSP (digital signal processing) extension also have four GE (greater than or equal) flags in bits 19 to 16, set by instructions that work on several bytes or halfwords at once. Conditions read only N, Z, C and V.

An addition a+ba + b of two 32-bit values that sets flags, such as ADDS (add and set flags), sets:

A subtraction a−ba - b, such as SUBS or CMP (compare, a subtraction that keeps only the flags), is computed as the addition a+NOT(b)+1a + \mathrm{NOT}(b) + 1, where NOT(b)\mathrm{NOT}(b) inverts every bit of bb. The flags are those of that addition. C is the carry out of it, which is 1 exactly when a≥ba \ge b as unsigned numbers. On Arm, therefore, C = 1 after a subtraction means that no borrow occurred. Other processors, such as the x86 family, use the opposite convention and set their carry flag after a subtraction when a borrow did occur.

Logical instructions that set flags, such as ANDS, ORRS, EORS, MOVS and TST, set N and Z from the result and leave V unchanged. They set C from the shifter, the circuit that prepares the second operand: to the last bit shifted out when that operand was a register shifted by a nonzero amount, to bit 31 of the constant when the constant was formed by rotation, and otherwise they leave C unchanged. The Modified immediate encoding §4.43 entry gives the rule for constants.

The flags are read by conditional branches, by instructions in an IT (If-Then) block, by ADC and SBC (add and subtract with carry), which use C as an extra input in order to build arithmetic wider than 32 bits, and by MRS (move from special register), which copies the APSR into a general-purpose register. An instruction that does not set flags leaves them unchanged, so the flags that a branch reads are those of the most recent instruction that set them, which need not be the nearest compare.

Worked example

Three additions with ADDS, using 32-bit values.

  1. 0xFFFFFFFF plus 0x00000001. Unsigned: 4,294,967,295+1=4,294,967,296=2324{,}294{,}967{,}295 + 1 = 4{,}294{,}967{,}296 = 2^{32}, which does not fit, so C = 1, and the stored result is 232−232=02^{32} - 2^{32} = 0. Signed: −1+1=0-1 + 1 = 0, which fits, so V = 0. The result 0 gives Z = 1 and N = 0.

  2. 0x7FFFFFFF plus 0x00000001. Unsigned: 2,147,483,647+1=2,147,483,6482{,}147{,}483{,}647 + 1 = 2{,}147{,}483{,}648, less than 2322^{32}, so C = 0. Signed: the same sum exceeds the largest signed value, 231−1=2,147,483,6472^{31} - 1 = 2{,}147{,}483{,}647, so V = 1. The stored result 0x80000000 gives N = 1 and Z = 0; read as signed, it is −2,147,483,648-2{,}147{,}483{,}648, a large positive sum turned negative.

  3. 0x80000000 plus 0x80000000. Unsigned: 231+231=2322^{31} + 2^{31} = 2^{32}, so C = 1. Signed: −231+(−231)=−232-2^{31} + (-2^{31}) = -2^{32}, below −231-2^{31}, so V = 1. The stored result is 0, so Z = 1 and N = 0.

The three cases give C = 1 with V = 0, C = 0 with V = 1, and both set.

Two comparisons with CMP r0, r1, which computes r0−r1r0 - r1 for the flags only.

  1. r0 = 0x00000001, r1 = 0xFFFFFFFF. As an addition: 1+NOT(𝟶𝚡𝙵𝙵𝙵𝙵𝙵𝙵𝙵𝙵)+1=1+0+1=21 + \mathrm{NOT}(\texttt{0xFFFFFFFF}) + 1 = 1 + 0 + 1 = 2, with no carry out, so C = 0: as unsigned numbers, 1<4,294,967,2951 < 4{,}294{,}967{,}295 and a borrow occurred. Signed: 1−(−1)=21 - (-1) = 2, which fits, so V = 0. The result 2 gives N = 0 and Z = 0. The unsigned condition LO (lower, C = 0) holds, and indeed 1 is below 4,294,967,295. The signed condition LT (less than, N and V differ) does not hold, and indeed 1 is not less than −1-1. One compare has answered both questions correctly.

  2. r0 = 0x80000000, r1 = 0x00000001. As an addition: 𝟶𝚡𝟾𝟶𝟶𝟶𝟶𝟶𝟶𝟶+𝟶𝚡𝙵𝙵𝙵𝙵𝙵𝙵𝙵𝙴+1=𝟶𝚡𝟷𝟽𝙵𝙵𝙵𝙵𝙵𝙵𝙵\texttt{0x80000000} + \texttt{0xFFFFFFFE} + 1 = \texttt{0x17FFFFFFF}, which needs 33 bits, so the carry out is 1 and the stored result is 0x7FFFFFFF. That gives C = 1 (as unsigned numbers, 2,147,483,648≥12{,}147{,}483{,}648 \ge 1), N = 0 and Z = 0. Signed: −2,147,483,648−1=−2,147,483,649-2{,}147{,}483{,}648 - 1 = -2{,}147{,}483{,}649, below −231-2^{31}, so V = 1. LT tests whether N and V differ: N = 0 and V = 1 differ, so LT holds, and indeed −231<1-2^{31} < 1. A test of N alone, the condition MI (minus), would answer “not less”, because the overflow flipped the sign of the stored result.

Stated precisely

Let aa and bb be 32-bit patterns. Read as unsigned numbers they are u(a)u(a) and u(b)u(b), with 0≤u<2320 \le u < 2^{32}. Read as signed two’s-complement numbers they are s(a)s(a) and s(b)s(b), where s(x)=u(x)−232s(x) = u(x) - 2^{32} if bit 31 of xx is 1 and s(x)=u(x)s(x) = u(x) otherwise, so −231≤s<231-2^{31} \le s < 2^{31}. Write [P][P] for 1 if the statement PP is true and 0 if it is false. For ADDS, the stored result is r=(u(a)+u(b))mod232r = (u(a) + u(b)) \bmod 2^{32} and N=bit 31 of r,Z=[r=0],C=[u(a)+u(b)≥232],V=[s(a)+s(b)<−231 or s(a)+s(b)≥231].\begin{aligned} N &= \text{bit 31 of } r, \qquad Z = [r = 0], \\ C &= [u(a) + u(b) \ge 2^{32}], \\ V &= [s(a) + s(b) < -2^{31} \text{ or } s(a) + s(b) \ge 2^{31}]. \end{aligned} For SUBS and CMP, the stored result is r=(u(a)−u(b))mod232r = (u(a) - u(b)) \bmod 2^{32}, N and Z are defined from rr in the same way, and C=[u(a)≥u(b)],V=[s(a)−s(b)<−231 or s(a)−s(b)≥231].C = [u(a) \ge u(b)], \qquad V = [s(a) - s(b) < -2^{31} \text{ or } s(a) - s(b) \ge 2^{31}].

After CMP with operands aa and bb, the conditions mean:

Why LT is N≠VN \ne V: let d=s(a)−s(b)d = s(a) - s(b), the true signed difference. If V=0V = 0, dd fits, the stored result read as signed equals dd, and N=1N = 1 exactly when d<0d < 0. If V=1V = 1, dd lies outside the signed range, and the stored result read as signed is d+232d + 2^{32} when d<−231d < -2^{31}, which is positive, or d−232d - 2^{32} when d≥231d \ge 2^{31}, which is negative. Either way N is then the opposite of the true sign. In both cases, N≠VN \ne V holds exactly when d<0d < 0.

These meanings hold for flags set by a subtraction of bb from aa. After any other instruction, the conditions test the same flag formulas, but the flags mean whatever that instruction put into them. The definitions are those of the Armv7-M architecture.

Limits and common mistakes

Carry is not signed overflow §4.15, and neither implies the other: the three additions of the worked example show every combination except both clear, which 1+11 + 1 supplies.

On Arm, C after a subtraction means no borrow. Reasoning carried over from x86, where the carry flag after a subtraction means a borrow, gets every unsigned condition backwards.

N alone does not mean less than. After a compare that overflows, N has the wrong sign; signed comparisons §2.12 need N and V together, as the second comparison shows.

Not every instruction sets every flag. Logical instructions never change V and change C only through a shift or a rotated constant. Inside an IT block §4.41, 16-bit instructions other than compares and tests set no flags. Instructions without the S suffix set none. The flags a later instruction reads come from the most recent instruction that set them.

For arithmetic wider than 32 bits, built from ADDS followed by ADCS, the flags after ADCS come from the upper 32 bits only. In particular, Z then says whether the upper half of the result is zero, not whether the whole result is.

The flags belong to the interrupted code during an interrupt §6.11. On exception entry a Cortex-M processor saves the program status register, flags included, on the stack and restores it on return, so a handler that changes the flags does not disturb the code it interrupted.

Questions answered

Is carry the same as signed overflow?

No. The carry flag C reports that a result did not fit when the operands are read as unsigned numbers: after an addition, C = 1 means the true sum reached 2322^{32}; after an Arm subtraction, C = 1 means no borrow occurred, that is, the first operand was at least the second. The overflow flag V reports that the result did not fit when the same operands are read as signed two’s-complement numbers. One addition can set either flag, both or neither. On 32 bits, 0xFFFFFFFF plus 1 sets C but not V, because −1+1=0-1 + 1 = 0 fits as signed; 0x7FFFFFFF plus 1 sets V but not C; 0x80000000 plus 0x80000000 sets both; and 1+11 + 1 sets neither.

Use C for unsigned decisions and V, together with N, for signed ones: after a compare, unsigned lower is C = 0, and signed less than is N different from V. Logical instructions show a further difference: they never change V, and they set C from a shift or a rotated constant, not from an addition.

Related entries: Signed versus unsigned comparison (§2.12), Modified immediate encoding (§4.43), Conditional execution and IT block (§4.41), Signed integer overflow (§4.15), Unsigned modular arithmetic (§4.16). Further reading: [6, 81].