User Guide
General-Purpose Programming 63
24592—Rev. 3.15—November 2009 AMD64 Technology
For details on stack operations, see “Control Transfers” on page 76.
Set and Clear Flags
• CLC—Clear Carry Flag
• CMC—Complement Carry Flag
• STC—Set Carry Flag
• CLD—Clear Direction Flag
• STD—Set Direction Flag
• CLI—Clear Interrupt Flag
• STI—Set Interrupt Flag
These instructions change the value of a flag in the rFLAGS register that is visible to application
software. Each instruction affects only one specific flag.
The CLC, CMC, and STC instructions change the carry flag (CF). CLC clears the flag to 0, STC sets
the flag to 1, and CMC inverts the flag. These instructions are useful prior to executing instructions
whose behavior depends on the CF flag—for example, shift and rotate instructions.
The CLD and STD instructions change the direction flag (DF) and influence the function of string
instructions (CMPSx, SCASx, MOVSx, LODSx, STOSx, INSx, OUTSx). CLD clears the flag to 0,
and STD sets the flag to 1. A cleared DF flag indicates the forward direction in string sequences, and a
set DF flag indicates the backward direction. Thus, in string instructions, the rSI and/or rDI register
values are auto-incremented when DF = 0 and auto-decremented when DF = 1.
Two other instructions, CLI and STI, clear and set the interrupt flag (IF). CLI clears the flag, causing
the processor to ignore external maskable interrupts. STI sets the flag, allowing the processor to
recognize maskable external interrupts. These instructions are used primarily by system software—
especially, interrupt handlers—and are described in “Exceptions and Interrupts” in Volume 2.
Load and Store Flags
• LAHF—Load Status Flags into AH Register
• SAHF—Store AH into Flags
LAHF loads the lowest byte of the RFLAGS register into the AH register. This byte contains the carry
flag (CF), parity flag (PF), auxiliary flag (AF), zero flag (ZF), and sign flag (SF). SAHF stores the AH
register into the lowest byte of the RFLAGS register.
3.3.13 Input/Output
The I/O instructions perform reads and writes of bytes, words, and doublewords from and to the I/O
address space. This address space can be used to access and manage external devices, and is
independent of the main-memory address space. By contrast, memory-mapped I/O uses the main-
memory address space and is accessed using the MOV instructions rather than the I/O instructions.