The LAHF and SAHF instructions
Loading the flags into the AH register and storing AH in the FLAGS register.
The LAHF instruction format
| Instruction | Description |
|---|---|
| LAHF | AH := FLAGS(SF:ZF:0:AF:0:PF:1:CF). |
This instruction moves the low eight bits of the FLAGS register into the ah register. The bits that are moved are: SF (bit 7), ZF (bit 6), AF (bit 4), PF (bit 2), CF (bit 0). Bits 1, 3 and 5 of the FLAGS register are copied as 1, 0 and 0 respectively. This does not affect the state of the flags in the FLAGS register.
The SAHF instruction format
| Instruction | Description |
|---|---|
| SAHF | Load SF, ZF, AF, PF and CF from AH into the FLAGS register. |
Loads the SF, ZF, AF, PF and CF flags of the FLAGS register with the values of the corresponding bits of the ah register (bits 7, 6, 4, 2 and 0 respectively). Bits 1, 3 and 5 of the ah register are ignored.
Usage example
In this example we modify the flags through the AH register.
title .model small .stack 100h code segment public ; Start of the code segment assume cs:code main proc ; Start of the main procedure lahf ; Load the FLAGS state into the ah register xor ah, 00010100b ; Enable the PF and AF flags (invert bits 2 and 4) sahf ; Store the new value of the flags in the FLAGS register lahf ; Load the FLAGS state into the ah register xor ah, 01010101b ; Enable the CF and ZF flags and disable the rest sahf ; Store the new value of the flags in the FLAGS register lahf ; Load the FLAGS state into the ah register xor ah, 11000000b ; Enable the SF and CF flags and disable the rest sahf ; Store the new value of the flags in the FLAGS register 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
*SAHF changes the CF, PF, AF, ZF and SF flags by loading them from AH.