GCC内联汇编(inline assembly )的几个例子

https://gcc.gnu.org/onlinedocs/gcc/index.html#SEC_Contents

int __ffs (int x)
{
  int cnt;
  int tmp;

  asm ("bsfl %2,%0\n"       /* Count low bits in X and store in %1.  */
       "cmovel %1,%0\n"     /* If number was zero, use -1 as result.  */
       : "=&r" (cnt), "=r" (tmp) : "rm" (x), "1" (-1));

  return cnt + 1;
}


int asm1(void)
{
    int src=1;
    int dst;
    __asm__ ("movl %1, %0\n\t"
            "addl $1, %0"
            :"=r"(dst)
            :"r"(src));

    printf("src=%d,dst=%d\n",src,dst);

}

int asm2(void)
{
    int src=1;
    int dst;
    __asm__ ("movl %1, %0\n\t"
            "addl $1, %0"
            :"=r"(dst)
            :"m"(src));

    printf("src=%d,dst=%d\n",src,dst);

}

void asm3(void)
{
    int Mask = 1234;
    int Index;

      asm ("bsfl %1, %0"
         : "=r" (Index)
         : "r" (Mask)
         : "cc");

}


void asm3b(void)
{
    int Mask = 1234;
    int Index;

      asm ("bsfl %[aMask], %[aIndex]\n\t"
         "addl $1,%1\n\t"
         "addl $2,%0"

         : [aIndex]"=r" (Index),[aMask]"=r"(Mask)
         : "r" (Mask)
         : "cc");

}


int asm4(void)
{
            int a=10, b;
            asm ("movl %1, %%eax\n\t"
                 "movl %%eax, %0"
                 :"=r"(b)        /* output */
                 :"r"(a)         /* input */
                 :"%eax"         /* clobbered register */
                 );
            return b;
}

int asm4b(void)
{
    int foo = 10, bar = 15;
    asm("addl  %%ebx,%%eax"
    :"=a"(foo)
    :"a"(foo), "b"(bar)
                                 );
    printf("foo+bar=%d\n", foo);
    return foo;

}

你可能感兴趣的:(C)