Search code examples
assemblyx86paritybcd

Uses of the AF, PF in modern asm


I've done some browsing and it seems like the PF and AF are very rarely used, and for a beginner such as myself, can basically be ignored as they rarely if ever will come up. From what I've found it seems the use-cases are:

Except for obscure cases, can these two flags be all but ignored? Or is there ever a time when I should be reading these flags and making use of them?

Examples:

# AF (CF on 4th bit)
mov $0b1000, %eax
add $0b1000, %eax # [ AF ]


# PF (num 1's is a multiple of two)
inc %eax # [ PF ]

Solution

  • Except for obscure cases ...

    Depends on what you call "obscure":

    The AF flag is used for BCD calculations which was supported by the early CPUs (in the case of x86: the 8088) mainly for being used in calculators:

    Early CPUs did not have floating-point support (in the case of x86 CPUs you could add a separate x87 FPU which was more expensive than the computer itself), and BCD arithmetic was a simple method to perform floating-point calculations if both the input (from the keyboard) and the output (to the display) was given in decimal system.

    I remember some special PASCAL dialect for physics calculations around the year 2000 using BCD arithmetic calculations for high-precision calculations.

    The PF flag could be used for some cases of hardware access:

    When sending a 7-bit data word with parity over some early RS-232 ports, you actually had to send an 8-bit word (without additional parity); the 8th bit was the parity that had to be calculated by the CPU. Using the PF flag calculating this bit is easy!

    ... in modern asm ...

    Or is there ever a time when I should be reading these flags and making use of them?

    As far as I know, the AF flag was not intended to be read at all even in early x86 CPUs:

    The flag was only used as input to BCD operations (just like the CF flag is an input to the sbb and adc instructions); however, unlike the CF flag it was not intended to "read" AF directly (using a conditional jump operation).

    So even early x86 programmers did not care about the AF flag but the flag was only used by the CPU in the "background" when BCD arithmetic was done.

    Using modern operating systems (such as Linux and Windows) only device drivers access the hardware; and most modern hardware (for example RS-232 ports) is capable of calculating the parity in hardware.

    So using the PF flag normally is also not necessary.