GCC扩展了asm,struct element offset encoding

Ser*_* L. 6 c gcc inline-assembly i386

我试图在GCC样式扩展asm(x86-64目标)中编写我的一小段代码,并且在编码结构偏移时遇到问题.

我有一个struct s成员size_t a[],一个指向这样的结构和索引的指针,这两个结构都是在asm块中生成的.

现在我需要在asm中解决这个问题

asm (
    "mov %[displ](%[s], %[index], 8), %%rbx"
    : [s] "+r" (s)
    , [index] "+r" (i)
    : "memory", "cc", "rax", "rbx"
);
Run Code Online (Sandbox Code Playgroud)

如何编码displ到asm块?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"
);
Run Code Online (Sandbox Code Playgroud)

Fra*_*kH. 7

实际上,使用操作数修饰符可能的%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;
}
Run Code Online (Sandbox Code Playgroud)

谢谢,谢谢到期 - 发现在这里.还有一个gcc邮件列表帖子也引用了这个; 关键字有"输出替换".
stackoverflow发布在CCC内联汇编代码中%c的含义是什么?%c特别是也有一个解释.