Prev: 8000 Up: Map Next: 8214
80E8: Divide
Input
HL Dividend
DE Divisor
Output
BC Quotient
Divide 80E8 LD B,$00 Set the sign to 00 in B.
80EA BIT 7,H Jump to Divide_0 if the dividend is positive.
80EC JR Z,Divide_0
80EE LD A,H Negate the dividend.
80EF CPL
80F0 LD H,A
80F1 LD A,L
80F2 CPL
80F3 LD L,A
80F4 INC HL
80F5 INC B Increment the sign by one.
Divide_0 80F6 LD A,B Copy the sign to A.
80F7 LD BC,$0000 Clear the quotient.
80FA EX AF,AF' Stash the sign in AF'.
80FB ADD HL,HL Shift the dividend left by four.
80FC ADD HL,HL
80FD ADD HL,HL
80FE ADD HL,HL
80FF LD A,L Move the low byte of the dividend into A.
8100 LD L,H Move the high byte of the dividend into L.
8101 LD H,$00 Clear the high byte of the dividend.
Unrolled restoring divide into the quotient.
8103 RLA Jump to Divide_14 if this remainder bit does not fit.
8104 ADC HL,HL
8106 SBC HL,DE
8108 JP C,Divide_14
810B SET 3,B Set bit 3 of the quotient.
810D RLA Jump to Divide_15 if this remainder bit does not fit.
810E ADC HL,HL
8110 SBC HL,DE
8112 JP C,Divide_15
Divide_1 8115 SET 2,B Set bit 2 of the quotient.
8117 RLA Jump to Divide_16 if this remainder bit does not fit.
8118 ADC HL,HL
811A SBC HL,DE
811C JP C,Divide_16
Divide_2 811F SET 1,B Set bit 1 of the quotient.
8121 RLA Jump to Divide_17 if this remainder bit does not fit.
8122 ADC HL,HL
8124 SBC HL,DE
8126 JP C,Divide_17
Divide_3 8129 SET 0,B Set bit 0 of the quotient.
812B XOR A Clear A.
812C RLA Jump to Divide_18 if this remainder bit does not fit.
812D ADC HL,HL
812F SBC HL,DE
8131 JP C,Divide_18
Divide_4 8134 SET 7,C Set bit 7 of the quotient.
8136 RLA Jump to Divide_19 if this remainder bit does not fit.
8137 ADC HL,HL
8139 SBC HL,DE
813B JP C,Divide_19
Divide_5 813E SET 6,C Set bit 6 of the quotient.
8140 RLA Jump to Divide_20 if this remainder bit does not fit.
8141 ADC HL,HL
8143 SBC HL,DE
8145 JP C,Divide_20
Divide_6 8148 SET 5,C Set bit 5 of the quotient.
814A RLA Jump to Divide_21 if this remainder bit does not fit.
814B ADC HL,HL
814D SBC HL,DE
814F JP C,Divide_21
Divide_7 8152 SET 4,C Set bit 4 of the quotient.
8154 RLA Jump to Divide_22 if this remainder bit does not fit.
8155 ADC HL,HL
8157 SBC HL,DE
8159 JP C,Divide_22
Divide_8 815C SET 3,C Set bit 3 of the quotient.
815E RLA Jump to Divide_23 if this remainder bit does not fit.
815F ADC HL,HL
8161 SBC HL,DE
8163 JP C,Divide_23
Divide_9 8166 SET 2,C Set bit 2 of the quotient.
8168 RLA Jump to Divide_24 if this remainder bit does not fit.
8169 ADC HL,HL
816B SBC HL,DE
816D JP C,Divide_24
Divide_10 8170 SET 1,C Set bit 1 of the quotient.
8172 RLA Jump to Divide_12 if this remainder bit does not fit.
8173 ADC HL,HL
8175 SBC HL,DE
8177 JP C,Divide_12
Divide_11 817A SET 0,C Set bit 0 of the quotient.
Restore the sign of the quotient.
Divide_12 817C EX AF,AF' Restore the sign from AF'.
817D AND A Jump to Divide_13 if the sign is positive.
817E JR Z,Divide_13
8180 LD A,C Negate the quotient.
8181 CPL
8182 LD C,A
8183 LD A,B
8184 CPL
8185 LD B,A
8186 INC BC
Divide_13 8187 RET Return.
Restore the remainder after a failed subtraction.
Divide_14 8188 RLA Restore the remainder.
8189 ADC HL,HL
818B ADD HL,DE
818C JP C,Divide_1 Jump to Divide_1 if carry is set.
Divide_15 818F RLA Restore the remainder.
8190 ADC HL,HL
8192 ADD HL,DE
8193 JP C,Divide_2 Jump to Divide_2 if carry is set.
Divide_16 8196 RLA Restore the remainder.
8197 ADC HL,HL
8199 ADD HL,DE
819A JP C,Divide_3 Jump to Divide_3 if carry is set.
Divide_17 819D XOR A Clear A.
819E RLA Restore the remainder.
819F ADC HL,HL
81A1 ADD HL,DE
81A2 JP C,Divide_4 Jump to Divide_4 if carry is set.
Divide_18 81A5 RLA Restore the remainder.
81A6 ADC HL,HL
81A8 ADD HL,DE
81A9 JP C,Divide_5 Jump to Divide_5 if carry is set.
Divide_19 81AC RLA Restore the remainder.
81AD ADC HL,HL
81AF ADD HL,DE
81B0 JP C,Divide_6 Jump to Divide_6 if carry is set.
Divide_20 81B3 RLA Restore the remainder.
81B4 ADC HL,HL
81B6 ADD HL,DE
81B7 JP C,Divide_7 Jump to Divide_7 if carry is set.
Divide_21 81BA RLA Restore the remainder.
81BB ADC HL,HL
81BD ADD HL,DE
81BE JP C,Divide_8 Jump to Divide_8 if carry is set.
Divide_22 81C1 RLA Restore the remainder.
81C2 ADC HL,HL
81C4 ADD HL,DE
81C5 JP C,Divide_9 Jump to Divide_9 if carry is set.
Divide_23 81C8 RLA Restore the remainder.
81C9 ADC HL,HL
81CB ADD HL,DE
81CC JP C,Divide_10 Jump to Divide_10 if carry is set.
Divide_24 81CF RLA Restore the remainder.
81D0 ADC HL,HL
81D2 ADD HL,DE
81D3 JP C,Divide_11 Jump to Divide_11 if carry is set.
81D6 JP Divide_12 Jump to Divide_12.
81D9 NOP
81DA NOP
81DB NOP
81DC NOP
This entry point is used by the routine at Test_In_View.
Divide_25 81DD PUSH HL
81DE LD C,(HL)
81DF INC HL
81E0 LD B,(HL)
81E1 INC HL
81E2 LD D,(HL)
81E3 INC HL
81E4 IN A,(C)
81E6 OR $E0
81E8 INC A
81E9 POP HL
81EA RET Z
81EB CP D
81EC INC HL
81ED INC HL
81EE INC HL
81EF RET Z
81F0 LD HL,($E17B)
81F3 RET
81F4 NOP
81F5 NOP
81F6 NOP
81F7 NOP
81F8 NOP
81F9 NOP
81FA NOP
81FB NOP
81FC NOP
81FD NOP
81FE NOP
81FF NOP
8200 NOP
8201 NOP
8202 NOP
8203 NOP
8204 NOP
8205 NOP
8206 NOP
8207 NOP
8208 NOP
8209 NOP
820A NOP
820B NOP
820C NOP
820D NOP
820E NOP
820F NOP
8210 NOP
8211 NOP
8212 NOP
8213 NOP
Prev: 8000 Up: Map Next: 8214