x86 "Flag" Bits

CS 301 Lecture, Dr. Lawlor

The "EFLAGS" register on x86 stores a bunch of flags, as shown on page 73 of the Intel arch manual Volume 1.  The important flags include:
You've also got to be aware of which instructions set which flags.  For example, the "cmp", "and" (bitwise AND), "sub", and "add" instructions set all the flags; "inc" (increment by 1) and "dec" (decrement by 1) set everything but CF; while "mov" and the jump instructions don't mess with the flags.  It's easy to accidentally overwrite flags you care about, if you leave too much stuff between the time the flag is set and the time it's read!

You can actually look at most flags with the "lahf" instruction, which copies the important bits of EFLAGS into register ah--that is, bits 8-16 of eax get EFLAGS(SF:ZF:0:AF:0:PF:1:CF).  For example:
mov rax,2
add rax,-3 ; all arithmetic sets the flags
mov rax,0 ; zero high bits of rax
lahf ; load flags into bits 8-16 of rax
ret ; return the flags: SF:ZF:0:AF : 0:PF:1:CF

(Try this in NetRun now!)

There are various funky jump instructions, like "jc" (jump if CF is set), "jo" (jump if OF is set), and "js" (jump if SF is set) that check these bits.

Note there's NO way to get at the flags, or to directly call the flag-using instructions in C!  None!