Gcc汇编inline

int a=10, b;
asm ("movl %1, %%eax; 
      movl %%eax, %0;"
     :"=r"(b)        /* output */
     :"r"(a)         /* input */
     :"%eax"         /* clobbered register */
     );      

Here what we did is we made the value of ’b’ equal to that of ’a’ using assembly instructions. Some points of interest are:

“b” is the output operand, referred to by %0 and “a” is the input operand, referred to by %1.
“r” is a constraint on the operands. We’ll see constraints in detail later. For the time being, “r” says to GCC to use any register for storing the operands. output operand constraint should have a constraint modifier “=”. And this modifier says that it is the output operand and is write-only.
There are two %’s prefixed to the register name. This helps GCC to distinguish between the operands and registers. operands have a single % as prefix.
The clobbered register %eax after the third colon tells GCC that the value of %eax is to be modified inside “asm”, so GCC won’t use this register to store any other value.
When the execution of “asm” is complete, “b” will reflect the updated value, as it is specified as an output operand. In other words, the change made to “b” inside “asm” is supposed to be reflected outside the “asm”.
这段来自于:
http://www.ibiblio.org/gferg/ldp/GCC-Inline-Assembly-HOWTO.html
b是输出操作数,关联的是上面的‘1’;a是输入操作数,关联的是上面的‘0’;
输出操作数是将寄存器的value写入b的地址,输入操作数是将a的value写入寄存器;
‘r’是对操作数施加的限制,r代表不限制,可以用任何寄存器来存储这个操作数;若是‘a’代表用eax来存储,’b‘代表用ebx来存储,如下所示:
±–±-------------------+
| r | Register(s) |
±–±-------------------+
| a | %eax, %ax, %al |
| b | %ebx, %bx, %bl |
| c | %ecx, %cx, %cl |
| d | %edx, %dx, %dl |
| S | %esi, %si |
| D | %edi, %di |
±–±-------------------+

输出操作数必须带有‘=’号;
clobbered register其实就是告知gcc,这个clobber list里面的寄存器都是不能使用的,不能用它去存储任何值,因为它已经被inline的asm汇编给修改了

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值