Assembly instruction for setting, clearing OF & TF flags

mohammad mahed picture mohammad mahed · Nov 17, 2012 · Viewed 8.9k times · Source

Are there any assembly instructions to let us directly "set" or "clear" the "OF" and "TF" flags in Intel's 8086 16-bit Flags register ? If not, what pseudo code should we use?

Answer

John Dvorak picture John Dvorak · Nov 17, 2012

http://en.wikipedia.org/wiki/Trap_flag

The 8086 has no instruction to directly set or reset the trap flag. These operations are done by pushing the flag register on the stack, changing the trap flag bit to what the programmer wants it to be, and then popping the flag register back off the stack. The instructions to set the trap flag are:

PUSHF ; Push flags on stack
MOV BP,SP  ; Copy SP to BP for use as index
OR WORD PTR[BP+0],0100H ; Set TF flag
POPF  ; Restore flag Register

To reset the trap flag, simply replace the OR instruction in the preceding sequence with the instruction:

AND WORD PTR[BP+0],0FEFFH

To set and clear the overflow flag, you can do the same, replacing 0100H with 0800H and 0FEFFh with 0F7FFh.

Be sure to know what TF does before you set it. It's a trap.