本文简介介绍一个在gcc中写asm的例子..

#include <stdio.h>

int main()
{
    int a=10,b=0;
    printf("0) a: %d b: %d\n",a,b);
    // b = a
    asm ( "movl %1, %%eax;\
           movl %%eax, %0;"
          : "=r"(b) // output
          : "r"(a) // input
          :"%eax" // clobbered register
        );

    printf("1) a: %d b: %d\n",a,b);

    // b = a * 5
    asm( "leal (%1,%1,4), %0"
          : "=r" (b)
          : "r" (a)
          );

    printf("2) a: %d b: %d\n",a,b);
    return 0;
}

Output

$ ./a.out
0) a: 10 b: 0
1) a: 10 b: 10
2) a: 10 b: 50

References

[1] http://argcv.com/archives/84 GCC汇编器语法

Categories: Code

Yu

Ideals are like the stars: we never reach them, but like the mariners of the sea, we chart our course by them.

Leave a Reply

Your email address will not be published.