1

I know that when performing an 8-bit binary addition, the auxiliary flag is set to 1 if there is a carry from 3rd bit to 4th bit; but what about the addition of 2 16-bit numbers? i can't see any clear answer on the web.

I'm studying intel 8086 microprocessor...

For example, when i add these 2 numbers, 0x30a2 and 0xf1ac

0011 0000 1010 0010 + 1111 0001 1010 1100

CF=1
ZF=0
PF=1
SF=0
OF=1
AF=? 

I'm not sure where to check it

Xsmael
  • 3,624
  • 7
  • 44
  • 60
  • which cpu? not all behave the same usually the 3rd to 4th bit carry flag is for BCD support. – BevynQ Apr 08 '16 at 06:00
  • 3
    Th _AF_ flag still only applies to carry out of the lowest nibble (lowest 4 bits). In your example the lowest nibble is 0010+1100 . Clearly there is no carry out of bit 3 so _AF_ =0. – Michael Petch Apr 08 '16 at 13:23
  • @MichaelPetch thanks for your simple and straightforward answer, but check Weather Vane's answer it's in contradiction with what you say, accordin to him, the nibble is a byte(8bits) then i should check from 1010 0010 + 1010 1100. – Xsmael Apr 08 '16 at 14:15
  • 2
    Weather vanes answer says "The high or low nibble *refer*s to the **LOW** order **BYTE** of a 16-bit value." . So the nibble being referred to is in the least significant byte. Nibbles are always 4 bits (not 8 bits). It basically says that no matter how many bits your register has (also applies to 32-bit) the AF flag will always be set based on carry out of the lowest nibble in the low order (least significant byte) – Michael Petch Apr 08 '16 at 14:29
  • 1
    What is it you are really trying to accomplish? Are you trying to do BCD arithmetic? If not, the AF flag is not very useful. – Michael Petch Apr 08 '16 at 14:47
  • 1
    I would help everyone's understanding if you stopped referring to __bit 3__ as the __3rd bit__ and to __bit 4__ as the __4th bit__. The only good wording is: __bit 3 is 4th bit__ and __bit 4 is 5th bit__. – Fifoernik Apr 08 '16 at 16:36
  • `bit 3 is 4th bit` is overkill. I use `bit 3` (not the `3rd bit`). `Bit 3` for anyone who read the Intel references would know that `bit` numbers are 0 based starting from the least significant bit. Are you suggesting that the Intel References are also wrong? You do not see Intel say `bit 3 is 4th bit`? – Michael Petch Apr 08 '16 at 17:54
  • @MichaelPetch Intel need not say such things. It's the normal implication of the zero based numbering and it expresses the use correct use of cardinals and ordinals. Bit 0 is the first bit, ... , bit 3 is the fourth bit. You used it fine ("no carry out of bit3 so _AF_=0"), so why take it personally? My comment addresses the OP who speaks about "a carry from 3rd bit to 4th bit". – Fifoernik Apr 09 '16 at 13:53

3 Answers3

4

From "Programming the 8086/8088" by James W. Coffron:

AF auxiliary carry flag. If this flag is set, there has been a carry of the low nibble to the high nibble or a borrow from the high nibble to the low. The high or low nibble refers to the low order byte of a 16-bit value.

In my day we would write a short piece of code to observe the processor behaviour. You could check it out by adding or subtracting two 16-bit numbers, followed by a pushf and pop ax to examine the status flags at your pleasure.

EDIT.

(Another way to get the flags is with LAHF which loads 5 bits of AH with flags, AF going to bit 4.)

So AF represents the carry out from bit 3 to bit 4, whatever the size of the operands.

Note that there are no branch instructions dependent on AF. It is used internally by the DAA instruction to do a decimal adjustment immediately after an ADD instruction, typically with AL.

Weather Vane
  • 33,872
  • 7
  • 36
  • 56
  • But Michael Petch said "The AF flag still only applies to carry out of the lowest nibble (lowest 4 bits). In your example the lowest nibble is 0010+1100 . Clearly there is no carry out of bit 3 so AF =0." – Xsmael Apr 08 '16 at 14:27
  • 1
    @Xsmael a nibble is four bits. The quote above says that for 16-bit arithmetic, the nibbles in question are in the low order byte. So in answer to your question, the same thing happens for a 16-bit operand as an 8-bit operand. In other words, `AF` represents the carry-out from bit 3 to bit 4 (addition), or the borrow from bit 4 to bit 3 (subtraction). – Weather Vane Apr 08 '16 at 15:17
2

On the 8086 the adjust flag (bit 4) was set when there was a carry from the 3rd to 4th bit or a borrow from the 4th to 3rd bit this is for BCD operation support.

BCD operationson the 8086 are 8 bit only and the BCD adjust operations only operate on the AL register.

For a 16 bit operation I would expect it to behave the same as if it was an 8 bit operation.

The carry flag (bit 0) is based on whether the operation is 8 or 16bit. If it is 16 bit then it will carry if the result > 65535. If it is 8 bit then it will carry if the result > 255.

BevynQ
  • 8,089
  • 4
  • 25
  • 37
0

the "posititon" of the carry flag depends on the operators instruction, it will always be the highest bit

e.g. add ax,bx : since the operants are 16 bit, the carry will represent the carry of the addition of the 16th bits even if you add ax ( with a value of 3) and 9 (these values will be treated as 0000000000000011 and 0000000000000101)

Tommylee2k
  • 2,683
  • 1
  • 9
  • 22
  • i don't know if you understood my question, i'm talking about auxiliary flag, not carry flag – Xsmael Apr 08 '16 at 07:58