title: X86 assembly - Handling the IDIV instruction
body: <p>I am currently writing a simple C compiler, that takes a .c file as input and generates assembly code (X86, AT&amp;T syntax).
  Everyting is good, but when I try to execute a IDIVQ instruction, I get a floating-point exception. Here's my input:</p>
  
  <pre><code>int mymain(int x){
    int d;
    int e;
    d = 3;
    e = 6 / d;
    return e;
  }
  </code></pre>
  
  <p>And here is my generated code:</p>
  
  <pre><code>mymain:
  .LFB1:
      .cfi_startproc
      pushq   %rbp
      .cfi_def_cfa_offset 16
      movq    %rsp, %rbp
      .cfi_offset 6, -16
      .cfi_def_cfa_register 6
      movq    %rdi, -40(%rbp)
      movq    $3, -8(%rbp)
      movq    $6, %rax
      movq    -8(%rbp), %rdx
      movq    %rdx, %rbx
      idivq   %rbx
      movq    %rax, -16(%rbp)
      movq    -16(%rbp), %rax
      leave
      .cfi_def_cfa 7, 8
      ret
      .cfi_endproc
  .LFE1:
      .size mymain, .-mymain
  </code></pre>
  
  <p>According to <a href="http://www.cs.virginia.edu/~evans/cs216/guides/x86.html">http://www.cs.virginia.edu/~evans/cs216/guides/x86.html</a>, <strong>idivq %rbx</strong> should produce 6/d (the quotient) in <strong>%rax</strong>. But I'm getting a floating-point exception, and I can't seem to find the problem.</p>
  
  <p>Any help will be much appreciated!</p>