汇编中的子指令及其对CF和AF的影响

Sub instruction in assembly and the effect on CF and AF

似乎CF(进位标志)和AF(辅助标志)在使用sub命令时很棘手。例如(第一种情况正确且易于理解):

 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

现在以第二种情况为例

 0100 0100
 0010 1001 -
------------
 0001 1011

没有从AH借给AL。那么为什么第二种情况会导致 AF=1?

第三种情况,我们借用AH。所以在我看来,CF=0,AF=1。但是结果和我理解的不一样。为什么?

英特尔开发人员手册AF 标志提供了一些见解。它是用于 BCD 运算的 "Auxiliary Carry,"。手册是这样说的:

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.

所以在你的例子中,没有从AHAL借位,但是从[中的低4位半字节借位=13=] 到 AL 中的高 4 位半字节:

0100 0100
0010 1001 -
------------
0001 1011
     ^
     Borrow needed in low nibble because '1001' (9) is greater than '0100' (4)

除非您使用 BCD 算法,否则您不必使用 AF 标志。

CF,另一方面,如果结果的最高有效位有进位或借位,无论指令使用 8、16 还是 32 位操作数,都会被设置。

英特尔开发人员手册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.