Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 679
user3827001 Avatar asked Apr 22 '16 16:04

user3827001


1 Answers

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)

like image 198
user1354557 Avatar answered Nov 15 '22 09:11

user1354557