Jump to content

Trap flag

From Wikipedia, the free encyclopedia

This is an old revision of this page, as edited by 128.61.83.190 (talk) at 12:02, 4 August 2011 (this article is still a mess and probably contains more inaccuracies). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

The Trap flag permits operation of processor in a single-step mode. Debugger programs such as DEBUG , Codeview etc. sets the trap flag so that you can step through execution a single instruction at a time to examine its effect on registers and memory.Some copy-protected programs also use this flag to prevent hackers from breaking their shield.

Trap flag, when set ,causes the processor to execute in single step mode, i.e. one instruction at a time under user control. Debuggers set the TF for single-step execution,and that's about the only place you'd expect to find it.

Single Step Interrupt

When you tell a system to single-step,it will execute one instruction and stop.You can examine the contents of registers and memory locations.If they are correct ,you can tell the system to go on and execute the next instruction.The 8086 trap flag and type-1 interrupt response make it quite easy to implement a single-step feature in a 8086-based system. If the trap flag is set,the 8086 will automatically do a type-1 interrupt after each instruction executes.When the 8086 does a type-1 interrupt , it pushes the flag register on the stack.

Set 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 you want it to be,and then popping the flag register back off the stack. The instructions to set the trap flag are-

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

Reset Trap flag

To reset the Trap flag,simply replace the OR instruction in the preceding sequence with the instruction-

AND WORD PTR[BP+0],0FEFFH

The Trap flag is reset when the 8086 does a type-1 interrupt,so the single-step mode will be disabled during the interrupt-service procedure.