Question: The flag `CF` is set when we compare two digits and one number is less than another:

```
mov eax,1
cmp eax,2
jb truWay

```

In binary form: 

```
1=00000001 
2=00000010=11111101+1(add. code)=11111110и 
00000001+11111110=11111111(no carry)

```

Why does instruction `cmp eax,2` set the `CF` flag? There was no carry, or I'm wrong.

Answer: 
You can refer to [these slides](http://web.cse.ohio-state.edu/~teodorescu.1/download/teaching/cse675.au08/Cse675.02.F.ALUDesign_part1.pdf) to understand how subtraction is performed in an ALU.

While it is true that A - B = A + (-B) and that in two's complement this is A + ¬B + 1, this consideration accounts for the result only. 

A subtracter implemented with half-adders takes the form

              [![A subtracter implemented with half-adders, note the inverted output carry](https://i.stack.imgur.com/0C4Ni.png)](https://i.stack.imgur.com/0C4Ni.png)

Note that the *CarryOut* bit is inverted. 

You can check that this is necessary and correct by taking your example (limiting the size to 4 bits):

1 - 2 = 1 + (-2) = 0001b + 1110b = 0|1111b since ¬0 = 1 we have a borrow

while for 2 - 1 one has

2 - 1 = 2 + (-1) = 0010b + 1111b = 1|0001b since ¬1 = 0 we have no borrow

So in your example, there is a borrow, and thus CF is set, exactly because 0001b + 1110b produces no carry.

For a more formal proof of the correctness of the inverted carry out flag, once can use induction on the number of bits.

*Base case*  

*n = 1*

This can be proven with a truth table

```
a  b   a-b (a+¬b+1)  ¬carryOut  Expected borrow
0  0   0+1+1 = 1|0       0           0
0  1   0+0+1 = 0|1       1           1
1  0   1+1+1 = 1|1       0           0
1  1   1+0+1 = 1|0       0           0

```

*Induction case* 

When subtracting two n-bit numbers A = an an-1 ... a0 and B = bn bn-1 ... b0 we can have A > B, A < B or A = B.  

In the first case there is a *k* such that ak < bk and aj = bj for all *j* > *k* up to *n*.  

The condition ak < bk implies ak = 0 and bk = 1.  

Thus A and ¬B are equal down to the *k-1*-th bit so only this many bits influence the carry out.  

But by the induction hypothesis, the carry out is correct, so it is correct in this case too. 

The same process goes for the case A > B.

The case A = B is easier as the whole operation is reduced to A + ¬A + 1 which reduces to 111...1 + 1 that always produces a carry out and thus no borrow.
There is **borrow out** indeed. `CF` is set by the `cmp` instruction if there is *borrow out*.

Your instruction `cmp` performs the following subtraction:

```
  1 = 0001
- 2 = 0010
--------------
      1111

```

For the the first rightmost column there is nothing special: `1 - 0 = 1`.

For the second rightmost column there is a *borrow* (i.e.: a `1` is "borrowed" from one of the next columns). The borrow is needed because `0 < 1`. The result of this column is then `10 - 1 = 1`. 

Note however, that for the first operand there is not a single column on the left with a `1` to borrow from, therefore a *borrow out* occurs: a borrow is coming out for the most significant bit, which actually sets `CF`.

Your subtraction operation would actually be like setting `CF` (i.e.: "borrowing" or "getting" a `1` for the MSB of the first operand) and performing the following subtraction:

```
  10001
-  0010
-------
   1111

```

Note that the first operand has a `1` on the left side of its true MSB. This `1` was not there before and it represents the *borrow*.