__udivmodqi4 Demonstration and confirmation - AVR Assembly division binary software arithmetic
Division using addition. This could be very hard for a normal person to understand, and here it's demonstrated that it works. It seems complicated. How does it really work? Division works on a Pizza as much on a computer with binary data, it works exactly the same.
0b00001111 / 0b00000011
1, b00001111 dividend is shifted to the reminder via the carry-flag or 9-th bit;
2, if reminder is more than or equal to divisor, then C-flag is cleared, and reminder is updated, and a 1 inserted to the bit position of the quotient.
3, 9 loops later, the result is calculated. Why 9 loops. It took 9 shifts to completely shift out 8 bit;
=========================================================
Here is a simplified version for dummies like you!!
0xff/0x0f= 0b11111111/0b00001111
1:
quotient = 0b11111110
remainder = 0b00000001
divisor bigger than remainder? (yes)
2:
quotient = 0b11111100
remainder = 0b00000011
divisor bigger than remainder? (yes)
3:
quotient = 0b11111000
remainder = 0b00000111
divisor bigger than remainder? (yes)
4:
quotient = 0b11110000
remainder = 0b00001111
divisor bigger than remainder? (no) same
remainder = remainder - divisor = 0 (carry-set)
5:
quotient = 0b11100001 (carry saved here)
remainder = 0b00000001
divisor bigger than remainder? (yes))
6:
quotient = 0b11000010
remainder = 0b00000011
divisor bigger than remainder? (yes))
7:
quotient = 0b10000100
remainder = 0b00000111
divisor bigger than remainder? (yes))
8:
quotient = 0b00001000
remainder = 0b00001111
divisor bigger than remainder? (no) same
remainder = remainder - divisor = 0 (carry-set)
9:
quotient = 0b00010001 = 17 (done and done)
remainder = 0b00000000
So you see, the quotient's 9th bit is push to the remainder, and when remainder is equal or more than divisor, the corresponding quotient bit is set, and the remainder is updated. It's fairly simple, and can be applied to bigger bit division.