Search code examples
assemblycarryflag

negative operation in assembly language and carryflag


when I am doing negative operation on A79CDB48h in assembly language,I am getting CF(carry flag ) to be set 1. Can someone please explain why this happen


Solution

  • Confused ? Understandable.

    I will assume that you are working with an x86 machine of some variety.

    The Carry Flag in this instance is not behaving like you would expect it to behave. Instead, it follows these rules...

    "...The CF flag set to 0 if the source operand is 0; otherwise it is set to 1. The OF, SF, ZF, AF, and PF flags are set according to the result....."

    I picked that up from THIS PAGE. Look in the "Flags affected" box.

    I forget which intel manual has this documented, so somebody, please help this guy with a link.

    They had their reasons for doing this, and they took care of the anomaly by documenting it. For the moment it's your job to live with the confusion and write code to accommodate it.

    If you're doing something like...

     Neg    Eax                    ;Negate some value
     Jc     It_was_positive        ;It was a positive number
     Jmp    It_was_negative        ;It was negative
    

    ...then change your Jc instruction to one of the signed arithmetic conditional jumps; and caveat: I'm not there writing your code for your specific problem, so this could be totally wrong for your immediate need. Test this before you trust it.

    Oh, warning for your future efforts: Other chips from other companies define the behavior of the carry flag according to their own desires. I think it's Microchip; maybe somebody else; whatever; they have designed their carry flag to work exactly opposite of the way other carry flags work. i.e., the 1 and 0 states can mean exactly the opposite of what you think.

    Short version: A good general rule is, don't use Jc and Jnc. Use the arithmetic conditional instructions instead.