Prev: A16D Up: Map Next: A1AA
A181: Divide Words
Divides the word in HL by the word in DE. Nothing in the game calls it.
Input
HL Dividend
DE Divisor
Output
HL Quotient
DE Remainder
Nothing in the game divides one word by another.
DivideWords A181 PUSH BC Hold the dividend.
A182 LD B,H
A183 LD C,L
A184 PUSH AF
A185 LD HL,$0000 The remainder starts at zero, for sixteen bits.
A188 LD A,B
A189 LD B,$10
Try the next bit of the dividend.
DivideWords_NextBit A18B SLA C Bring that bit into the remainder.
A18D RLA
A18E ADC HL,HL
A190 SBC HL,DE See whether the divisor fits.
A192 JR C,DivideWords_1
DivideWords_0 A194 INC C The quotient gains a bit, and the next bit follows.
A195 DJNZ DivideWords_NextBit
A197 JR DivideWords_2
The divisor did not fit.
DivideWords_Restore A199 SLA C Put the remainder back.
A19B RLA
A19C ADC HL,HL
A19E ADD HL,DE
A19F JR C,DivideWords_0 Try the next bit.
DivideWords_1 A1A1 DJNZ DivideWords_Restore
A1A3 ADD HL,DE
Nothing in the game uses this quotient.
DivideWords_2 A1A4 EX DE,HL Nothing in the game uses this quotient.
A1A5 LD H,A
A1A6 LD L,C
A1A7 POP AF Restore AF and BC from the stack.
A1A8 POP BC
A1A9 RET Return.
Prev: A16D Up: Map Next: A1AA