</>EmbeddedInterview

What are NZCV condition flags?

EasyARM Architecture

Short answer: N is negative, Z is zero, C is carry (or no borrow for subtraction), and V is signed overflow. Flag-setting instructions and conditional branches use them.

CMPSet NZCVB.EQBranch if Z=1
CMP X0, #0
B.EQ is_zero

1. Flags

Flag Meaning
N Result sign bit
Z Result is zero
C Unsigned carry / no borrow
V Signed overflow

2. Example: CMP X0, #0 performs a subtraction for flags without storing the result. If X0 is zero, Z becomes 1 and B.EQ branches.

3. Signed vs unsigned: B.LT checks signed less-than (N differs from V); B.LO checks unsigned lower (C is clear). Choosing the wrong condition causes bugs.

CMP X0, X1
B.LO unsigned_lower
B.LT signed_lower

Interview takeaway: NZCV records arithmetic conditions and supports conditional execution and branching.

Signed overflow versus unsigned carry

For a 32-bit ADD instruction that updates flags:

// W0 = 0x7FFFFFFF, W1 = 1
ADDS W2, W0, W1
// W2 = 0x80000000: N=1, Z=0, C=0, V=1

The signed mathematical result exceeds INT32_MAX, so V is set. There is no unsigned carry out of bit 31.

If W0=0xFFFFFFFF and W1=1, the result is zero with N=0, Z=1, C=1 and V=0. Unsigned addition wraps while signed -1+1 is representable.

Condition Flag test Interpretation after CMP
EQ Z=1 Equal
NE Z=0 Not equal
LO / CC C=0 Unsigned lower
HS / CS C=1 Unsigned higher or same
LT N≠V Signed less than
GE N=V Signed greater or equal

CMP is an alias of a flag-setting subtraction whose result is discarded. Ordinary ADD does not update NZCV; ADDS does. CSEL can select one of two register values using a condition without an explicit branch. The compiler chooses such instructions when appropriate.