Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <blockquote> <p>The first instruction is moving what is in <code>%rax</code> into <code>%rdi</code> to prepare for the <code>printf</code> call. Thats all fine except we just moved <code>$.LC0</code> (which is the string <code>"%s"</code>) into <code>%eax</code>. This seems unnecessary why didn't we just move <code>$.LC0</code> into <code>%rdi</code> in the first place instead of moving it into <code>%eax</code> and then into <code>%rdi</code>?</p> </blockquote> <p>That’s probably because you’re compiling with no optimisations. When I compile your example with GCC 4.2.1 on Mac OS X v10.6.8, I get the following output:</p> <pre><code>.globl _main _main: LFB3: pushq %rbp LCFI0: movq %rsp, %rbp LCFI1: leaq LC0(%rip), %rsi leaq LC1(%rip), %rdi movl $0, %eax call _printf movl $0, %eax leave ret </code></pre> <p>As you can see, the arguments were directly stored into <code>%rsi</code> and <code>%rdi</code>.</p> <blockquote> <p>The second instruction is clearing out <code>%eax</code> which I understand to be the return value of a function. But if the function is going to just clobber it anyways why do GCC care to clear it out?</p> </blockquote> <p>Because the x86_64 ABI specifies that if a function takes variable arguments then <code>AL</code> (which is part of <code>%eax</code>) is expected to hold the number of vector registers used for the arguments to that function call. Since you’re not specifying floating-point arguments when calling <code>printf()</code>, no vector registers are used, so <code>AL</code> (<code>%eax</code>) is zeroed out. I give more examples in an answer to another question <a href="https://stackoverflow.com/questions/6212665/why-is-eax-zeroed-before-a-call-to-printf/6212835#6212835">here</a>.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload