Question

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

Was it helpful?

Solution 2

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.

OTHER TIPS

First at all, assuming we are dealing with x86 architecture, it's defined as is in the Intel reference manual.

 IF DEST = 0
   THEN CF ← 0;
   ELSE CF ← 1;
 FI;
 DEST ← [– (DEST)]

A likely reason behind this arrangement is that the feature can be implemented as a side effect of a generalized addition/subtraction as

 DEST ← [ TEMP - (DEST) ]

Where TEMP can be [R/M] or the immediate 0, when the opcode is NEG. As a side effect, the ALU will calculate CF and the rest of the flags as if the operations was carried between two general purpose registers. This arrangement simply allows re-use of costly hardware. Later processors have simply confirmed to the specifications, even though the implementation was not based on the same micro-coded architecture.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top