How can I set or clear overflow flag in x86 assembly?

assembly, eflags, x86

Solution

There are many possible solutions.

For instance, `test al, al` will clear the `OF` flag without affecting register contents.

Or, if you don't want to affect the other flags, you can just directly modify the `*FLAGS` register. For example, in 32-bit, this would look like:

pushfd                   ; Push EFLAGS onto the stack
and dword [esp], ~0x800  ; Clear bit 11 (OF)
popfd                    ; Pop the modified result back into EFLAGS

Edit: Changed `or al, al` to `test al, al` per Peter Cordes' recommendation. (The effects are the same but the latter is better for performance reasons)

Problem

I want to write a simple code (or algorithm) to set/clear overflow flag. For setting OF, I know that I can use signed values. But how can I clear that?

Original source

Related problems