Instruction
SAHF
Stores bits 7, 6, 4, 2 and 0 of AH into SF, ZF, AF, PF and CF; 64-bit mode needs the LAHF-SAHF feature.
- Extensions
- Forms
- 1 (1 Legacy)
- Data
- Intel XED v2026.08.23
SAHF loads five flags from AH: bit 7 into SF, 6 into ZF, 4 into AF, 2 into PF and 0 into CF. Bits 5, 3 and 1 of AH are ignored, and every other flag keeps its value, OF included. It is the reverse of LAHF, which describes saving the arithmetic flags in a register and the CPUID bit that both need in 64-bit mode; this page looks at SAHF’s own use, putting values into the flags.
After an x87 compare
The x87 compares report in the condition codes C3, C2 and C0 of the
status word. After FNSTSW AX they sit in bits 6, 2 and
0 of AH, so SAHF turns them into ZF, PF and CF, and the unsigned
conditions (JA, JB, SETA …) then read the result as they would after
FCOMI. An unordered result, a NaN on either side, sets
all three, which makes equality a two-flag test. GCC 14 and Clang 23 at
-O2 with -m32 -march=i486 compiled a == b on doubles both ways:
- Clang: FUCOMPP,
fnstsw ax, SAHF, then SETNP and SETE ANDed, since ZF alone is also set by a NaN. - GCC: no SAHF;
and ah, 69keeps C3, C2 and C0 (45h), andcmp ah, 64asks for C3 alone.
Setting flags from a value
SAHF puts a computed pattern into CF, ZF, SF, PF and AF at once, and the instruction table of AMD’s Zen 5 guide lists it as one macro-op, while POPF, which can set them too, is microcoded and also loads TF, DF and, depending on privilege, other system flags. What SAHF cannot reach is OF in bit 11; LAHF’s page shows how to restore it with an ADD. Zen 5 recorded both halves of that rule: with AH = 0 all five flags cleared, and OF, set before, stayed set.
Encoding
SAHF is the single byte 9E, without operands. In 64-bit mode it raises #UD on a processor that lacks the LAHF-SAHF bit, CPUID 80000001H ECX[0]; Zen 5 has it, and the bit is part of x86-64-v2.
Forms
| Instruction | Encoding | Requires |
|---|---|---|
SAHF
SAHF | 9E | LAHF |
Latency and throughput
| Model | SAHF |
|---|---|
| Sandy Bridge | 1 / 0.5p05 |
| Haswell, Broadwell, Skylake, Skylake-SP, Ice Lake | 1 / 0.5p06 |
| Alder Lake, Sapphire Rapids | 4 / 1p06 |
| Lunar Lake | 4 / 0.67p024 |
| Bonnell | 2 / 1p01 |
| Silvermont | 1 / 0.5IEC0 IEC1 |
| Piledriver | 2 / 1EX0 EX1 |
| Zen, Zen 2 | 2 / 0.25ALU0 ALU1 ALU2 ALU3 |
| Zen 3, Zen 4 | 1 / 1ALU3 |
| Jaguar | 1 / 0.5ALU0 ALU1 |
Operation
What the forms do, in pseudocode that our tests run against results recorded on real processors. How to read.
sf = ah >> 7 & 1
zf = ah >> 6 & 1
af = ah >> 4 & 1
pf = ah >> 2 & 1
cf = ah & 1 # AH's bits 5, 3 and 1 are ignored; OF stays
# in 64-bit mode, only on a processor with CPUID.80000001H:ECX[0]
# (LAHF-SAHF); #UD otherwiseRun on AMD Zen 5 (EPYC 9645): 8 cases.
Operands
| Operand | Kind | Action |
|---|---|---|
SAHF | ||
AH | implicit | read |
RFLAGS | implicit | write |
Flags
| SF | ZF | AF | PF | CF |
|---|---|---|---|---|
| All forms | ||||
| A | A | A | A | A |
CPUID and processors
Each form belongs to an XED ISA set. A form can be used when the processor reports every CPUID bit of one of its ISA set's alternatives.
| ISA set | CPUID | Processors |
|---|---|---|
LAHF | LAHF | Every x86-64 processor in XED’s model and Zen 3 to Zen 6 |
More LAHF-SAHF instructions
Sources
- Intel XED v2026.08.23 (commit
0bcb6237345c): forms, encodings, operands, flags, exception classes, attributes, ISA sets, CPUID bits and chip model. - The CPU data of x86-64.net: Zen 3 to Zen 6, from GCC’s
-marchfeature sets and AMD’s Processor Programming References, which the page of each processor cites. - LLVM 23.1.2's x86 scheduling models (commit
85ac56026243): the latency, reciprocal throughput and ports of each form, as llvm-mca reports them. - Intel 64 and IA-32 Architectures Software Developer's Manual, Volume 2 (325383-093, September 2026): Volume 2B, page 4-601 (SAHF)
- Intel 64 and IA-32 Architectures Software Developer's Manual, Volume 1 (253665-093, September 2026)
- AMD64 Architecture Programmer's Manual, Volume 3: General-Purpose and System Instructions (24594, rev. 3.39, October 2026)
- Software Optimization Guide for the AMD Zen5 Microarchitecture (58455, rev. 1.00, August 2024), Wayback Machine copy of AMD's ZIP
The tables are derived from Intel XED, Copyright Intel Corporation, licensed under the Apache License 2.0; x86-64.net converted and reformatted them. The timings come from LLVM's scheduling models, part of the LLVM Project, licensed under the Apache License 2.0 with LLVM Exceptions. The text is our own.