title: x86 Assembly: How do I get the quotient and remainder from a function
body: <p>Here are my questions,</p>
  
  <ol>
  <li>How do I store the remainder from edx back at the memory address of the third parameter?</li>
  <li>Do I need to use a subroutine in order to do so?</li>
  <li>What is the most efficient way to do so?</li>
  </ol>
  
  <p>If you could show the correct code for this or a detailed explanation of how to that would be great. If you could show a combination of the two that would be excellent.</p>
  
  <p>Here is my .asm file:</p>
  
  <pre><code>.386
  
  .model flat
  
  public _Divide
  
  .code
  
  _Divide proc
  
          push ebp
          mov ebp, esp
          mov eax, [ebp + 8]
          mov ebx, [ebp + 12]
          cwd
          idiv ebx
          mov ecx, [ebp + 16]
          cmp ecx, 0
          jne remainder
          jmp done
  remainder:
          mov [ebp + 16], edx   ;this is showing as the minimum value for a 
                                ;long in the cpp(-860,000,000)
  done:
          pop ebp
          ret
  _Divide endp
  
          end
  </code></pre>
  
  <p>Here is the .cpp file from which I am calling the _Division function:</p>
  
  <pre><code>#include &lt;iostream&gt;
  
  using namespace std;
  
  extern "C" long Divide (long, long, long *);
  
  void main ()
  {
  long    Result;
  long    Remainder;
  long    Dividend;
  long    Divisor;
  
  do
      {
      cout &lt;&lt; "Enter Dividend" &lt;&lt; endl; 
      cin &gt;&gt; Dividend;
      cout &lt;&lt; "Enter Divisor" &lt;&lt; endl;
      cin &gt;&gt; Divisor;
  
      Result = Divide (Dividend, Divisor, &amp;Remainder);
      cout &lt;&lt; "Result is " &lt;&lt; Result &lt;&lt; " and Remainder is " &lt;&lt; Remainder &lt;&lt; endl;
      } while ((Result &gt;= 0) || (Remainder != 0));
  
  Result = Divide (Dividend, Divisor, 0);
  cout &lt;&lt; "Result is " &lt;&lt; Result &lt;&lt; " and Remainder is not used" &lt;&lt; endl;
  }
  </code></pre>