The PUSHF and POPF instructions

Pushing the FLAGS register onto the stack and popping it off the stack.

The PUSHF instruction format

InstructionDescription
PUSHFPush the lower 16 bits of the FLAGS register onto the stack.

Decrements the stack pointer by 2 and places the lower 16 bits of the FLAGS register (that is, the flags) on the stack.

The POPF instruction format

InstructionDescription
POPFLoad the value from the stack into the FLAGS register.

Pops a word from the top of the stack and stores it in the lower 16 bits of the FLAGS register. This may affect all the flags.

Usage example

In this example we first save the flags on the stack (pushf), then zero AX (which changes the ZF and PF flags), and then restore the original flags from the stack (popf).

title Author: Evgeny Sergeevich Dovgopolov

.model small
.stack 100h

code segment public        ; Start of the code segment
    assume cs:code

    main proc              ; Start of the main procedure
        mov     ax, 1      ; Copy the value 1 into the ax register
        pushf              ; Save the value of the FLAGS register at the top of the stack

        xor     ax, ax     ; Zero the value of the ax register
        popf               ; Pop the value from the stack back into FLAGS

        mov     ax, 4C00h  ; Copy the process termination function number into ah and the value into al
        int     21h        ; Call the interrupt
    main endp              ; End of the main procedure

code ends                  ; End of the code segment
end main                   ; End of file. The main procedure is the entry point

Example taken from the textbook "HANDYFAR - MASM (Evgeny Sergeevich Dovgopolov)".

Processor status flags

CF
Yes*
PF
Yes*
AF
Yes*
ZF
Yes*
SF
Yes*
TF
Yes*
IF
Yes*
DF
Yes*
OF
Yes*

*POPF changes all the flags by loading them from the stack.