将C变量的内容复制到寄存器(GCC)中

3 c linux x86 assembly gcc

由于我是GCC的新手,因此我遇到了内联汇编代码的问题.问题是我无法弄清楚如何将C变量(属于类型UINT32)的内容复制到寄存器中eax.我试过以下代码:

__asm__
(
    // If the LSB of src is a 0, use ~src.  Otherwise, use src.
    "mov     $src1, %eax;"
    "and     $1,%eax;"
    "dec     %eax;"
    "xor     $src2,%eax;"

    // Find the number of zeros before the most significant one.
    "mov     $0x3F,%ecx;"
    "bsr     %eax, %eax;"
    "cmove   %ecx, %eax;"
    "xor     $0x1F,%eax;"
);
Run Code Online (Sandbox Code Playgroud)

但是mov $src1, %eax;不起作用.

有人可以建议解决这个问题吗?

0x9*_*x90 12

我想你正在寻找的是扩展组装,例如:

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

在上面的例子中,我们使得值b等于a使用汇编指令和eax寄存器的值:

int a = 10, b;
b = a;
Run Code Online (Sandbox Code Playgroud)

请参阅内联评论.

注意:

mov $4, %eax          // AT&T notation

mov eax, 4            // Intel notation
Run Code Online (Sandbox Code Playgroud)

关于GCC环境中内联汇编的一个很好的阅读.