Half-carry flag
Ahalf-carry flag(also known as anauxiliary flag) is a condition flag bit in thestatus registerof manyCPUfamilies, such as theIntel 8080,Zilog Z80,thex86,[1]and theAtmel AVRseries, among others. It indicates when acarryor borrow has been generated out of the least significant four bits of theaccumulatorregister following the execution of anarithmeticinstruction. It is primarily used in decimal (BCD) arithmetic instructions.
Usage
[edit]Normally, a processor that usesbinary arithmetic(which includes almost all modern CPUs) will add two 8-bit byte values according to the rules of simple binary addition. For example, adding 2516and 4816produces 6D16.However, forbinary-coded decimal(BCD) values, where each 4-bit nibble represents a decimal digit, addition is more complicated. For example, adding the decimal value 25 and 48, which are encoded as the BCD values 2516and 4816,the binary addition of the two values produces 6D16.Since the lower nibble of this value is a non-decimal digit (D), it must be adjusted by adding 0616to produce the correct BCD result of 7316,which represents the decimal value 73.
0010 0101 25 + 0100 1000 48 ----------- 0110 1101 6D,intermediate result + 0110 06,adjustment ----------- 0111 0011 73,adjusted result
Likewise, adding the BCD values 3916and 4816produces 8116.This result does not have a non-decimal low nibble, but it does cause a carry out of the least significant digit (lower four bits) into the most significant digit (upper four bits). This is indicated by the CPU setting the half-carry flag. This value must also be corrected, by adding 0616to 8116to produce a corrected BCD result of 8716.
0011 1001 39 + 0100 1000 48 ----------- 1000 0001 81,intermediate result + 0110 06,adjustment ----------- 1000 0111 87,adjusted result
Finally, if an addition results in a non-decimal high digit, then 6016must be added to the value to produce the correct BCD result. For example, adding 7216and 7316produces E516.Since the most significant digit of this sum is non-decimal (E), adding 6016to it produces a corrected BCD result of 14516.(Note that the leading 1 digit is actually acarry bit.)
0111 0010 72 + 0111 0011 73 ----------- 1110 0101 E5,intermediate result + 0110 60,adjustment ----------- 1 0100 0101 145,adjusted result
Summarizing, if the result of a binary addition contains a non-decimal low digit or causes the half-carry flag to be set, the result must be corrected by adding 0616to it; if the result contains a non-decimal high digit, the result must be further corrected by adding 6016to produce the correct final BCD value.
The Auxiliary Carry Flag in x86
[edit]
|
TheAuxiliary Carry Flag(AF) is aCPU flagin theFLAGS registerof allx86-compatibleCPUs,[2]and the preceding8080-family. It has occasionally been called the Adjust Flag by Intel.[3]The flag bit is located atposition4 in the CPU flag register. It indicates when anarithmeticcarryor borrow has been generated out of the four least significant bits, or lower nibble. It is primarily used to supportbinary-coded decimal(BCD) arithmetic.
The Auxiliary Carry flag is set (to 1) if during an "add"operation there is a carry from the low nibble (lowest four bits) to the high nibble (upper four bits), or a borrow from the high nibble to the low nibble, in the low-order 8-bit portion, during a subtraction. Otherwise, if no such carry or borrow occurs, the flag is cleared or" reset "(set to 0). [4]
See also
[edit]References
[edit]- ^"Intel Architecture Software Developer's Manual, Volume 2: Instruction Set Reference Manual"(PDF).Retrieved2013-05-29.
- ^Intel 64 and IA-32 Architectures Software Developer’s Manual, Vol. 1.Dec 2022. p. 3-16.
- ^Pentium Pro Family Developer's Manual: Volume 2(PDF).January 1996. p. 3-11.
- ^"The 8086 Family User Manual"(PDF).Intel. Archived fromthe original(PDF)on 27 September 2020.Retrieved2 July2020.