title: IA-32 assembly divison
body: <p>Can somebody explain to me the following snippet of assembly:</p>
  
  <pre><code>mydiv:
          pushl   %ebp
          movl    %esp, %ebp
          movl    8(%ebp), %edx  ; get x
          movl    %edx, %eax
          sarl    $31, %edx      ; divide by y
          idivl   12(%ebp)       ; return %eax
          popl    %ebp
          ret
  </code></pre>
  
  <p>This is the equivalent to the following C function:</p>
  
  <pre><code>int mydiv( int x, int y )
  {
      return x / y;
  }
  </code></pre>
  
  <p>The part which I'm having trouble understanding is the <code>sarl</code> instruction: why do you need to shift <code>edx</code>?</p>