Prev: 81AD Up: Map Next: 81F2
81E6: Divide by repeated subtraction
Divides HL by DE by repeated subtraction, returning the ASCII digit of the quotient in A and the remainder in HL. Z is set if the digit is '0'.
Input
HL Number
DE Divisor
Output
A Digit ("0"-"9")
HL Remainder
81E6 LD A,$2F A will become the ASCII digit ("0"-1, since the loop below starts by incrementing it)
81E8 INC A One more digit counted
81E9 AND A Subtract the divisor from what is left...
81EA SBC HL,DE
81EC JR NC,$81E8 ...as many times as it will go
81EE ADD HL,DE Put back the one subtraction that went too far
81EF CP $30 Is the digit "0" (Z reflects whether it is)?
81F1 RET
Prev: 81AD Up: Map Next: 81F2