title: How do I divide two numbers in assembly without using the MUL or DIV operators?
body: <p>I am working on something that will divide two numbers in assembly without using the MUL or DIV operators.  The logic is, if n*var &lt; x, where x is the number to divide and var is the number to divide by, increment n by 1 and repeat.  So in the event of there being a remainder, the divider will round up the answer by 1.</p>
  
  <p>First, I created a multiplier that takes advantage of bit shifting, and this works.</p>
  
  <pre><code>//multiply
  int ans, var1;
  _asm {
          mov eax, 00001111b; A = 1111
          mov ebx, 00001010b; B = 1010
          mov var1, 00000001b; C = 0001
          mov ecx, 0;
          shl ebx, 3; shift b register left by 3
      L1:
          cmp ecx, 4; check if multiplication is done
              je L4; go to empty loop
          inc ecx; increment counter by 1
          AND var1, eax; check if the least significant bit is 1
          cmp var1, 1; checks if var1 is = 1
              je L2;
          cmp var1, 0; check if var1 = 0
              je L3;
      L2:
          shr eax, 1; shift a register right 1 bit
          add eax, ebx; add a and b registers
          jmp L1; go back to L1
      L3:
          shr eax, 1;
          jmp L1; go back to L1
      L4 :
          mov ans, eax; move eax to answer
  };
  cout &lt;&lt; "Multiply 00001111 and 00001010" &lt;&lt; endl &lt;&lt; ans &lt;&lt; endl;
  </code></pre>
  
  <p>So what I am trying to do is implement this multiplier with two more loops:  one that increments n and sets the registers to the correct numbers to multiply, and one that checks if n<em>var >= x.  If n</em>var &lt; x, it will go to increment n and set the registers to multiply, then multiply them out.  After the answer is found, the incremented register (edx) is but into variable d and output to the console.</p>
  
  <p>There is something wrong in this program that I'm not seeing, and somehow, the answer always turns out to be var2*16, where var2 is the number to divide.</p>
  
  <pre><code>int var1;
  int var2;
  int var3;
  int d;
  _asm {
      ; if n*var3 &lt; var2, increment n and repeat
      mov var2, 12; number to be divided
      mov var3, 5; number to divide with
      mov ecx, 0;
      mov edx, 0; number to increment
  L11 :
      cmp eax, var2;
      js L12; if n*n &lt; var2, increment n and multiply.
      cmp eax, var2
      jns L16; answer found, jump to empty loop       
  L12 :
      inc edx
      mov eax, edx
      mov ebx, var3
      shl ebx, 3
      mov ecx, 0
      jmp L13
  L13 :
      cmp ecx, 4; check if multiplication is done
      je L11;
      inc ecx; increment counter by 1
      AND var1, eax; check if the least significant bit is 1
      cmp var1, 1; checks if var1 is = 1
      je L14;
      cmp var1, 0; check if var1 = 0
      je L15;
  L14:
      shr eax, 1; shift a register right 1 bit
      add eax, ebx; add a and b registers
      jmp L13; go back to L1
  L15 :
      shr eax, 1;
      jmp L13;
  L16:
      mov d, edx
  };
  cout &lt;&lt; "The quotient is " &lt;&lt; d &lt;&lt; endl;
  </code></pre>
  
  <p>I'm also creating a very similar program that calculates the square root of a number, where if n*n &lt; x, where x is the number to take the square root of, increment n and repeat.  Solving this bug with the divider should also help me with the square root program, where the same bug is occuring.</p>
  
  <p>I've been stuck on this for a few days, and I cannot figure out what is wrong that's causing this strange bug.  Any help is appreciated.</p>