GCC扩展的asm,结构元素偏移编码

编程入门 行业动态 更新时间:2024-10-26 20:23:30
本文介绍了GCC扩展的asm,结构元素偏移编码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我试图写在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,结构元素偏移编码

本文发布于:2023-11-05 18:57:03,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1561596.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:元素   结构   GCC   asm

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!