title: xorl %eax, %eax in x86_64 assembly code produced by gcc
body: <p>I'm a total noob at assembly, just poking around a bit to see what's going on.  Anyway, I wrote a very simple function:</p>
  
  <pre><code>void multA(double *x,long size)
  {
    long i;
    for(i=0; i&lt;size; ++i){
      x[i] = 2.4*x[i];
    }
  }
  </code></pre>
  
  <p>I compiled it with:</p>
  
  <pre><code>gcc -S -m64 -O2 fun.c
  </code></pre>
  
  <p>And I get this:</p>
  
  <pre><code>    .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
  </code></pre>
  
  <p>The assembly output makes sense to me (mostly) except for the line <code>xorl %eax, %eax</code>.  From googling, I gather that the purpose of this is simply to set <code>%eax</code> to zero, which in this case corresponds to my iterator <code>long i;</code>.</p>
  
  <p>However, unless I am mistaken, <code>%eax</code> is a 32-bit register.  So it seems to me that this should actually be <code>xorq %rax, %rax</code>, particularly since this is holding a 64-bit long int.  Moreover, further down in the code, it actually uses the 64-bit register <code>%rax</code> to do the iterating, which never gets initialized outside of <code>xorl %eax %eax</code>, which would seem to only zero out the lower 32 bits of the register.</p>
  
  <p>Am I missing something?</p>
  
  <p>Also, out of curiosity, why are there two <code>.long</code> constants there at the bottom?  The first one, <code>858993459</code> is equal to the double floating-point representation of <code>2.4</code> but I can't figure out what the second number is or why it is there.</p>