Prev: 44136 Up: Map Next: 44186
44149: Divide
Used by the routines at Print_Completion_Percentage and Set_Up_Line.
Divides H multiplied by 256 by DE, one bit at a time. The quotient has eight bits, so DE must be greater than H for the answer to be right. The quotient is built up in the shadow registers and the working is done in the main registers, so the shadow registers are not preserved.
The line drawing code uses it to find how far to move across for each pixel moved down.
Input
H Dividend (it is multiplied by 256)
DE Divisor
Output
HL Quotient
DE Remainder
Divide 44149 LD L,H Copy the dividend to L.
44150 LD H,0 Clear H.
44152 EXX Switch to the shadow registers.
44153 LD HL,0 HL'=0 (the quotient so far).
44156 LD B,8 B'=8 (the number of bits).
Divide_Loop 44158 EXX Switch to the main registers.
44159 SLA L Shift the dividend left.
44161 RL H
44163 PUSH HL
44164 AND A
44165 SBC HL,DE Try to subtract the divisor.
44167 JR C,Divide_Restore Jump to Divide_Restore if it does not go.
44169 POP AF Discard the stashed dividend.
44170 EXX Switch to the shadow registers.
44171 ADD HL,HL Shift the quotient left.
44172 INC HL Add one for the bit.
Divide_Next 44173 DJNZ Divide_Loop Decrease the bit counter and loop back until all eight bits have been done.
44175 PUSH HL Stash the quotient on the stack.
44176 EXX Switch to the main registers.
44177 LD E,L Copy the remainder to DE.
44178 LD D,H Copy H to D.
44179 POP HL Restore HL from the stack.
44180 RET Restore the quotient from the stack.
Divide_Restore 44181 POP HL Restore the stashed dividend (the subtraction is undone).
44182 EXX Switch to the shadow registers.
44183 ADD HL,HL Shift the quotient left.
44184 JR Divide_Next Jump to Divide_Next.
View the equivalent code in;
Prev: 44136 Up: Map Next: 44186