what is the diffrence between zero flag and carry flag ?

Osama Al-far picture Osama Al-far · Jun 8, 2012 · Viewed 20.5k times · Source

What is the difference between the zero flag and carry flag?

For zero flag it said

mov ax , 0FFFF ; 
inc ax ; AX= 0 , ZF=1

For carry flag it said

mov al , 0FFh
add al , 1  ; CF=1,AL=00

The value of AL is equal to zero so why is the zero flag not set to 1?

Answer

old_timer picture old_timer · Jun 8, 2012

It sounds like you understand what the flags mean/do, just in case:

the zero flag is quite simple if the result of the operation is a zero the zero flag will be set otherwise it will be clear

The carry flag is used a number of ways but in this case with an add operation, it is the carry out bit, an unsigned overflow.

Say you were adding the two decimal numbers 92 and 5 you get 97, no problem, if you add 92 and 12, you get 104, but what if you had a decimal computer or had a small scrap of paper and you could only hold two digits as a result then the answer is 04 not 104, the answer may be considered wrong depending on what you were wanting to do. As you were doing that math you were saying to yourself 2+2 = 4, then 9 + 1 = 0 carry the 1. that is where carry comes from you carry that one to the next column but the processor has a limited number of columns.

The add logic (sub uses an add) does not know or care about signed (twos complement) vs unsigned numbers (that is the beauty of twos complement), you feed the operands to the same logic you get the same answer. Where it will differ is in the overflow, unsigned overflow is the carry out bit. Signed overflow if the processor has it (I am speaking generally not specific to one processor) detects if the result does not contain enough information to store the answer. 0x80 + 0x80 = 0x00 for example. unsigned that is 128 + 128 and you get a signed overflow, unsigned it is -128 + -128 the result is -256. You have added two negative numbers, minus plus a minus equals a minus but you ended up with 0x00 which is a positive number, overflow. so 0x80 + 0x80 for 8 bit systems that support both overflows will give both overflows (plus the zero flag as an added bonus).

No matter what the instruction set if you are using the flags you need to look up and or memorize the flags affected by each instruction (you are using to set flags). Each instruction set has different flag rules, not every alu instruction affects all the flags, etc. Cases where you have an inc instruction separate from an add with (small) immediate instruction which could be used to perform an inc it is within reason that the designers would have one affect flags and the other not. For a case like this one in particular where you have these split definitions of the same register, EAX, AX, AH, AL, where you can do operations on fractions of the register do you declare an AH operation to be zero even if the rest of the register is not zero? Is it a definition based on the operation size or the whole register outside the portion you operated on? Everyone here may know the answer, but as soon as you saw register definitions when first starting to learn this processor that should have been a first thought. And until you look up the answer and memorize it you should look it up each time you want to get flags out of an instruction. Designers will intentionally put operations in that dont mess with flags to save on instructions needed to perform an operation. Some may not some force the flags to always be the instruction before the branch on decision, some (mips) the branch itself is the alu operation and decision (no flags).

The answer to your question is look it up in the documentation, make a habit of looking it up. Dont assume that all alu operations affect all flags, look it up for the instruction and registers in question. The answer to "why" is because that is what they felt like doing, it is what it is, unless you are on a team designing a new processor, as a programmer you get what you get, work with it...Your question itself leads to expectations of different answers, you switch from 16 bit to 8 bit operations and you switch from an add to inc, either one or both is perfectly valid for the designers to change the definitions for, expect them to.