我正在尝试用 GCC 风格的扩展 asm(x86-64 目标)编写一小段代码,但在编码结构偏移量时遇到了问题。
我有一个struct s
,它有一个成员size_t a[]
,一个指向这样一个结构的指针和一个索引,它们都是在asm block 中生成的。
现在我需要在 asm 中处理该元素
asm (
"mov %[displ](%[s], %[index], 8), %%rbx"
: [s] "+r" (s)
, [index] "+r" (i)
: "memory", "cc", "rax", "rbx"
);
如何将 displ
编码到 asm block 中?将 offsetof(struct s, a)
作为立即前缀传递给 $
并生成无效程序集。
asm (
"mov %[displ](%[s], %[index], 8), %%rbx"
: [s] "+r" (s)
, [index] "+r" (i)
: [displ] "i" (offsetof(struct s, a))
: "memory", "cc", "rax", "rbx"
);
最佳答案
实际上 是可能的,使用 %c...
操作数修饰符:
#include <stddef.h>
#include <stdint.h>
struct s
{
int a, b;
};
int foo (struct s *s, int i)
{
int r;
asm (
"movl %c[displ](%[s],%[index],8), %[r]\n\t"
: [r] "=r" (r)
: [s] "r" (s) , [index] "r" ((uintptr_t)i),
[displ] "e" (offsetof(struct s, b))
:
);
return r;
}
感谢该感谢的地方 - 发现 here .有 a gcc mailing list posting也引用这个;那里的关键字是“输出替换”。
stackoverflow 发帖 What does %c mean in GCC inline assembly code?也有关于 %c
特别的解释。
关于c - GCC 扩展 asm,struct 元素偏移编码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13254512/