HALICERY

free-time coding, hardware dev, articles

Top
Home 8042 Blogs About
Home IntelEssential 16/32-bit Instructions LAHF

Last modified: Wed Sep 17 20:42:56 UTC+0200 2025 © A. Tarpai


8086 LAHF/SAHF

Load 8-bit FLAGL into AH register or Store AH into FLAGL.

LAHF/SAHF can be handy: a one-byte instruction to read or set 5 flags, eg. carry. (Note that OF is not included. It was a new flag in 8086 for ALU signed-arithmetic support and located in FLAGH.)

The story

LAHF/SAHF is provided primarily for converting 8080/8085 assembly language programs to run on an the 8086/88. 8086 implemented identical flag bits in FLAGL to the 8-bit 8080/8085 FLAGS:

   7    6    5    4    3    2    1    0
+----+----+----+----+----+----+----+----+
| SF | ZF |    | AF |    | PF |    | CF |  FLAGL
+----+----+----+----+----+----+----+----+

LAHF/SAHF move the 5 flag bits between FLAGL and AH. One-byte opcodes:

   7    6    5    4    3    2    1    0
+----+----+----+----+----+----+----+----+
|    |    |  x |    |  x |    |  x |    |  AH
+----+----+----+----+----+----+----+----+
   |    |         |         |         |          9E SAHF
   |    |         |         |         |          Store AH Into Flags
   v    v         v         v         v
+----+----+----+----+----+----+----+----+
| SF | ZF |  0 | AF |  0 | PF |  1 | CF |  FLAGL
+----+----+----+----+----+----+----+----+
   |    |    |    |    |    |    |    |          9F LAHF
   |    |    |    |    |    |    |    |          Load Flags Into AH
   v    v    v    v    v    v    v    v
+----+----+----+----+----+----+----+----+
|    |    |  0 |    |  0 |    |  1 |    |  AH
+----+----+----+----+----+----+----+----+
   7    6    5    4    3    2    1    0

x: ignored bits on store


Load unused bits difference:

8086 LAHF --> SF:ZF:x:AF:x:PF:x:CF     x: undefined
IA32 LAHF --> SF:ZF:0:AF:0:PF:1:CF

Confirmed on real hardware:

xor eax, eax
sahf
lahf   ; EAX = 0000_02_00

or eax, -1
sahf
lahf   ; EAX = FFFF_D7_FF

Why the AH register?

Maybe.. notice CF at bit position 0. When adding 8-bit numbers in AL, CF is bit 8 of the correct addition result in the 511-0 range. Maybe with LAHF it was meant to be placed in the correct position of AX:

+----+
|    | FLAGL
+----+
   |
   | LAHF
   v
+----+----+
|   .| .. | AX
+----+----+
  AH   AL

ADD AL, src
LAHF
AND AH, 1

Now AX contains the correct 9-bit result!