title: Why is the SF bit set when the first operand is greater?
body: <p>I have the following 32-bit x86 assembly code:</p>
  
  <pre><code>.text
      .global _start
  
  _start:
      /* Compare 3 &lt; 2 */
      mov     $2, %eax
      cmp     $3, %eax
  
      /* Set the low byte of %eax according to the SF and 0F flags. */
      setl    %al
  
      /* Syscall exit with the value of %eax. */
      mov     %eax, %ebx
      mov     $1, %eax
      int     $0x80
  </code></pre>
  
  <p>If I assemble and link it, I get a binary that exits with an exit code of 1. If I switch the CMP operands, my binary exits with 0.</p>
  
  <p>This is the opposite of what I expected. CMP <a href="http://x86.renejeschke.de/html/file_module_x86_id_35.html" rel="nofollow">according to this x86 reference</a>:</p>
  
  <blockquote>
    <p>The comparison is performed by subtracting the second operand from the first operand and then setting the status flags in the same manner as the SUB instruction.</p>
  </blockquote>
  
  <p>The first operand is 3, the second operand is 2. 3-2 is 1, which is greater than zero, so why is SF (the sign flag) set?</p>