16bit division is the division of one 16bit value by another 16bit value, returning a 16bit quotient and a 16bit remainder. I used r1/r0 for dividend/remainder and r3/r2 for divisor/quotient.
Programming Tip: The number of bits in the quotient and the remainder can never be larger than the number of bits in the original divident. For example, if you are dividing a 16bit value by a 2bit value, both the quotient and the remainder must be able to handle a 16bit result. If you are dividing a 24bit value by a 16bit value, the quotient and remainder must both be able to handle a 24bit result.
So, again, let's remember how we did division in elementary school. For example, 179 divided by 8:
1 7 9 / 8 = 22 (quotient) 1 6  1 9 1 6  3 (remainder)
It's necessary necessary to follow this same process step by step. There is a 3digitdividend, so we expect 3 digits maximum for quotient. We "shift left" the divisor 2 digits (31) such that the number of digits in the divisor is the same as the number of digits in the dividend. So we get:
 1 7 9 / 8 0 0 = ? ? ?
We divide the two numbers, multiply the result by the divisor and substract this result from the dividend. In this first step 179 can't be divided by 800, so the the result is 0. We subtract 0 from 179 and still have 179:
1 7 9 / 8 0 0 = 0 ? ? 0  1 7 9
We then "shift right" the divisor 1 digit and repeat the process. 179 divided by 80 results in an answer of 2. After we subtract 160 (2x80) we are left with a remainder of 19:
1 7 9 / 8 0 = 0 2 ? 1 6 0  1 9
We repeat the process again until the divisor has shifted into its original position:
1 7 9 : 8 = 0 2 2 1 6 0  1 9 1 6  3
This may have been an unnecessary review of elementary school math, but it is important to remember exactly how the process is performed because we do exactly the same with the 8052 in binary system.
In this routine we will place the original dividend into R1 (highbyte) and R0 (lowbyte) and the divisor in R3 (highbyte) and R2 (lowbyte).
In the case of our example (179 divided by 8), the initial registers would be:

R1/R0 00000000 10110011
R3/R2 00000000 00001000
Step 1. Shift left the divisor.
MOV B,#00h ;Clear B since B will count the number of leftshifted bits div1: INC B ;Increment counter for each left shift MOV A,R2 ;Move the current divisor low byte into the accumulator RLC A ;Shift lowbyte left, rotate through carry to apply highest bit to highbyte MOV R2,A ;Save the updated divisor lowbyte MOV A,R3 ;Move the current divisor high byte into the accumulator RLC A ;Shift highbyte left high, rotating in carry from lowbyte MOV R3,A ;Save the updated divisor highbyte JNC div1 ;Repeat until carry flag is set from highbyte 
In the case of our example, once the above code is executed the registers will be as follows (including the carry bit 'C'):

C/R1/R0 0 00000000 10110011
C/R3/R2 1 00000000 00000000
At this point we can do the division itself. As we are in binary mode there is no need for a real divisionit's just a comparison. At this point it's important to know the steps from above.
Step 2. Shift left the divisor.
div2: ;Shift right the divisor MOV A,R3 ;Move highbyte of divisor into accumulator RRC A ;Rotate highbyte of divisor right and into carry MOV R3,A ;Save updated value of highbyte of divisor MOV A,R2 ;Move lowbyte of divisor into accumulator RRC A ;Rotate lowbyte of divisor right, with carry from highbyte MOV R2,A ;Save updated value of lowbyte of divisor CLR C ;Clear carry, we don't need it anymore MOV 07h,R1 ;Make a safe copy of the dividend highbyte MOV 06h,R0 ;Make a safe copy of the dividend lowbyte MOV A,R0 ;Move lowbyte of dividend into accumulator SUBB A,R2 ;Dividend  shifted divisor = result bit (no factor, only 0 or 1) MOV R0,A ;Save updated dividend MOV A,R1 ;Move highbyte of dividend into accumulator SUBB A,R3 ;Subtract highbyte of divisor (all together 16bit substraction) MOV R1,A ;Save updated highbyte back in highbyte of divisor JNC div3 ;If carry flag is NOT set, result is 1 MOV R1,07h ;Otherwise result is 0, save copy of divisor to undo subtraction MOV R0,06h div3: CPL C ;Invert carry, so it can be directly copied into result MOV A,R4 RLC A ;Shift carry flag into temporary result MOV R4,A MOV A,R5 RLC A MOV R5,A DJNZ B,div2 ;Now count backwards and repeat until "B" is zero 
To see how the loop works here are the registers after each step:
1 r1/0 00000000 10110011 ;dividend r3/2 10000000 00000000 ;divisor r5/4 00000000 00000000 ;result 2 r1/0 00000000 10110011 ;dividend r3/2 01000000 00000000 ;divisor r5/4 00000000 00000000 ;result ... 8 r1/0 00000000 10110011 ;dividend r3/2 00000001 00000000 ;divisor r5/4 00000000 00000000 ;result 9 r1/0 00000000 00110011 ;dividend r3/2 00000000 10000000 ;divisor r5/4 00000000 00000001 ;result 10 r1/0 00000000 00110011 ;dividend r3/2 00000000 01000000 ;divisor r5/4 00000000 00000010 ;result 11 r1/0 00000000 00010011 ;dividend r3/2 00000000 00100000 ;divisor r5/4 00000000 00000101 ;result 12 r1/0 00000000 00000011 ;dividend r3/2 00000000 00010000 ;divisor r5/4 00000000 00001011 ;result 13 r1/0 00000000 00000011 ;dividend r3/2 00000000 00001000 ;divisor r5/4 00000000 00010110 ;resultSTOP!
Register "B" is zero at this point. The remainder is already in R1/R0, and it is 3 decimal, same as above. The result is still in R5/R4, but we can see it's correct, too (10110b=22d). To finish the routine, we just "clean up" by moving R5/R4 to R3/R2.
Step 3. Final Cleanup.
MOV R3,05h ;Move result to R3/R2 MOV R2,04h ;Move result to R3/R2 
We used small numbers here for easier explanation. Of course it works also with 16bit numbers, that's what it was designed to do.
Taken as a whole, the above division algorithm can be converted into an easytouse function that can be called from your program. To call this function, you should preload R1/R0 with the high/low value to be divided, and R3/R2 with the high/low value that the number is to be divided by.
div16_16: CLR C ;Clear carry initially MOV R4,#00h ;Clear R4 working variable initially MOV R5,#00h ;CLear R5 working variable initially MOV B,#00h ;Clear B since B will count the number of leftshifted bits div1: INC B ;Increment counter for each left shift MOV A,R2 ;Move the current divisor low byte into the accumulator RLC A ;Shift lowbyte left, rotate through carry to apply highest bit to highbyte MOV R2,A ;Save the updated divisor lowbyte MOV A,R3 ;Move the current divisor high byte into the accumulator RLC A ;Shift highbyte left high, rotating in carry from lowbyte MOV R3,A ;Save the updated divisor highbyte JNC div1 ;Repeat until carry flag is set from highbyte div2: ;Shift right the divisor MOV A,R3 ;Move highbyte of divisor into accumulator RRC A ;Rotate highbyte of divisor right and into carry MOV R3,A ;Save updated value of highbyte of divisor MOV A,R2 ;Move lowbyte of divisor into accumulator RRC A ;Rotate lowbyte of divisor right, with carry from highbyte MOV R2,A ;Save updated value of lowbyte of divisor CLR C ;Clear carry, we don't need it anymore MOV 07h,R1 ;Make a safe copy of the dividend highbyte MOV 06h,R0 ;Make a safe copy of the dividend lowbyte MOV A,R0 ;Move lowbyte of dividend into accumulator SUBB A,R2 ;Dividend  shifted divisor = result bit (no factor, only 0 or 1) MOV R0,A ;Save updated dividend MOV A,R1 ;Move highbyte of dividend into accumulator SUBB A,R3 ;Subtract highbyte of divisor (all together 16bit substraction) MOV R1,A ;Save updated highbyte back in highbyte of divisor JNC div3 ;If carry flag is NOT set, result is 1 MOV R1,07h ;Otherwise result is 0, save copy of divisor to undo subtraction MOV R0,06h div3: CPL C ;Invert carry, so it can be directly copied into result MOV A,R4 RLC A ;Shift carry flag into temporary result MOV R4,A MOV A,R5 RLC A MOV R5,A DJNZ B,div2 ;Now count backwards and repeat until "B" is zero MOV R3,05h ;Move result to R3/R2 MOV R2,04h ;Move result to R3/R2 RET 
Previous: 16Bit Multiplication  Tutorial Contents  Next: Done! 