Home · part 9 of 9

Flag drills: predict, then check

Short randomised exercises on CMP, the N Z C V flags, ARM's signed and unsigned branches, and the two widening loads. Each one asks for your answer before it shows the working, drawn the same way as the Bit Lab.

Score this browser: flags 0 / 0 branches 0 / 0 type 0 / 0 widen 0 / 0

1. Predict the flags

A CMP subtracts B from A at 8 bits and keeps only the flags. Work out N, Z, C and V on paper before you press check. ARM's rule: C is 1 when no borrow was needed.


    
NZCV

2. Which branches fire

No numbers this time, only the four flags a CMP left behind. Press every branch that would be taken. The unsigned family reads C and Z, the signed family reads N, V and Z, and BEQ and BNE read Z alone.


    
Taken:

3. Recover the type

Here is a compiled if. Say whether the variables were declared unsigned or signed, and which comparison the C source used. There are two clues to the type, the load and the branch, and one trap: a compiler often branches over the body when the test fails, so read where the branch goes before you read its condition.


    
Declared as
if (ab)

4. Widen by hand

A byte is loaded into a 32-bit register. Write the register's contents as eight hex digits, and its value as a decimal number, read the way the load intended. LDRB zero-extends; LDRSB sign-extends.


    

How to use these