2

How can I divide two numbers in Assembly without using DIV instruction but by using shift and add method?

I did that with multiplication and here is my code:

mov bl, 56H ;For example
mov dl, 79H
;start
mov bh, 00H
mov dh, 00H
xor di, di
mov cx, 08H
L1:
shr dx, 1 ;shifting the multiplier
jnc nxt
add di, bx ;adding the multiplicand to the result register di
nxt:
shl bx, 1 ;shifting the multiplicand
loop L1

Secondary question: My teacher told us that there is an instruction called MOVZE to do what I did in lines 4 and 5, but it didn't work? I use emu8086 emulator.

Ammar Alyousfi
  • 4,112
  • 5
  • 31
  • 42
  • 3
    It's `MOVZX`, not `MOVZE`. – Michael Nov 23 '13 at 17:55
  • Even MOVZX doesn't work! – Ammar Alyousfi Nov 23 '13 at 18:03
  • 1
    For division by power n of 2, shift right n times to get the quotient; and get the remainder by and'ing the dividend with a mask that is 0 in all but the right-most n bits, which are 1. Why does this work? – gnometorule Nov 23 '13 at 18:04
  • In fact I know that but I am asking for the algorithm or code. – Ammar Alyousfi Nov 23 '13 at 18:09
  • MOVZX/MOVSX instructions have been introduced with the 80386. An 8086 does not have such instructions. Extending the same register from byte to word the MOVZX instruction brings no benefit. Only when moving to another register or using 32-bit registers there is a benefit. Example: "MOVZX EAX, BL". (Note: EAX is the 32-bit AX register in 80386+). – Martin Rosenau Nov 23 '13 at 19:29
  • Hint: Remember how you did long division on paper? You can do long division in binary on paper the same way; just use base two arithmetic. It helps to do an example, on paper, *really*. With that in front of you, it should be obvious that partial results are shifted, and that you are comparing to the divisor (shift/compare/subtract). That's technically enough for you to puzzle it out. Doing this kind of division is well known to computer architects (after all, *they* have to implement it with gates and flip-flops!) and is well documented. Search for "multiprecision arithmetic". – Ira Baxter Nov 23 '13 at 21:28
  • Thanks Martin Rosenau & Ira Baxter – Ammar Alyousfi Nov 24 '13 at 10:23
  • @IraBaxter Can you give me an algorithm to do that. For example, the algorithm for the code above is : 1) Store the 8-bit Multiplicand in 16-bit register 2) Store the 8-bit Multiplier in 16-bit register 3) Clear intermediate result (di here) 4) Do steps from 5) to 7) eight times, using a counter, then goto step 8) 5) Shift one bit right the Multiplier to fill in the Carry the LSB 6) If Carry = 1 add the Multiplicand to the intermediate result 7) Shift one bit left the Multiplicand 8) Display the 16-bit result. – Ammar Alyousfi Nov 24 '13 at 14:46
  • @ammarx: Sure, I could do that. Or, you could go look it up; I gave you the perfect reference. Given that this seems to be homework, better that you work it out, and then implement it. Welcome to programming as a career. – Ira Baxter Nov 26 '13 at 03:41
  • For posteriority, you can also use a *magic number* if you know the divisor: https://ridiculousfish.com/blog/posts/labor-of-division-episode-i.html – Luiz Martins Nov 22 '20 at 13:29

1 Answers1

-5

You cannot divide one number to another using shifts and adding. Don't know who told you can do that. Shift right by one is equivalent to divinding by two. But you cannot divide to random number by shifting.

Stepan
  • 36
  • 4
  • 1
    If you implement a classic divide algorithm (from your 4th grade arithmetic class or more appropriately for software, from Knuth's Seminumerical algorithms), you WILL use shifts and adds. You need a compare instruction, too. – Ira Baxter Nov 23 '13 at 21:18