title: GCC inline assembler using memory references
body: <p>I'm trying to write an inline assembly instruction that will load a variable with the contents of a register by using a pointer to that variable instead of a direct reference.</p>
  
  <p>The code using a direct reference works fine and it looks like this:</p>
  
  <pre><code>int x;
  int *y = &amp;x;
  int z = 1;
  
  __asm__ __volatile__ ("mov %%edx, %0;"::"r"(z):);
  __asm__ __volatile__ ("mov %0, %%edx;":"=r" (x)::); 
  printf("\n%x\n", x);
  </code></pre>
  
  <p>disasm:</p>
  
  <pre><code>0x000000000040052d &lt;+0&gt;:     push   %rbp  
  0x000000000040052e &lt;+1&gt;:     mov    %rsp,%rbp                                          
  0x0000000000400531 &lt;+4&gt;:     sub    $0x10,%rsp                                         
  0x0000000000400535 &lt;+8&gt;:     lea    -0x10(%rbp),%rax                                   
  0x0000000000400539 &lt;+12&gt;:    mov    %rax,-0x8(%rbp)                                    
  0x000000000040053d &lt;+16&gt;:    movl   $0x1,-0xc(%rbp)                                    
  0x0000000000400544 &lt;+23&gt;:    mov    -0xc(%rbp),%eax                                    
  0x0000000000400547 &lt;+26&gt;:    mov    %edx,%eax                                          
  0x0000000000400549 &lt;+28&gt;:    mov    %eax,%edx                                          
  0x000000000040054b &lt;+30&gt;:    mov    %eax,-0x10(%rbp)                                   
  0x000000000040054e &lt;+33&gt;:    mov    -0x10(%rbp),%eax                                   
  0x0000000000400551 &lt;+36&gt;:    mov    %eax,%esi                                          
  0x0000000000400553 &lt;+38&gt;:    mov    $0x4005f4,%edi                                     
  0x0000000000400558 &lt;+43&gt;:    mov    $0x0,%eax                                          
  0x000000000040055d &lt;+48&gt;:    callq  0x400410 &lt;printf@plt&gt;                              
  0x0000000000400562 &lt;+53&gt;:    mov    $0x0,%eax                                          
  0x0000000000400567 &lt;+58&gt;:    leaveq       
  0x0000000000400568 &lt;+59&gt;:    retq      
  </code></pre>
  
  <p>and it outputs 1 as expected</p>
  
  <p>The pointer version looks like this:</p>
  
  <pre><code>int x;
  int *y = &amp;x;
  int z = 1;
  
  __asm__ __volatile__ ("mov %%edx, %0;"::"r"(z):);
  __asm__ __volatile__ ("mov (%0), %%edx;":"+r" (y)::);
  //or
  __asm__ __volatile__ ("mov %[mem], %%edx":[mem] "=m" (y)::);
  printf("\n%x\n", x);
  </code></pre>
  
  <p>disasm:</p>
  
  <pre><code>0x000000000040052d &lt;+0&gt;:     push   %rbp
  0x000000000040052e &lt;+1&gt;:     mov    %rsp,%rbp
  0x0000000000400531 &lt;+4&gt;:     sub    $0x10,%rsp
  0x0000000000400535 &lt;+8&gt;:     lea    -0x10(%rbp),%rax
  0x0000000000400539 &lt;+12&gt;:    mov    %rax,-0x8(%rbp)
  0x000000000040053d &lt;+16&gt;:    movl   $0x1,-0xc(%rbp)
  0x0000000000400544 &lt;+23&gt;:    mov    -0xc(%rbp),%eax
  0x0000000000400547 &lt;+26&gt;:    mov    %edx,%eax
  0x0000000000400549 &lt;+28&gt;:    mov    -0x8(%rbp),%edx
  0x000000000040054c &lt;+31&gt;:    mov    -0x10(%rbp),%eax
  0x000000000040054f &lt;+34&gt;:    mov    %eax,%esi
  0x0000000000400551 &lt;+36&gt;:    mov    $0x4005f4,%edi
  0x0000000000400556 &lt;+41&gt;:    mov    $0x0,%eax
  0x000000000040055b &lt;+46&gt;:    callq  0x400410 &lt;printf@plt&gt;
  0x0000000000400560 &lt;+51&gt;:    mov    $0x0,%eax
  0x0000000000400565 &lt;+56&gt;:    leaveq 
  0x0000000000400566 &lt;+57&gt;:    retq   
  </code></pre>
  
  <p>This prints a non-deterministic integer every time (i.e bb524b90 15979050). When run in gdb it prints the same integer (ffffe2f0) every time, which doesn't change based on the value of z. Does anyone have an idea what causes this?</p>