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?
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.
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.
(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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With