assemblyarmcomparecarryflag

Confusion about ARM documentation on carry flag


In the ARM documentation here, it says that:

A carry occurs:

... if the result of a subtraction is positive or zero ...

I know from this answer on SO that the carry flag is set in subtraction when there is unsigned underflow (ie the number being subtracted (subtrahend) is greater than the number that is being subtracted from (minuend)).

So consider the example:

r1 = 5
r2 = 10
CMP r1, r2

Compare (CMP) does a subtraction as mentioned here and then sets the appropriate flags. In this case, r1-r2 = 5-10 = -5. Since we have unsigned underflow here (5 < 10), we expect the carry flag to be set. But according to the ARM documentation, the result (-5) is not positive or zero, so the carry flag should not be set. (Assuming we look at the result signed; otherwise a carry would never occur according to the documentation).

Is the ARM documentation wrong? What's my misunderstanding?


Solution

  • ARM uses an inverted carry flag for borrow (i.e. subtraction). That's why the carry is set whenever there is no borrow and clear whenever there is. This design decision makes building an ALU slightly simpler which is why some CPUs do it.