How to determine if the zero flag, sign flag, overflow flag, and carry flag are set?

The whole flag baffles me. The definitions on the Internet seem very simple. I can't seem to get a really useful explanation for all this.

According to their definitions, - carry: indicates the overflow of unsigned integers - overflow: indicates the overflow of an integer with a sign - zero: the operation produced zero - sign: the operation produced a negative number

So, how are the following sentences true in the world? - The following instructions will check the Sign box: (The answer here is not 252, not a negative number. So, why is the sign flag set?)

mov al,0FEh sub al,2 
  • Adding 7Fh and 05h in the 8-bit register sets the overflow flag (the answer is 132. Its not higher than 255, so why is this overflow?)

  • Adding 0FFh and 05h in the 8-bit register does not set an overflow flag (answer 300, since there is no overflow flag? Is it higher than 256)

  • Adding 5 to 0FBh in an 8-bit register sets the Zero flag (the answer here is 256, not 0. I understand that 8 bits can only contain 255, but where does "0" come from? Just don't get it.)

Can someone please tell me what I am doing wrong here, and what is the correct approach to this? Thanks.

+5
source share
2 answers

The answer here is 252, not a negative number. So why is the sign flag set?

As without a sign it is 252, but without a sign it cannot have a sign, therefore the flag of the sign is associated only with a number processed as signed. No matter how you handle it, the processor always treats it as signed with a sign icon. Thus, the value 252 exceeds 127, therefore it is negative in 2 parts and the sign bit is set.

Adding 7Fh and 05h in the 8-bit register sets the overflow flag (the answer is 132. Its not higher than 255, so why is this overflow?)

As you said, overflow occurs when the signed number is overflowed. A signed 8-bit variable can go from -128 to 127. Therefore, the transition from 127 to 132 is an overflow.

Adding 0FFh and 05h in the 8-bit register does not set an overflow flag (answer 300, since there is no overflow flag? Is it higher than 256)

Again, the overflow is crowded. This causes an unsigned overflow, so the carry bit will be set.

Adding 5 to 0FBh in an 8-bit register sets the Zero flag (the answer here is 256, not 0. I understand that 8 bits can only contain 255, but where is “0”? I just don't get it.)

As you said, 8 bits can increase to 255. After that, it overflows, and the lower 8 bits are 0. Thus, the result is zero, and the zero bit is set.

+6
source

Another great guide: Understanding the transfer / transfer conditions in Carry vs. It has some nice step-by-step examples with 4-bit numbers that make it easy to keep everything in mind. This also explains that unsigned carry is what you check when you interpret bits as unsigned, while signed overflow is what you check when interpreting bits as signed.


Based on OP comment:

Does this mean that the zero flag is really on when the value of the 8 least significant bit is 0, and not just "when the operation calls 0"

The main thing you need to remember is that it is a fixed width integer arithmetic. In an 8-bit register, 0xFF + 1 does produce 0.

In mathematical terms, this is modular arithmetic with a module of 2 8 for an 8-bit operation.

So yes, ZF set according to dst = (dst+src) % 0x100 .

It is designed this way because usually you just want to know if the register is zero, regardless of whether you count to zero with inc in a register that starts with a negative value, or you count to zero with the register that started .

You can check CF==0 and ZF==1 to find a case where you received zero without hyphenation. If ZF was installed only when dst and CF were zero, you would often need another command to simply test the results registry.


If CF and ZF are independent, they mean that unsigned codes after a cmp or sub work as follows:

 JA Jump if above (CF=0 and ZF=0). JAE Jump if above or equal (CF=0). JB Jump if below (CF=1). JBE Jump if below or equal (CF=1 or ZF=1). JC Jump if carry (CF=1). JE Jump if equal (ZF=1). 

I think that if ZF could only be installed when there was no transfer, you could not distinguish between Above and Above-or-Equal . So, probably, the most specific reason for the design decision should not be made as soon as your first assumption began.

Here is one of the signed comparison conditions:

 JLE Jump if less or equal (ZF=1 or SF ≠ OF). 

A complete set of conditions is provided in the Intel insn reference manual (links in the tag wiki), in the jcc instruction jcc (jump if condition).

+1
source

Source: https://habr.com/ru/post/1246975/


All Articles