title: Trying to recode memmove in asm
body: <p>I am using nasm on Ubuntu 16.04, and I'm currently trying to recode the C <code>memmove()</code> function.</p>
  
  <p>Here is my code : </p>
  
  <pre><code>BITS 64
  
  global memmove
  
  memmove:
          push rbp
          mov rbp, rsp
          xor rcx, rcx
  
  while:
          cmp rcx, rdx
          je end
          mov r10b, byte [rsi + rcx]
          mov byte [rdi + rcx], r10b
          inc rcx
          jmp while
  
  end:
          mov rax, rdi
          leave
          ret
  </code></pre>
  
  <p>I'm calling it this way : </p>
  
  <pre><code>char *str = strdup("Salutation");
  printf("%s, %s\n", (char *)memmove(str + 3, str, 5), str);
  </code></pre>
  
  <p>Expected output is : </p>
  
  <pre><code>Saluton, SalSaluton
  </code></pre>
  
  <p>But I get  :</p>
  
  <pre><code>SalSaon, SalSalSaon
  </code></pre>
  
  <p>For some reasons, when I get to the fourth character, it goes back to the begining of my string <code>rsi</code>. My question is why? What am I doing wrong?</p>
  
  <p>PS : The same problem happens every three characters, like if it could not go futher, and had to go back to the first one.</p>
  
  <p>Thanks in advance.</p>