GCC扩展的asm,结构元素偏移编码 [英] GCC extended asm, struct element offset encoding
问题描述
我试图写在GCC样式的扩展ASM(x86-64的目标)一小块我的code和我有编码结构失调的麻烦。
我有一个的struct
与成员为size_t一个[]
,指针这样的结构和这两个指数都在asm块中产生的。
现在我需要解决元素ASM
ASM(
MOV%[显示终端(%[S],%[指数],8),%% RBX
:[S]+ R(S)
[指数]+ R(一)
:记忆,抄送,RAX,RBX
);
我怎样才能连接code 显示终端
进入ASM块?通过 offsetof(结构S,A)
作为即时$ P $与 $
pfixes并产生无效的组装。
ASM(
MOV%[显示终端(%[S],%[指数],8),%% RBX
:[S]+ R(S)
[指数]+ R(一)
:显示终端]I(offsetof(结构S,A))
:记忆,抄送,RAX,RBX
);
实际上是的可能,使用%C ...
操作数修正:
的#include<&STDDEF.H GT;
#包括LT&;&stdint.h GT;的struct
{
诠释A,B;
};INT富(结构S * S,int i)以
{
INT R;
ASM(
MOVL%C [显示终端(%[S],%[指数],8),%[R] \\ n \\ t的
:[R]= R(r)的
:[S]R(S),[指数]R((uintptr_t形式)I)
[显示终端]E(offsetof(结构S,B))
:
); 返回ř;
}
感谢感谢在这里是因为 - 发现这里 。有一个gcc的邮件列表发布提到这个问题,以及;关键字有输出替代。结果
该计算器发帖是什么%C意味着GCC内嵌装配code?也有关于%C
特别的解释。
I am trying to write a small piece of my code in GCC style extended asm (x86-64 target) and am having trouble encoding struct offsets.
I have a struct s
with a member size_t a[]
, a pointer to such a struct and an index both of which are generated within the asm block.
Now I need to address that element in asm
asm (
"mov %[displ](%[s], %[index], 8), %%rbx"
: [s] "+r" (s)
, [index] "+r" (i)
: "memory", "cc", "rax", "rbx"
);
How can I encode displ
into the asm block? Passing offsetof(struct s, a)
as an immediate prefixes it with $
and generates invalid assembly.
asm (
"mov %[displ](%[s], %[index], 8), %%rbx"
: [s] "+r" (s)
, [index] "+r" (i)
: [displ] "i" (offsetof(struct s, a))
: "memory", "cc", "rax", "rbx"
);
It actually is possible, using the %c...
operand modifier:
#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;
}
Thanks where thanks is due - found that here. There's a gcc mailing list posting referring to this as well; the keywords there are "output substitution".
The stackoverflow posting What does %c mean in GCC inline assembly code? also has an explanation about %c
in particular.
这篇关于GCC扩展的asm,结构元素偏移编码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!