Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Default state of Direction Flag (DF) during x86 program execution

In disassembly, I often see that string manipulation instructions are being used without regard to the state of the direction flag (DF), like this:

or      ecx, 0FFFFFFFFh
xor     eax, eax
mov     edi, ebp
repne scasb

CLD or STD instructions are not found since function begins, neither other instructions which could affect DF flag.
So does the compiler assume the predefined state of this flag since program launch, courtesy of the loader, and being preserved unchanged while program runs?

like image 509
def Avatar asked Dec 11 '16 19:12

def


People also ask

What happens when the directional flag is reset in microprocessor?

If directional flag is reset (0), then access the string data from lower memory location towards higher memory location. Interrupt Flag (I) – This flag is for interrupts. If interrupt flag is set (1), the microprocessor will recognize interrupt requests from the peripherals.

What is interrupt flag I in microprocessor?

Interrupt Flag (I) – This flag is for interrupts. If interrupt flag is set (1), the microprocessor will recognize interrupt requests from the peripherals. If interrupt flag is reset (0), the microprocessor will not recognize any interrupt requests and will ignore them.

What are the control flags in 8086 microprocessor?

(b) Control Flags – The control flags enable or disable certain operations of the microprocessor. There are 3 control flags in 8086 microprocessor and these are: Directional Flag (D) – This flag is specifically used in string instructions.

What is the use of directional flag in C++?

Directional Flag (D) – This flag is specifically used in string instructions. If directional flag is set (1), then access the string data from higher memory location towards lower memory location. If directional flag is reset (0), then access the string data from lower memory location towards higher memory location.


1 Answers

This is specified by the ABI of the platform that you're using. The System V Intel386 ABI (chapter Registers and the Stack Frame) says that :

The direction flag must be set to the "forward" (that is, zero) direction before entry and upon exit from a function.

The same requirement is preserved in the AMD64 ABI (Dropbox link, since x86-64.org is down) (section 3.2.1 Registers and the Stack Frame) :

The direction flag DF in the %rFLAGS register must be clear (set to "forward" direction) on function entry and return.

So, yes, userland code can safely assume that DF is set to zero.

like image 131
Daniel Kamil Kozar Avatar answered Sep 19 '22 12:09

Daniel Kamil Kozar