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
negative operation in assembly language and carryflag
1.2k Views Asked by Nikunj Kumar At
2
There are 2 best solutions below
0

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.
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...
...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
and0
states can mean exactly the opposite of what you think.Short version: A good general rule is, don't use
Jc
andJnc
. Use the arithmetic conditional instructions instead.