From what I've read, seems like there are 9 different flags. Is it possible to read/change them directly? I know I can know for example if the zero flag is set after doing a cmp/jmp instruction, but I'm asking if it's possible to do something like
mov eax, flags
or something.
Also, for writing, is it possible to set them by hand?
For reading and writing the sign, zero, auxiliary carry, parity, and carry flags, you can use LAHF to load the lower 8 bits (those 5 flags plus 3 indeterminate bits) into the AH register, and you can use SAHF to store those values from AH back into the flags register.
Only certain flags are copied across with these instructions. The whole FLAGS / EFLAGS / RFLAGS register can be saved or restored on the stack: PUSHF / POPF Push/pop 16-bit FLAGS onto/from the stack.
The flag register is one of the special purpose register. The flag bits are changed to 0 or 1 depending upon the value of result after arithmetic or logical operations. 8086 has 16-bit flag register, and there are 9 valid flag bits. The format of flag register is like below.
64-bit x86 adds 8 more general-purpose registers, named R8, R9, R10 and so on up to R15. R8–R15 are the new 64-bit registers. R8D–R15D are the lowermost 32 bits of each register. R8W–R15W are the lowermost 16 bits of each register.
Some flags can be set or cleared directly with specific instructions:
For reading and writing the sign, zero, auxiliary carry, parity, and carry flags, you can use LAHF to load the lower 8 bits (those 5 flags plus 3 indeterminate bits) into the AH register, and you can use SAHF to store those values from AH back into the flags register.
You can also use the PUSHF instruction to push the flags onto the stack, read and modify them on the stack, and then use the POPF1 instruction to store them back into the flags register.
Note that you cannot set the VM and RF flags with POPF -- they retain their previous values. Similarly, you can only change the I/O privilege level when executing at privilege level 0, and the interrupt flag can only be changed when executing at a privilege level at least as privileged as the I/O privilege level.
Footnote 1:
Note that popf
is quite slow on modern CPUs; see Agner Fog's optimization guide and instruction tables. It's microcoded because in kernel mode it's able to change IF and AC, and IO privilege level. We suffer the penalty regardless of mode on current CPUs because the decoders aren't mode-sensitive.
If possible, use lahf/sahf instead of pushf/popf for performance, or save a single flag you care about like setc al
then later add al, 255
to set CF = (AL!=0)
. Or setnc al
/ sub al, 1
or whatever. Sequences to set or clear SF or OF based on a 0 or 1 register are also straightforward, with/without inverting the flag.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With