Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

about assembly CF(Carry) and OF(Overflow) flag

It's known that CF indicates unsigned carry out and OF indicates signed overflow. So how does an assembly program differentiate between unsigned and signed data since it's only a sequence of bits? (Through additional memory storage for type information, or through positional information or else?) And could these two flags be used interchangeably?

like image 831
Pwn Avatar asked Apr 27 '09 01:04

Pwn


People also ask

What is CF flag in assembly?

The CF (carry flag) tells whether a bit was carried out of the word entirely (e.g. into bit 33 or bit 65). If numbers are interpreted as unsigned, carry flag means that addition overflowed, and the result is too large to fit in a machine word. The overflow flag is irrelevant.

What is overflow flag in assembly language?

Signed overflow means there has been an unexpected sign change in the result of an arithmetic operation involving signed numbers. This can happen when two signed numbers with the same sign are added or subtracted and the result ends up with the wrong sign. Signed overflow is indicated by setting the Overflow Flag.

What is the carry condition flag and the overflow condition flag?

In unsigned arithmetic, watch the carry flag to detect errors. In unsigned arithmetic, the overflow flag tells you nothing interesting. In signed arithmetic, watch the overflow flag to detect errors. In signed arithmetic, the carry flag tells you nothing interesting.

What is the difference between carry flag and overflow flag?

From a mechanistic point of view, the carry flag is set when there is a carry out of the most-significant bit. The overflow flag is set when the carry into the most significant bit is different from the carry out of it. With unsigned arithmetic you only have to worry about the carry flag.


1 Answers

The distinction is in what instructions are used to manipulate the data, not the data itself. Modern computers (since circa 1970) use a representation of integer data called two's-complement in which addition and subtraction work exactly the same on both signed and unsigned numbers.

  • The difference in representation is the interpretation given to the most significant bit (also called the sign bit). For unsigned numbers the most significant bit is set when the number is in the upper half of the wholly positive range. For signed numbers the most significant bit is set when the number is in the lower and negative half of the whole range.

  • Different instructions may use different interpretations of the same bit. For example most big machines have both signed and unsigned multiply instructions. Machines with a 'set less than' instruction may have both signed and unsigned flavors.

  • The OF (overflow flag) tells whether a carry flipped the sign of the most significant bit in the result so that it is different from the most significant bits of the arguments. If numbers are interpreted as unsigned, the overflow flag is irrelevant, but if they are interpreted as signed, OF means, e.g., two large positive numbers were added and the result was negative.

  • The CF (carry flag) tells whether a bit was carried out of the word entirely (e.g. into bit 33 or bit 65). If numbers are interpreted as unsigned, carry flag means that addition overflowed, and the result is too large to fit in a machine word. The overflow flag is irrelevant.

The answer to your question is that assembly code has several ways of distinguishing signed from unsigned data:

  • It may choose either CF or OF to do signed or unsigned comparisons.
  • It may choose either signed or unsigned multiply and divide instructions.
  • It may choose a signed or unsigned right shift (signed copies the high bit; unsigned shifts in zeroes).
like image 159
Norman Ramsey Avatar answered Sep 24 '22 04:09

Norman Ramsey