Auxiliary flag
   HOME

TheInfoList



OR:

A half-carry flag (also known as an auxiliary flag or decimal adjust flag) is a condition flag bit in the
status register A status register, flag register, or condition code register (CCR) is a collection of status flag bits for a processor. Examples of such registers include FLAGS register in the x86 architecture, flags in the program status word (PSW) register in ...
of many CPU families, such as the
Intel 8080 The Intel 8080 (''"eighty-eighty"'') is the second 8-bit microprocessor designed and manufactured by Intel. It first appeared in April 1974 and is an extended and enhanced variant of the earlier 8008 design, although without binary compatibil ...
,
Zilog Z80 The Z80 is an 8-bit microprocessor introduced by Zilog as the startup company's first product. The Z80 was conceived by Federico Faggin in late 1974 and developed by him and his 11 employees starting in early 1975. The first working samples were ...
, the
x86 x86 (also known as 80x86 or the 8086 family) is a family of complex instruction set computer (CISC) instruction set architectures initially developed by Intel based on the Intel 8086 microprocessor and its 8088 variant. The 8086 was intr ...
, and the Atmel AVR series, among others. It indicates when a
carry Carry or carrying may refer to: People *Carry (name) Finance * Carried interest (or carry), the share of profits in an investment fund paid to the fund manager * Carry (investment), a financial term: the carry of an asset is the gain or cost of h ...
or borrow has been generated out of the least significant four bits of the accumulator register following the execution of an arithmetic instruction. It is primarily used in decimal ( BCD) arithmetic instructions.


Usage

Normally, a processor that utilizes
binary arithmetic A binary number is a number expressed in the base-2 numeral system or binary numeral system, a method of mathematical expression which uses only two symbols: typically "0" (zero) and "1" (one). The base-2 numeral system is a positional notation ...
(which includes almost all modern CPUs) will add two 8-bit byte values according to the rules of simple binary addition. For example, adding 25 and 48 produces 6D. However, for binary-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 25 and 48, the binary addition of the two values produces 6D. Since the lower nibble of this value is a non-decimal digit (D), it must be adjusted by adding 06 to produce the correct BCD result of 73, 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 39 and 48 produces 81. 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 06 to 81 to produce a corrected BCD result of 87. 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 60 must be added to the value to produce the correct BCD result. For example, adding 72 and 73 produces E5. Since the most significant digit of this sum is non-decimal (E), adding 60 to it produces a corrected BCD result of 145. (Note that the leading 1 digit is actually a
carry bit In computer processors the carry flag (usually indicated as the C flag) is a single bit in a system status register/flag register used to indicate when an arithmetic carry or borrow has been generated out of the most significant arithmetic logic ...
.) 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 06 to it; if the result contains a non-decimal high digit, the result must be further corrected by adding 60 to produce the correct final BCD value.


See also

*
Carry flag In computer processors the carry flag (usually indicated as the C flag) is a single bit in a system status register/flag register used to indicate when an arithmetic carry or borrow has been generated out of the most significant arithmetic l ...


References

{{reflist Binary arithmetic Computer arithmetic