Prev: A311 Up: Map Next: A39E
A379: Divide
Used by the routine at DrawLine.
Divides a number by another and returns the answer as a fraction of 0100 which is a number between 00 and FF as long as the first number is the smaller. The answer is worked out one bit at a time (long division), with the answer in the shadow registers and the working in the normal registers. DrawLine uses it to work out how far to move across or up for every pixel moved in the other direction.
Input
H The number to divide (the numerator)
DE The number to divide by (the denominator)
Output
DE The remainder
HL The answer (the numerator * 0100 / the denominator)
Divide A379 LD L,H Move the numerator into L.
A37A LD H,$00 H=00 (so that HL is the numerator).
A37C EXX Switch to the shadow registers.
A37D LD HL,$0000 HL'=00 (the answer so far).
A380 LD B,$08 B'=08 (the number of bits to work out).
Divide_Loop A382 EXX Switch to the normal registers.
A383 SLA L Shift HL left by one.
A385 RL H Rotate the carry into H.
A387 PUSH HL Stash the working number on the stack.
A388 AND A Subtract the denominator.
A389 SBC HL,DE
A38B JR C,Divide_Restore Jump to Divide_Restore if it is bigger than the working number.
A38D POP AF Discard the stashed working number, as the subtraction can stay.
A38E EXX Switch to the shadow registers.
A38F ADD HL,HL Shift the answer left by one.
A390 INC HL Add one to the answer for this bit.
Divide_Next A391 DJNZ Divide_Loop Decrease the bit counter by one and loop back to Divide_Loop until all eight bits of the answer have been worked out.
A393 PUSH HL Stash the answer on the stack.
A394 EXX Switch to the normal registers.
A395 LD E,L E=L (the low byte of the remainder).
A396 LD D,H D=H (the high byte of the remainder).
A397 POP HL Fetch the answer.
A398 RET Return.
Divide_Restore A399 POP HL Restore the working number from the stack.
A39A EXX Switch to the shadow registers.
A39B ADD HL,HL Shift the answer left by one.
A39C JR Divide_Next Jump to Divide_Next to move on to the next bit.
View the equivalent code in;
Prev: A311 Up: Map Next: A39E