Half-carry Flag
   HOME

TheInfoList



OR:

A half-carry flag (also known as an auxiliary 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 (computing), flag bits for a Central processing unit, processor. Examples of such registers include FLAGS register (computing), FLAGS register in the ...
of many CPU families, such as the
Intel 8080 The Intel 8080 is Intel's second 8-bit computing, 8-bit microprocessor. Introduced in April 1974, the 8080 was an enhanced successor to the earlier Intel 8008 microprocessor, although without binary compatibility.'' Electronic News'' was a week ...
,
Zilog Z80 The Zilog Z80 is an 8-bit computing, 8-bit microprocessor designed by Zilog that played an important role in the evolution of early personal computing. Launched in 1976, it was designed to be Backward compatibility, software-compatible with the ...
, 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 8086 microprocessor and its 8-bit-external-bus variant, the 8088. Th ...
, and the
Atmel AVR AVR is a family of microcontrollers developed since 1996 by Atmel, acquired by Microchip Technology in 2016. They are 8-bit RISC single-chip microcontrollers based on a modified Harvard architecture. AVR was one of the first microcontroller ...
series, among others. It indicates when a carry or borrow has been generated out of the least significant four bits of the accumulator register following the execution of an
arithmetic Arithmetic is an elementary branch of mathematics that deals with numerical operations like addition, subtraction, multiplication, and division. In a wider sense, it also includes exponentiation, extraction of roots, and taking logarithms. ...
instruction. It is primarily used in decimal ( BCD) arithmetic instructions.


Usage

Normally, a processor that uses
binary arithmetic A binary number is a number expressed in the Radix, base-2 numeral system or binary numeral system, a method for representing numbers that uses only two symbols for the natural numbers: typically "0" (zero) and "1" (one). A ''binary number'' may ...
(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 In computing and electronic systems, binary-coded decimal (BCD) is a class of binary encodings of decimal numbers where each digit is represented by a fixed number of bits, usually four or eight. Sometimes, special bit patterns are used f ...
(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.) 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.


The Auxiliary Carry Flag in x86

The Auxiliary Carry Flag (AF) is a CPU flag in the
FLAGS register The FLAGS processor register, register is the status register that contains the current state of an x86 CPU. The size and meanings of the flag bits are architecture dependent. It usually reflects the result of arithmetic operations as well as infor ...
of all
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 8086 microprocessor and its 8-bit-external-bus variant, the 8088. Th ...
-compatible CPUs, and the preceding 8080-family. It has occasionally been called the Adjust Flag by Intel. The flag bit is located at
position Position often refers to: * Position (geometry), the spatial location (rather than orientation) of an entity * Position, a job or occupation Position may also refer to: Games and recreation * Position (poker), location relative to the dealer * ...
 4 in the CPU flag register. It indicates when an
arithmetic Arithmetic is an elementary branch of mathematics that deals with numerical operations like addition, subtraction, multiplication, and division. In a wider sense, it also includes exponentiation, extraction of roots, and taking logarithms. ...
carry or borrow has been generated out of the four least significant bits, or lower nibble. It is primarily used to support
binary-coded decimal In computing and electronic systems, binary-coded decimal (BCD) is a class of binary encodings of decimal numbers where each digit is represented by a fixed number of bits, usually four or eight. Sometimes, special bit patterns are used f ...
(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).


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 A status register, flag register, or condition code register (CCR) is a collection of status Flag (computing), flag bits for a ...
*
Intel BCD opcodes The Intel BCD opcodes are a set of six x86 instruction (computer science), instructions that operate with binary-coded decimal numbers. The radix used for the representation of numbers in the x86 central processing unit, processors is 2. This is c ...


References

{{reflist Binary arithmetic Computer arithmetic X86 architecture