title: Help understanding DIV instruction in x86 inline assembly
body: <p>While reading through some source code in a GNU project, I came across this bit of inline assembly:</p>
  
  <pre><code>__asm__ (
    "divq %4"
    : "=a" (q), "=d" (r)
    : "0" (n0), "1" (n1), "rm" (d)
  );
  </code></pre>
  
  <p>Here the variables <code>q</code>, <code>r</code>, <code>n0</code>, <code>n1</code>, and <code>d</code> are 64-bit integers.  I know enough assembly to get the gist of what this does, but there's some details I'm not certain about.</p>
  
  <p><strong>What I understand:</strong></p>
  
  <p>We're dividing the contents of the RAX register by <code>d</code>, placing the quotient in <code>q</code>, and placing the remainder in <code>r</code>.</p>
  
  <p><strong>What I don't understand</strong></p>
  
  <ol>
  <li>Why are there <em>three</em> inputs
  here?  We only need to input a
  dividend and a divisor, so what use
  could there be for 3 inputs?</li>
  <li>I can't tell which of the inputs is the dividend.  More generally, I don't see anything actually
  being loaded into the RAX register,
  so how does it know what to divide by what?</li>
  </ol>