title: Miscalculation in assembly language
body: <p>The program gives a wrong calculation in assembly language compared to C++ and I can't find the error. Any ideas?</p>
  <pre><code>// (2 * c + b + a - 5) / (a / 4 - 1)
  
  #include &lt;iostream&gt;
  
  using namespace std;
  
  void print_of() {
      cout &lt;&lt; &quot;Overflow error\n&quot;;
      exit(1);
  }
  
  void print_zf() {
      cout &lt;&lt; &quot;Division by zero error\n&quot;;
      exit(1);
  }
  
  int main() {
      long int a, b, c;
      long int asm_answer;
      cout &lt;&lt; &quot;(2 * c + b + a - 5) / (a / 4 - 1)\n&quot;;
      cout &lt;&lt; &quot;Enter a, b, c: &quot;;
      cin &gt;&gt; a &gt;&gt; b &gt;&gt; c;
      // eax, ebx, ecx - general-purpose registers, its specific abilities don't matter in this laboratory work
      __asm {
          ; a / 4
          mov rax, a; rax = a
          mov rbx, 4; rbx = 4
          cqo; Convert Quad to Octa (rax -&gt; rdx:rax)
          idiv rbx; division with a sign (dividend should be in rdx:rax -&gt; quotient in rax, remainder in rdx)
          
          ; a / 4 - 1
          dec rax; decrement
          jo of_error; overflow error
          jz zf_error; if rax = 0, there will be division by zefo error
           
          ; reset
          push rax; push (a / 4 - 1) in stack
          
          ; 2 * c
          mov rax, c; rax = c
          imul rax, 2; rax = rax * 2
          jo of_error; overflow error
          
          ; 2 * c + b
          mov rdx, b; rdx = b
          add rax, rdx; rax = rax + rdx
          jo of_error; overflow error
          
          ; 2 * c + b + a
          mov rdx, a; rdx = a
          add rax, rdx; rax = rax + rdx
          jo of_error; overflow error
          
          ; 2 * c + b + a - 5
          sub rax, -5; rax = rax - 5
          jo of_error; overflow error
          
          ; rax = 2 * c + b - 5, rbx = a / 4 - 1
          pop rbx; rbx = a / 4 - 1
          
          ; (2 * c + b + a - 5) / (a / 4 - 1)
          cqo; Convert Quad to Octa (rax -&gt; rdx:rax)
          idiv rbx; division with a sign (dividend should be in rdx:rax -&gt; quotient in rax, remainder in rdx)
          mov asm_answer, rax
          jmp ext; &quot;exit&quot; is reserved
          
      of_error:
          call print_of; print overflow error (exit in function!)
      zf_error:
          call print_zf; print division by zero error (exit in function!)
      ext:
          
      }
      cout &lt;&lt; &quot;asm result: &quot; &lt;&lt; asm_answer &lt;&lt; &quot;\n&quot;;
      cout &lt;&lt; &quot;C++ result: &quot; &lt;&lt; (2 * c + b + a - 5) / (a / 4 - 1) &lt;&lt; &quot;\n&quot;;
      return 0;
  }
  </code></pre>
  <p>Example of console output:</p>
  <blockquote>
  <p>Enter a, b, c: 100 0 20
  asm result: 6
  C++ result: 5
  Program ended with exit code: 0</p>
  </blockquote>
  <p>Another example:</p>
  <blockquote>
  <p>Enter a, b, c: 0 0 0
  asm result: -5
  C++ result: 5
  Program ended with exit code: 0</p>
  </blockquote>