我怎样写下面的内联汇编$ C $用C使用GCCÇ [英] How do I write the following inline Assembly Code in C using GCC

查看:239
本文介绍了我怎样写下面的内联汇编$ C $用C使用GCCÇ的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在阅​​读这里的一些答案和问题,保持想出​​这个建议,但我发现没有人真正解释说,正是你需要做的采用英特尔和GCC编译器去做,在Windows上的东西。下面评论正是我想要做的。

 的#include<&stdio.h中GT;诠释的main()
{
    INT X = 1;
    INT Y = 2;
    //组装code开始
    / *
      推x转换成堆叠; <需要帮助
      X = Y; <有了这个
      弹出堆栈成Y; <请
    * /
    //组装code结束
    输出(X =%d个,Y =%d个,X,Y);
    的getchar();
    返回0;
}


解决方案

让编译器来选择寄存器,使用 K preFIX表示32位报名参加 INT 键入(所以它可以作为预期的x86-64):

  __ asm__(pushl%K0 \\ n \\ t的
         MOVL%K1,K0%\\ n \\ t的
         popl%K1
         :+ R(x)中,+ R(Y));

这是没有必要的(实际上是错的)在这种情况下指定任何被破坏的操作数。


这也是明显的是 X 是可交换的位置,也就是说,交换操作数还是应该产生相同的使用效果::+%R(X),+ R(Y),其中表示该操作数和下一个操作数的可以的通勤。

I was reading some answers and questions on here and kept coming up with this suggestion but I noticed no one ever actually explained "exactly" what you need to do to do it, On Windows using Intel and GCC compiler. Commented below is exactly what I am trying to do.

#include <stdio.h>

int main()
{
    int x = 1;
    int y = 2;
    //assembly code begin
    /*
      push x into stack; < Need Help
      x=y;               < With This
      pop stack into y;  < Please
    */
    //assembly code end
    printf("x=%d,y=%d",x,y);
    getchar();
    return 0;
}

解决方案

Let the compiler choose the registers, using the k prefix to denote a 32-bit register for the int type (so it works as expected on x86-64) :

__asm__ ("pushl %k0\n\t"
         "movl %k1, %k0\n\t"
         "popl %k1"
         : "+r" (x), "+r" (y));

It's not necessary (and actually wrong) to specify any clobbered operands in this case.


It's also apparent that x,y are commutative here, i.e., swapping the operands should still yield the same result using: : "+%r" (x), "+r" (y) where % denotes that this operand and the next operand may commute.

这篇关于我怎样写下面的内联汇编$ C $用C使用GCCÇ的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆