This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Carry-Bit vs. Documentation

Note: This was originally posted on 27th June 2012 at http://forums.arm.com

Hello,

in every documentation I found something like

"A carry occurs (..), if the result of a subtraction is positive, (...)."

If I understand this right after a substraction (this is important)  with a negativ (N = 1) result there can´t occur a carry, isn´t it?

For examble if I try

0x7FFF FFFF - 0xFFFF FFFF = 0x8000 0000
The CPSR are set like this NzcVqIFt_SVC => N = 1, C  = 0, V = 1

After a substraction - Carry (borrow) = 0 means we have a borrow! Right?
So a carry occurs but the result is negativ!

Is the documentation right and complete? Or is there something missing or I´m completely wrong?

Thanks
René
  • Note: This was originally posted on 27th June 2012 at http://forums.arm.com

    Subtract / compare can produce all combinations of flags except negative-zero (N=1, Z=1) and (N=0, C=0, V=1);
    however, the combinations of (N=0, C=0, V=0, Z=1) and (N=0, C=1, V=1, Z=1) require the use of SBCS/RSCS (subtract with carry).

    A simple subtract of the value one from value minus-one will produce a negative result (minus-two) and set the carry flag (not borrow).

    In summary, I don't think the documentation you are referring to is definitive.

    hth
    s.
  • Note: This was originally posted on 27th June 2012 at http://forums.arm.com

    If you are talking about my Cortex-M3 book, look like it is my fault... I didn't double check all cases.
    So the "If the result of a subtraction is positive or zero" text is incorrect. Sorry.
    (This might originated from RealView Compilation Tools Assembler Guide...including latest DS-5 document.  I will log a defect on this.)

    I can't find the definition in the ARMv7-M architecture reference manual either, but got this in ARMv6-M architecture reference manual:

    C is set in one of four ways:

    • For an addition, including the comparison instruction CMN, C is set to 1 if the addition produced a carry (that is, an unsigned overflow), and to 0 otherwise.
    • For a subtraction, including the comparison instruction CMP, C is set to 0 if the subtraction produced a borrow (that is, an unsigned underflow), and to 1 otherwise.
    • For non-addition/subtractions that incorporate a shift operation, C is set to the last bit shifted out of the value by the shifter.
    • For other non-addition/subtractions, C is normally left unchanged (but see the individual instruction descriptions for any special cases).
    regards,
    Joseph