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

标签 c linux gcc assembly x86

由于我是 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;"
);

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

有人可以对此提出解决方案吗?

最佳答案

我猜你要找的是 extended assembly例如:

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

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

int a = 10, b;
b = a;

请查看内联评论。

注意:

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

mov eax, 4            // Intel notation

一本关于 inline assembly 的好书在 GCC 环境中。

关于将 C 变量的内容复制到寄存器 (GCC),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14494818/

相关文章:

c++ - 为什么我的程序在恰好循环 8192 个元素时很慢?

c++ - 条件变量中的 gcc 错误::wait_for

c - 利用 LLVM 的半路交叉编译 - 在 Raspberry Pi 上编译更快

创建目录并返回 dirfd 为 `open`

c - 在 C 程序中,收到警告 : "Statement with no effect"

c++ - 在 QT 项目中使用 Crypto++ 静态库

java - Linux ./configure 不会检测到 java 或 javac

c++ - 防止链接器删除导出的函数

c - C 中 arduino 上的 ADC

c - c中的结构和标记 union