title: How to compare two registers in x86-64
body: <p>I'm comparing two arrays in assembly character by character, the C code would be something like this:</p>
  
  <pre><code>   count=0;
     for (i=0;i&lt;vector;i++) {
       if (array1[i]==array2[i]) {
         count++;
       }
     }
  </code></pre>
  
  <p>Basically, both arrays are compared and when the character in that position is the same, count is increased.</p>
  
  <p>I ended up with this code in assembly, which actually seems to work as intended:</p>
  
  <pre><code>   forEachVector:
       cmp bl, vector
       jge endforEachVector
       mov rax, QWORD[array1+rbx] ; move array[i] to rax
       mov rdx, QWORD[array2+rbx] ; move array[i] to rdx
  
       if1Equals2:
       cmp dl, al
       jne fi1Equals2
       inc cl
       fi1Equals2:
  
       inc bl
       jmp forEachVector
     endforEachVector: 
  </code></pre>
  
  <p>vector is a constant, and array1 and 2 are extern and defined in C as "char array1[vector]". 
  In my understanding, array1[i] is 16 bit long because it's a char array, and a char is an int == 16 bits. Because of that, first I tried to operate with the full 64 bit registers, jut like:</p>
  
  <pre><code>cmp rdx, rax
  </code></pre>
  
  <p>And the code fails if I use 64bits registers (the zero bit is not set so jne is triggered and if ends), it only works only when I use al and dl (8 lsb) and I want to understand why.</p>
  
  <p>It's not a problem to compare the 8 lsb because arrayX[i] is never going to be higher than 2^8, but doing cmp rax, rdx and cmp al, dl should have the same effect.</p>