码迷,mamicode.com
首页 > 其他好文 > 详细

asm: Writing Inline Assembly

时间:2016-07-20 13:27:51      阅读:123      评论:0      收藏:0      [点我收藏+]

标签:

A usual IA includes these parts:

asm [volatile] ( AssemblerTemplate
               : OutputOperands
             [ : InputOperands
             [ : Clobbers ] ])
  • the first line includes asm [volatile], it means the contents in the following parenthesis is IA.
  • the AssblerTemplate is some assembler snippet, separated by ‘;‘, surrounded by "". for example, "mov %1 %%eax; mov %%eax %0" means to mov %1‘s content into %0. [first into %%eax, then into %0], note that the %N‘s N is the order of the CVars displays in the OutputOprands, and if N is larger than the total counts of the OOs, the exceeding is taken from vars above the assembly part, this part will be explained in the example.
  • the OutputOprands is consist of 3 parts, the alias name, the constraints and the variable, this can be duplicated and separated by ‘,‘. like [asmVarName1] "=m"(CVar1),[asmVarName2] "+r"(CVar2)
  • the InputOperands is almost the same as the OOs, but note that the constraints part should not begin with "+" "=" or other modifiers.
  • the Clobber part, declare the parts of register name to be used, or if using memory, declare memory usage.

example:

#include <stdio.h>
int main()
{
    //changes a and b‘s value
    int a = 10, b = 0;
    asm(//note this program should be compile with gcc
        "xchg %1,%%eax;xchg %%eax,%0"
        :"=r"(b),"=m"(a)
        :"r"(a)
        :"%eax" //note register declare with one ‘%‘
    );
    return 0;
}

note the register %eax is not initialized. a‘s value should be unknown. b‘s value should be 10.

the question is, in assembler, mov a,b means pass b‘s value into a, however, here is the opposite, to pass a‘s value into b..

asm: Writing Inline Assembly

标签:

原文地址:http://www.cnblogs.com/sansna/p/5687830.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!