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

user3827001 picture user3827001 · Apr 22, 2016 · Viewed 7.9k times · Source

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?

Answer

user1354557 picture user1354557 · Apr 22, 2016

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)