x86 Assembly Language Reference Manual

Flag Instructions

Load Flags into AH Register (lahf)

lahf

Operation

SF:ZF:xx:AF:xx:PF:xx:CF -> AH

Description

Transfers the low byte of the flags word to the AH register. The bits (lsb to msb) are: sign, zero, indeterminate, auxiliary carry, indeterminate, parity, indeterminate, and carry.

Example

Transfer the flags word into the AH register:

lahf

Store AH into Flags (sahf)

sahf
Operation

AH -> SF:ZF:xx:AF:xx:PF:xx:CF

Description

Loads flags (sign, zero, indeterminate, auxiliary carry, indeterminate, parity, indeterminate, and carry) with values from the AH register.

Example

Load values from the AH register into the flags word:

sahf

Pop Stack into Flag (popf)

popf{wl}
Operation

stack -> flags register

Description

Pops the word or long from the top of the stack and stores the value in the flags register. Stores a word in FLAGS; stores a long in EFLAGS.

Example

Pops the word from the top of the stack and stores it in the flags register:

popfw

Pops the long from the top of the stack and stores it in the eflags register:

popfl

Push Flag Register Onto Stack (pushf)

pushf{wl}

Operation

flags register -> stack

Description

For a word, SP - 2 and copies FLAGS to the new top of stack pointed to by SP. For a long, SP - 4 and copies EFLAGS to the new top of stack pointed to by SS:eSP.

Example

Pushes the flags register onto the top of the stack:

pushfw

Pushes the eflags register onto the top of the stack:

pushfl

Complement Carry Flag (cmc)

cmc
Operation

not CF -> CF

Description

Reverses the setting of the carry flag; affects no other flags.

Example

Reverse the setting of the carry flag:

cmc

Clear Carry Flag (clc)

clc

Operation

0 -> CF

Description

Sets the carry flag to zero; affects no other flags.

Example

Clear the carry flag:

clc

Set Carry Flag (stc)

stc
Operation

1 -> CF

Description

Sets the carry flag to 1.

Example

Set the carry flag:

stc

Clear Interrupt Flag (cli)

cli
Operation

0 -> IF

Description

Clears the interrupt flag if the current privilege level is at least as privileged as IOPL; affects no other flags. External interrupts disabled at the end of the cli instruction or from that point on until the interrupt flag is set.

Example

Clear the interrupt flag:

cli

Set Interrupt Flag (sti)

sti
Operation

1 -> IF

Description

Sets the interrupt flag to 1.

Example

Set the interrupt flag:

sti

Clear Direction Flag (cld)

cld
Operation

0 -> DF

Description

Clears the direction flag; affects no other flags or registers. Causes all subsequent string operations to increment the index registers, (E)SI and/or (E)DI, used during the operation.

Example

Clear the direction flag:

cld

Set Direction Flag (std)

std
Operation

1 -> DF

Description

Sets the direction flag to 1, causing all subsequent string operations to decrement the index registers, (E)SI and/or (E)DI, used during the operation.

Example

Set the direction flag:

std