x86 程序执行期间方向标志 (DF) 的默认状态
Default state of Direction Flag (DF) during x86 program execution
在反汇编中,我经常看到不考虑方向标志 (DF) 的状态而使用字符串操作指令,如下所示:
or ecx, 0FFFFFFFFh
xor eax, eax
mov edi, ebp
repne scasb
CLD
或 STD
指令在函数开始后未找到,其他可能影响 DF 标志的指令也未找到。
那么编译器是否假定自程序启动以来此标志的预定义状态,由加载程序提供,并在程序运行时保持不变?
编译器运行时,包括为操作系统编译的代码,将期望标志处于特定状态。其他代码也可以使用这个假设,而不必经常清除标志。
这是由您正在使用的平台的 ABI 指定的。 System V Intel386 ABI(第 章寄存器和堆栈框架)说:
The direction flag must be set to the "forward" (that is, zero)
direction before entry and upon exit from a function.
AMD64 ABI (Dropbox link, since x86-64.org is down)(3.2.1 节中保留了相同的要求
寄存器和堆栈帧) :
The direction flag DF
in the %rFLAGS
register must be clear (set to
"forward" direction) on function entry and return.
所以,是的,用户空间代码可以安全地假设 DF
设置为零。
在反汇编中,我经常看到不考虑方向标志 (DF) 的状态而使用字符串操作指令,如下所示:
or ecx, 0FFFFFFFFh
xor eax, eax
mov edi, ebp
repne scasb
CLD
或 STD
指令在函数开始后未找到,其他可能影响 DF 标志的指令也未找到。
那么编译器是否假定自程序启动以来此标志的预定义状态,由加载程序提供,并在程序运行时保持不变?
编译器运行时,包括为操作系统编译的代码,将期望标志处于特定状态。其他代码也可以使用这个假设,而不必经常清除标志。
这是由您正在使用的平台的 ABI 指定的。 System V Intel386 ABI(第 章寄存器和堆栈框架)说:
The direction flag must be set to the "forward" (that is, zero) direction before entry and upon exit from a function.
AMD64 ABI (Dropbox link, since x86-64.org is down)(3.2.1 节中保留了相同的要求 寄存器和堆栈帧) :
The direction flag
DF
in the%rFLAGS
register must be clear (set to "forward" direction) on function entry and return.
所以,是的,用户空间代码可以安全地假设 DF
设置为零。