Reputation: 24745
It seems that CF (carry flag) and AF (auxiliary flag) are tricky when using sub
command. For example (first case is correct and easy to understand):
AL=4CH, BL=29H (C>9 , 4>2)
SUB AL,BL ; AL=23H CF=0 AF=0
AL=44H, BL=29H (4<9 , 4>2)
SUB AL,BL ; AL=1BH CF=0 AF=1
AL=1CH, BL=29H (c>9 , 1<2)
SUB AL,BL ; AL=F3H CF=1 AF=0 ALSO SF=1
AL=13H, BL=29H (3<9 , 1<2)
SUB AL,BL ; AL=F3H CF=1 AF=1 ALSO SF=1
Now take the second case for more details
0100 0100
0010 1001 -
------------
0001 1011
There is no borrowing from AH to AL. So why the second case results in AF=1?
Regarding the third case, we borrow from AH. So in my opinion, CF=0 and AF=1. But the result is different from what I understand. Why?
Upvotes: 0
Views: 1707
Reputation: 4873
The Intel Developer's Manual gives some insight on the AF
flag. It is the "Auxiliary Carry," used for BCD arithmetic. The manual says about it:
AF — Auxiliary Carry flag — Set if an arithmetic operation generates a carry or a borrow out of bit 3 of the result; cleared otherwise. This flag is used in binary-coded decimal (BCD) arithmetic.
So in your example, there is no borrowing from AH
to AL
, but there is borrowing from the low 4-bit nibble in AL
to the high 4-bit nibble in AL
:
0100 0100
0010 1001 -
------------
0001 1011
^
Borrow needed in low nibble because '1001' (9) is greater than '0100' (4)
Unless you are using BCD arithmetic, you shouldn't have to use the AF
flag.
CF
, on the other hand, will be set if there is a carry or borrow from the most significant bit of the result, whether the instruction uses 8, 16, or 32 bit operands.
The Intel Developer's Manual says about CF
:
CF — Carry flag — Set if an arithmetic operation generates a carry or a borrow out of the most significant bit of the result; cleared otherwise. This flag indicates an overflow condition for unsigned-integer arithmetic. It is also used in multiple-precision arithmetic.
Upvotes: 1