The Intel 80386, part 3: Flags and condition codes
The flags register contains a bunch of stuff, but here are the flags easily accessible in the debugger:
|DF||up/dn||Direction||Must be up at function boundaries|
|IF||ei/di||Interrupts||Set if interrupts are enabled|
|AF||na/ac||Auxiliary carry||Not used by C code|
|PF||pe/po||Parity||Not used by C code|
We’ll learn about the direction flag when we get to string operations. The important detail for now is that the direction flag must be clear (up) at function boundaries.
Instructions for manipulating the interrupt flag are privileged, so you won’t see user-mode code messing with it. I wouldn’t normally have mentioned it, but the Windows disassembler displays the state of the interrupt flags in the register output, so I included it here just so you can see what it means (and then promptly forget about it).
The auxiliary carry is used to indicate whether a carry occurred between bits 3 and 4. It is used by the binary coded decimal instructions.
The parity is used to indicate whether the number of set bits in the least significant 8 bits of the result is odd or even.
The Clear/Set column denotes how the Windows disassembler represents flags in the register output:
eax=00000000 ebx=00000000 ecx=9f490000 edx=00000000 esi=7f19e000 edi=00000000 eip=77a93dad esp=0048f844 ebp=0048f870 iopl=0cs=0023 ss=002b ds=002b es=002b fs=0053 gs=002b
The efl represents the value of the 32-bit flags register, and selected bits are parsed out and rendered as mnemonics on the line above.
Various combinations of conditions can be expressed with condition codes. Note that many conditions have multiple mnemonics. The first one listed is the one the disassembler uses.
|Above||Unsigned greater than|
|Not below or equal|
|Above or equal||Unsigned greater than or equal|
|No carry||No unsigned overflow|
|Below||Unsigned less than|
|Not above or equal|
|Carry set||Unsigned overflow|
|Below or equal||Unsigned less than or equal|
|Greater||Signed greater than|
|Not less than or equal|
|Greater than or equal||Signed greater than or equal|
|Not less than|
|Less than||Signed less than|
|Not greater than or equal|
|Less than or equal||Signed less than or equal|
|Not greater than|
|No sign||Positive or zero|
|No overflow||No signed overflow|
|Parity||Even number of bits set|
|No parity||Odd number of bits set|
The overflow and parity conditions are not normally used by C code. Note also that many flags are not testable via condition codes. (Poor auxiliary carry flag. Nobody loves you.)
There are a few instructions for directly manipulating selected flags:
STC ; set carry CLC ; clear carry CMC ; complement (toggle) carry STD ; set direction (go down) CLD ; clear direction (go up)
Controlling the interrupt flag is a privileged instruction, so you won’t see it in user-mode code. There are no instructions for directly manipulating the other flags, but you can manipulate them indirectly by performing an arithmetic operation with a known effect on flags. For example, you can force ZF to be set by performing a calculation whose result is known to be zero, such as
XOR EAX, EAX.
Okay, that was extremely boring, but it had to be done. Next time, we’ll start doing arithmetic.