Recently came across x85 Auxiliary carry. The textbook says- it set if there was a carry out from bit 3 to bit 4 of the result. Why should there be a status register to check for a carry from 3rd bit to 4th bit? Can someone explain it to me with an example?
The auxiliary carry flag (also called the adjust flag) indicates that a carry or borrow occurred during binary-coded decimal (BCD) arithmetic. In BCD, four bits are used to store the decimal digits 0-9, with the other six values (hex A-F) considered invalid. After performing arithmetic operations on BCD values, they must be adjusted to ensure the result is properly encoded. The processor can detect non-decimal (A-F) values in the four bits, which are always adjusted, but some operations will result in valid values; the processor uses the adjust flag to determine whether to correct the value.
For example, 916+816 = 1116 = 0001 00012, which is a valid BCD value -- but it's the wrong result, as 910+810 = 1710. The processor knows to adjust the value because a carry occurred from one 4-bit BCD value to the other. In processors without special instructions for BCD arithmetic, the regular binary arithmetic instructions set the auxiliary carry flag to indicate BCD adjustment is required.
I'm not familiar with x85, but it seems to be a cut-down version of x86. BCD math in x86 uses the regular binary arithmetic instructions, plus the BCD-specific instructions DAA, DAS, AAA, AAS, AAM and AAD, which perform adjustment if required and may the (normal) carry flag (for conditional jumps). x86 supports both unpacked (one BCD digit per byte) and packed (two BCD digits per byte) formats, hence why it needs so many adjust instructions.
The Intel x86 manuals have more information on x86 BCD support, though it's scattered throughout the manual rather than in one section. Wikipedia has a more extended example in the half-carry flag article.