Prev: 44567 Up: Map Next: 44657
44630: Calculate Square Root
Used by the routine at Position_Rope_End.
Calculates the square root of a 16-bit number, rounded down. It works eight times through, taking two bits of the number each time.
Input
HL Number
Output
L Square root (H is not meaningful)
Calculate_Square_Root 44630 LD DE,64 Set up the trial value: D builds up the root and E is 64.
44633 LD A,L Copy the low byte of the number to A.
44634 LD L,H Copy the high byte of the number to L.
44635 LD H,D Clear H.
44636 LD B,8 Set a counter in B for eight bits of the root.
44638 OR A Clear the carry flag.
Calculate_Square_Root_Loop 44639 SBC HL,DE Subtract the trial value (the root so far and 64) from the remainder.
44641 JR NC,Calculate_Square_Root_Fitted Jump to Calculate_Square_Root_Fitted if it fitted.
44643 ADD HL,DE Otherwise restore the remainder.
Calculate_Square_Root_Fitted 44644 CCF Invert the carry flag, so that it is set if the trial value fitted.
44645 RL D Shift the new bit into the root.
44647 ADD A,A Shift the next bit of the number out of A.
44648 ADC HL,HL Shift it into the remainder.
44650 ADD A,A Shift the next bit of the number out of A.
44651 ADC HL,HL Shift it into the remainder.
44653 DJNZ Calculate_Square_Root_Loop Decrease the bit counter and loop back until all eight bits of the root have been found.
44655 LD L,D Copy the root to L.
44656 RET Return.
Prev: 44567 Up: Map Next: 44657