Why does CMP (comparison) sometimes set the Carry flag in assembly 8086?

I read and with the 8086 instruction set, it says that CMP (comparison) can set the Carry flag. I understand that the comparison subtracts two operands, but I was wondering if anyone could provide an example when this is the case.

I just can't understand the idea of ​​adding a number, and a negative number will set the carry flag. I read the borrowing flag, but I just need an example to clarify my understanding of the comparison instruction.

Also, I understand that if 3 - 5 = -2 would set a negative flag ... when is hyphen set?

+6
source share
3 answers
  • The carry flag is set after the operation that led to overflow or overflow. For example, subtracting 10 from 6 will overflow and set the carry flag. Similarly, adding 1 to the maximum value of the register will overflow and set the carry flag.
  • The carry flag also changes during the switching operation; it is set to the value of the last bit offset from the destination register.
  • Bit testing will put the value of the tested bit in the carry flag. The codes that do this are: BT, BTC, BTR, and BTS.
  • Instructions that directly affect the carry flag: CLC, CMC, and STC.
  • During comparison, the carry flag is set as if the two operands were subtracted.
  • During negation (NEG), the carry flag is set if only the operand is zero, in which case it is cleared.
+4
source

The carry flag is usually set when using unsigned arithmetic. For example, adding two unsigned numbers (the result of which is not case-sensitive) will not lead to an overflow flag, but only to a flag. However, when using signed arithmetic in this case, an overflow flag is set.

+2
source

You can find examples where the carry and overflow flags are set to 0 and 1 after adding or subtracting integers in this answer to the corresponding question. <w> You can also find an example of C code that emulates adding with carry and subtraction using borrowing instructions for 8-bit numbers, and you can play with this, you might get more examples.

There is something like this in the output format:
127( 127) - 255( -1) - 1 = 127( 127) CY=1 OV=0
Where each number is represented as unsigned and enclosed in brackets (2 additions) next to it. The number before = is the carry flag before the ADC / SBB. CY= and OV= show carry and overflow flags after ADC / SBB.

Comparison does almost the same thing as subtraction without borrowing, except that it only affects the carry, overflow, sign and zero flags (and parity and auxiliary carry, but they are not important here) without changing the number in the register / memory.

+1
source

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


All Articles