I want to write a simple code (or algorithm) to set/clear overflow flag.can you help me?
Asked
Active
Viewed 2,326 times
2 Answers
2
Many instructions clear OF
as a side effect, for example test
. As such you can write something like test eax, eax
and that will clear OF
, although it will affect other flags. You didn't specify whether that's allowed.
To set OF
you can perform some operation that will set it. For example, add 1 to the biggest positive number: mov al, 0x7f; add al, 1
Alternatively, you can make a copy of the flags on the stack using pushf
, modify it to your liking and then get it back using popf
.

Jester
- 56,577
- 4
- 81
- 125
-
You should consider deleting your answer here and moving it to the duplicate. Or flagging this question and asking a moderator to merge all of its answers into the duplicate. – Cody Gray - on strike Dec 09 '16 at 13:29
1
To set the Overflow flag(OF) - Bit 11 of EFLAGS use (in MASM syntax)
pushfd ; mov EFLAGS to stack
bts dword ptr [esp], 11 ; set OF flag
popfd ; write it back to status register
and, respectively, use the following to reset the OF flag:
pushfd ; mov EFLAGS to stack
btr dword ptr [esp], 11 ; reset OF flag
popfd ; write it back to status register

zx485
- 28,498
- 28
- 50
- 59
-
3`or` or `and` with a memory destination is fewer uops than `bts [mem], imm` on Intel CPUs. Even with an immediate bit count, `bts` and friends with a memory destination aren't as efficient, so generally avoid them with memory operands other than when you need an atomic (`lock`) test-and-set. – Peter Cordes Apr 27 '16 at 14:35
-
`pushf` pushes that `FLAGS` register. To push `EFLAGS`, better use `pushfd`. – Nubok Mar 26 '18 at 14:04
-
1["The PUSHF (push flags) and PUSHFD (push flags double) mnemonics reference the same opcode...."](http://www.felixcloutier.com/x86/PUSHF:PUSHFD:PUSHFQ.html). I changed the answer anyway to improve the emphasis. – zx485 Mar 27 '18 at 00:30
-
2@Nubok: even if it did only push FLAGS, `OF` is in the low 16 bits of EFLAGS, i.e. FLAGS, so this code works. `btr` and `popf` are both quite slow, and `btr dword ptr[]` isn't guaranteed to load/store a whole dword anyway, so you could get a store-forwarding stall either way. `or` or `and mem, imm32` to set or clear might take an extra 2 or 3 code bytes, though. But if you want min code size to set `OF` without clobbering regs, `push eax` / `xor eax,eax` / `sub al, -128` (2 bytes) / `pop eax` should do it in 6B total, vs. 7B for this answer which also avoids clobbering any other flags. – Peter Cordes Mar 27 '18 at 02:44
-
@zx485 Based on your source you it indeed seems to be a common convention that these instructions are treated as synonyms to each other. I had the other convention that is mentioned there in mind: "The PUSHF instruction is intended for use when the operand-size attribute is 16 and the PUSHFD instruction for when the operand-size attribute is 32. Some assemblers may force the operand size to 16 when PUSHF is used and to 32 when PUSHFD is used." – Nubok Mar 27 '18 at 11:01