# A Little Practical Note and Experiment on CF, OF, and AF
[Reference 1](https://teaching.idallen.com/dat2343/10f/notes/040_overflow.txt)
[Reference 2](https://stackoverflow.com/questions/19301498/carry-flag-auxiliary-flag-and-overflow-flag-in-assembly)
[Reference 3](https://stackoverflow.com/questions/23990071/sign-carry-and-overflow-flag-assembly)
In this note we're only interested with Carry Flag (CF), Overflow Flag (OF) and Auxiliary Carry Flag (AF).
There's a lot of confusion between these flags on when should be set. From the references, I attempt to summarize the concepts:

## Most Significant Bit (MSB)
We say that the leftmost bit is the Most Significant Bit (MSB) or the sign bit.
```
MSB -> 0000 0000 0000 0000
pos -> 0...
neg -> 1...
```
We can visualize this in dosbox too, notice how SF turns to NG:

## Carry Flag (CF)
Turns on if and only if:
- MSB carry out additions (outside of `FFFF`)
- MSB borrow in subtractions (small - large)
- 0000 - 0001 = 1111
Otherwise, they're all set as 0 by default. A better explanation would be that CF is only interested with MSB related operations. Any carry or borrow that needs to shift MSB bits results in setting `CF = 1`.
What happens internally:
```
MSB carry out: 0111 + 0001 = 1000
MSB
0 111
+0 001
-------
1 000 <- overflow, sets OV too
-------
MSB borrow in: 0000 - 0001 = 1111
MSB
0 000
-0 001
convert to two's complement:
MSB
0 000
+1 111
-------
1 111
-------
```


Hence, we can say that don't **look for CF for unsigned arithmetic since they're always 0 during large - small subtractions.**
## Overflow Flag (OF)
Similar to CF, Overflow Flag (OF) is only interested when the arithmetic results in an incorrect output by looking at the MSB.
Turns of if and only if:
- sum of numbers with same sign but results in a diff one
- pos + pos = neg
- neg + neg = pos
Otherwise, **any mixed-sign addition (pos + two's complement neg and vice versa) never turns on the overflow flag.**
```
MSB
1 000
+1 000
------
0 000 MSB carries out from additions of two negatives
------
MSB
0 100
+0 100
------
1 000 MSB changes to negative after additions of two positives
------
```


The rules for two's complement detect errors by examining the sign of the result. A negative and positive added together cannot be wrong, because the sum is between the addends. Since both of the addends fit within the allowable range of numbers, and their sum is between them, it must fit as well. This is why mixed-sign addition never turns on the overflow flag.
## Auxiliary Carry Flag (AF)
Only interested with BCD operations between lower nibble (bit 3) to higher nibble (bit 4):
Turns on if and only if:
- bit 3 carries to bit 4
- bit 4 borrows into bit 3
From the tutorial involving subtraction `0FH` with `08H`, we don't care about the two's complement operation. We simply care about the initial inputs:
```
0000 1111
-0000 1000
----------
0000 0111 <- no carry/borrow here, so disregard AF
----------
0001 0111
-0000 1000
----------
0000 1000 <- borrow from bit 4 to bit 3, set AF = 1
----------
0000 1000
+0000 1000
----------
0001 0000 <- carry from bit 3 to bit 4, set AF = 1
----------
```
**We don't have to consider the two's complement process of converting neg to two's complement.** We are only interested on the direct inputs of the arithmetic operation.


