Gcc 从内联程序集中访问结构成员?
我试图使用内联汇编将一组结构成员(Gcc 从内联程序集中访问结构成员?,gcc,inline-assembly,gnu-assembler,Gcc,Inline Assembly,Gnu Assembler,我试图使用内联汇编将一组结构成员(Particle是指向这种结构的指针)加载到一些寄存器中。以下是我的初步解决方案: asm("mov %1(%0), %%edx\n" "fld %2(%0)\n" "fld %3(%0)\n" "fld %4(%0)\n" "fld %5(%0)\n" "movups %6(%0), %%xmm1\n" "movups %7(%0), %%xmm2\n" "movups %8
Particle
是指向这种结构的指针)加载到一些寄存器中。以下是我的初步解决方案:
asm("mov %1(%0), %%edx\n"
"fld %2(%0)\n"
"fld %3(%0)\n"
"fld %4(%0)\n"
"fld %5(%0)\n"
"movups %6(%0), %%xmm1\n"
"movups %7(%0), %%xmm2\n"
"movups %8(%0), %%xmm3\n"
"movups %9(%0), %%xmm4\n"
:
: "r" (Particle),
"n" (offsetof(ptcParticle, Active)),
"n" (offsetof(ptcParticle, Size)),
"n" (offsetof(ptcParticle, Rotation)),
"n" (offsetof(ptcParticle, Time)),
"n" (offsetof(ptcParticle, TimeScale)),
"n" (offsetof(ptcParticle, Colour)),
"n" (offsetof(ptcParticle, Location)),
"n" (offsetof(ptcParticle, Velocity)),
"n" (offsetof(ptcParticle, Accel))
: "%edx", "%st", "%st(1)", "%st(2)", "%st(3)", "%xmm1", "%xmm2",
"%xmm3", "%xmm4"
);
但它不起作用,因为GCC将这些偏移量输出为立即数文本,如下所示:
mov $0(%eax), %edx
fld $44(%eax)
fld $40(%eax)
fld $8(%eax)
fld $4(%eax)
movups $12(%eax), %xmm1
movups $28(%eax), %xmm2
movups $48(%eax), %xmm3
movups $60(%eax), %xmm4
asm("mov %c[Active](%0), %%edx\n"
"fld %c[Size](%0)\n"
"fld %c[Rotation](%0)\n"
"fld %c[Time](%0)\n"
"fld %c[TimeScale](%0)\n"
"movups %c[Colour](%0), %%xmm1\n"
"movups %c[Location](%0), %%xmm2\n"
"movups %c[Velocity](%0), %%xmm3\n"
"movups %c[Accel](%0), %%xmm4\n"
:
: "r" (Particle),
DECLARE_STRUCT_OFFSET(ptcParticle, Active),
DECLARE_STRUCT_OFFSET(ptcParticle, Size),
DECLARE_STRUCT_OFFSET(ptcParticle, Rotation),
DECLARE_STRUCT_OFFSET(ptcParticle, Time),
DECLARE_STRUCT_OFFSET(ptcParticle, TimeScale),
DECLARE_STRUCT_OFFSET(ptcParticle, Colour),
DECLARE_STRUCT_OFFSET(ptcParticle, Location),
DECLARE_STRUCT_OFFSET(ptcParticle, Velocity),
DECLARE_STRUCT_OFFSET(ptcParticle, Accel)
: "%edx", "%st", "%st(1)", "%st(2)", "%st(3)", "%xmm1", "%xmm2",
"%xmm3", "%xmm4"
);
因此,gas
在表达式之后将(%eax)
视为垃圾:
Error: junk `(%eax)' after expression
如果我能去掉输出中的美元符号,这就行了。知道如何访问结构成员吗?好的,我已经弄明白了-需要
%c
操作符。我已经编写了以下帮助宏:
#define DECLARE_STRUCT_OFFSET(Type, Member) \
[Member] "i" (offsetof(Type, Member))
然后像这样使用它:
mov $0(%eax), %edx
fld $44(%eax)
fld $40(%eax)
fld $8(%eax)
fld $4(%eax)
movups $12(%eax), %xmm1
movups $28(%eax), %xmm2
movups $48(%eax), %xmm3
movups $60(%eax), %xmm4
asm("mov %c[Active](%0), %%edx\n"
"fld %c[Size](%0)\n"
"fld %c[Rotation](%0)\n"
"fld %c[Time](%0)\n"
"fld %c[TimeScale](%0)\n"
"movups %c[Colour](%0), %%xmm1\n"
"movups %c[Location](%0), %%xmm2\n"
"movups %c[Velocity](%0), %%xmm3\n"
"movups %c[Accel](%0), %%xmm4\n"
:
: "r" (Particle),
DECLARE_STRUCT_OFFSET(ptcParticle, Active),
DECLARE_STRUCT_OFFSET(ptcParticle, Size),
DECLARE_STRUCT_OFFSET(ptcParticle, Rotation),
DECLARE_STRUCT_OFFSET(ptcParticle, Time),
DECLARE_STRUCT_OFFSET(ptcParticle, TimeScale),
DECLARE_STRUCT_OFFSET(ptcParticle, Colour),
DECLARE_STRUCT_OFFSET(ptcParticle, Location),
DECLARE_STRUCT_OFFSET(ptcParticle, Velocity),
DECLARE_STRUCT_OFFSET(ptcParticle, Accel)
: "%edx", "%st", "%st(1)", "%st(2)", "%st(3)", "%xmm1", "%xmm2",
"%xmm3", "%xmm4"
);
生成的程序集现在是正确的,似乎一切都正常。在中讨论了
structs
和$
的相同问题。这可能是问题的重复,但我不确定,因为另一个问题是在扩展ASM的上下文中。也看到