gcc - gcc 生成的 x86_64 汇编代码中的 xorl %eax, %eax

标签 gcc assembly x86-64

我在组装方面完全是个菜鸟,只是四处看看发生了什么。无论如何,我写了一个非常简单的函数:

void multA(double *x,long size)
{
  long i;
  for(i=0; i<size; ++i){
    x[i] = 2.4*x[i];
  }
}

我编译它:
gcc -S -m64 -O2 fun.c

我明白了:
    .file   "fun.c"
    .text
    .p2align 4,,15
    .globl  multA
    .type   multA, @function
multA:
.LFB34:
    .cfi_startproc
    testq   %rsi, %rsi
    jle .L1
    movsd   .LC0(%rip), %xmm1
    xorl    %eax, %eax
    .p2align 4,,10
    .p2align 3
.L3:
    movsd   (%rdi,%rax,8), %xmm0
    mulsd   %xmm1, %xmm0
    movsd   %xmm0, (%rdi,%rax,8)
    addq    $1, %rax
    cmpq    %rsi, %rax
    jne .L3
.L1:
    rep
    ret
    .cfi_endproc
.LFE34:
    .size   multA, .-multA
    .section    .rodata.cst8,"aM",@progbits,8
    .align 8
.LC0:
    .long   858993459
    .long   1073951539
    .ident  "GCC: (Ubuntu/Linaro 4.6.3-1ubuntu5) 4.6.3"
    .section    .note.GNU-stack,"",@progbits

除了行 xorl %eax, %eax 之外,汇编输出对我来说(大部分)有意义.通过谷歌搜索,我认为这样做的目的只是为了设置 %eax为零,在这种情况下对应于我的迭代器 long i; .

但是,除非我弄错了,%eax是一个 32 位寄存器。所以在我看来,这实际上应该是 xorq %rax, %rax ,特别是因为它持有一个 64 位长整数。此外,在代码的更深处,它实际上使用了 64 位寄存器 %rax进行迭代,它永远不会在 xorl %eax %eax 之外初始化,这似乎只是将寄存器的低 32 位清零。

我错过了什么吗?

另外,出于好奇,为什么有两个 .long底部有常数吗?第一个,858993459等于 2.4 的双浮点表示但我无法弄清楚第二个数字是什么或为什么它在那里。

最佳答案

I gather that the purpose of this is simply to set %eax to zero



是的。

which in this case corresponds to my iterator long i;.



号您的 i在声明中未初始化。严格来说,该操作对应于 i = 0 for 循环中的表达式。

However, unless I am mistaken, %eax is a 32-bit register. So it seems to me that this should actually be xorq %rax, %rax, particularly since this is holding a 64-bit long int.



但是清除寄存器的低位双字会清除整个寄存器。这不是直观的,但它是隐含的。

关于gcc - gcc 生成的 x86_64 汇编代码中的 xorl %eax, %eax,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19082108/

相关文章:

c - 在无法识别 ## 的编译器上吞下可变参数宏中的逗号

使用 GCC 交叉编译 C 应用程序

c++ - <atomic> 的 CMPXCHG16B 和 MSVC 实现不是默认的?

assembly - 左移操作期间使用的寄存器

visual-studio-2010 - __attribute__() 宏及其对基于 Visual Studio 2010 的项目的影响

c++ - 修复警告 "comparison is always false due to limited range of data type [-Wtype-limits]"

assembly - 最佳解决方案: add or addu

linux - 汇编 - 将参数传递给函数调用

c - 64位机上栈帧的创建

c++ - 为什么 GCC 不能为两个 int32s 的结构生成最佳 operator==?