简体   繁体   English

如何让 GCC 知道 eflags 寄存器中的方向标志 DF 在 inline asm 中发生变化?

[英]How to let GCC know the Direction flag DF in eflags register is change in inline asm?

For example, this code:例如,这段代码:

void ppp(void);

void kkk()
{
    __asm__ volatile("std":::"cc");
    ppp();
}

Before calling ppp(), it need to execute cld due to x86 API. Referencing Default state of Direction Flag (DF) during x86 program execution在调用ppp()之前,由于x86 API需要执行cld 。在x86程序执行过程中参考方向标志(DF)的默认值state

How ever, the assemble comes out:然而,汇编出来了:

        .globl  kkk
        .type   kkk, @function
kkk:
#APP
# 5 "test.c" 1
        std
# 0 "" 2
#NO_APP
        jmp     ppp@PLT
        .size   kkk, .-kkk

According to GCC doc( https://gcc.gnu.org/onlinedocs/gcc/Extended-Asm.html ), "cc" means EFLAGS register in x86_64, why GCC not excuting cld ?根据 GCC 文档( https://gcc.gnu.org/onlinedocs/gcc/Extended-Asm.html ),“cc”表示 EFLAGS 在 x86_64 中注册,为什么 GCC 不执行cld

As you already understand the i386 ABI (and AMD64 System V ABI) require the Direction Flag (DF) to be clear to enable forward direction for instructions that rely on that flag (ie. string instructions).如您所知, i386 ABI(和 AMD64 System V ABI)要求清除方向标志 (DF),以便为依赖该标志的指令(即字符串指令)启用前向。

The FLAGS/EFLAGS/RFLAGS register on the x86/x86-64 contains many conditional flags but it also contains system flags (like the Direction Flag). x86/x86-64 上的 FLAGS/EFLAGS/RFLAGS 寄存器包含许多条件标志,但它也包含系统标志(如方向标志)。 Note: There are also conditional flags in the x87 FPU status word .注意: x87 FPU 状态字中也有条件标志。

While inline assembly has a cc clobber, at present it isn't used on all target CPUs.虽然内联汇编有一个cc破坏器,但目前它并未在所有目标 CPU 上使用。 All asm statements on GCC compilers that target x86/x86-64 processors have an implied cc clobber and that clobber has no effect .针对 x86/x86-64 处理器的 GCC 编译器上的所有asm语句都有一个隐含的cc clobber ,并且该 clobber没有任何效果

The GCC documentation is a bit vague in that what a conditional flag is for the x86/x86-64 targets. GCC 文档在 x86/x86-64 目标的条件标志是什么方面有点含糊。 I believe the intent can be inferred from this statement in the 6.47.2.4 Flag Output Operands section for the x86/x86-64 targets which says:我相信可以从6.47.2.4 Flag Output Operands section for the x86/x86-64 targets 的这个声明中推断出意图,它说:

The flag output constraints for the x86 family are of the form '=@cccond' where cond is one of the standard conditions defined in the ISA manual for jcc or setcc . x86 系列的标志 output 约束的形式为“=@cccond”,其中 cond 是ISA 手册中为 jcc 或 setcc 定义的标准条件之一

In particular the last sentence that references the conditional flags supported by Jcc or SETcc gives us a clue as to what flags are assumed to not be preserved by inline assembly on x86/x86-64: Carry Flag (CF), Overflow Flag (OF), Parity Flag (PF), Sign Flag (SF), and Zero Flag(ZF).特别是引用JccSETcc支持的条件标志的最后一句话为我们提供了关于 x86/x86-64 上的内联汇编假定不保留哪些标志的线索:进位标志 (CF)、溢出标志 (OF) 、奇偶校验标志 (PF)、符号标志 (SF) 和零标志 (ZF)。

Note: One flag in the FLAGS register that GCC doesn't care at all about is the Auxiliary Flag (AF) which can be used during the processing of certain instructions like AAA .注意:FLAGS 寄存器中 GCC 根本不关心的一个标志是辅助标志 (AF),它可以在处理某些指令(如AAA )期间使用。


To actually answer the Direction Flag (DF) question, GCC doesn't consider most of the flags in the FLAGS/EFLAGS/RFLAGS registers as conditional flags.要实际回答方向标志 (DF) 问题,GCC 不会将 FLAGS/EFLAGS/RFLAGS 寄存器中的大多数标志视为条件标志。 Things like Direction Flag, Trap Flag, Interrupt Flag really don't apply. Direction Flag、Trap Flag、Interrupt Flag 之类的东西真的不适用。 The Direction Flag is the odd man out since it is the one flag that's state is defined in the ABI.方向标志是一个奇怪的人,因为它是 ABI 中定义的标志 state。 It must be cleared for forward direction.它必须清除正向。

To use the STD instruction or other method to set the Direction Flag (backward processing) inside an inline assembly asm statement without restoring it would be undefined behaviour.使用STD指令或其他方法在内联汇编asm语句中设置方向标志(向后处理)而不恢复它是未定义的行为。 If you change the Direction Flag in an inline assembly asm statement you are responsible for restoring it in the same asm statement.如果在内联汇编asm语句中更改方向标志,您有责任在同一asm语句中恢复它。 It MUST be restored before control is return out of inline assembly.它必须在控制从内联汇编返回之前恢复。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM