title: Assembly Language to C issue with registers
body: <p>I am trying to write C code that will have an effect equivalent to the following assembly code:</p>
  
  <pre><code>addq %rsi, %rdi
  imulq %rdx, %rdi
  movq %rdi, %rax
  sarq $15, %rax
  salq $31, %rax
  andq %rdi, %rax
  ret
  </code></pre>
  
  <p>With a function prototype</p>
  
  <p>long decode(long x, long y, long z)</p>
  
  <p>Parameters x, y, and z are passed in registers %rdi, %rsi, and %rdx. The code stores the return value in register %rax.</p>
  
  <p>So far I have the following C code:</p>
  
  <pre><code>long decode(long x, long y, long z)
  {
    long w;
  
    x = x+y;
    x = x*z;
  
    w = x;
    w = ((x&gt;&gt;15)&lt;&lt;31);
  
    return x&amp;w;
  }
  </code></pre>
  
  <p>Which, when compiled with the gcc -O2 -S -c filename.c produces the following:</p>
  
  <pre><code>addq %rdi, %rsi
  imulq %rdx, %rsi
  movq %rsi, %rax
  sarq $15, %rax
  salq $31, %rax
  andq %rsi, %rax
  ret
  </code></pre>
  
  <p>Apparently, the registers %rdi and %rsi are swapped.</p>
  
  <p>So, if I change the function by swapping x and y, it looks like this:</p>
  
  <pre><code>long decode4(long x, long y, long z)
  {
    long w;
  
    y = x + y;
    y = y * z;
  
    w = y;
    w = ((y&gt;&gt;15)&lt;&lt;31);
  
    return y&amp;w;
  }
  </code></pre>
  
  <p>And, again the assembly looks like this:</p>
  
  <pre><code> addq %rdi, %rsi
   imulq %rdx, %rsi
   movq %rsi, %rax
   sarq $15, %rax
   salq $31, %rax
   andq %rsi, %rax
   ret
  </code></pre>
  
  <p>The swapping of y and x, did not change a thing in the assembly code produced.
  Any ideas of how to fix that issue?
  Thanks!</p>