Prev: A16D Up: Map Next: A1AA
A181: Divide Words
HL=HL/DE. Nothing in the game calls it.
Input
HL First number
DE Second number
Output
HL HL/DE
DE What is left after the division
Nothing in the game divides one word by another.
DivideWords A181 PUSH BC Hold the first number.
A182 LD B,H
A183 LD C,L
A184 PUSH AF
A185 LD HL,$0000 The leftover starts at zero, for sixteen bits.
A188 LD A,B
A189 LD B,$10
Try the next bit of the first number.
DivideWords_NextBit A18B SLA C Bring that bit into the leftover.
A18D RLA
A18E ADC HL,HL
A190 SBC HL,DE Take the second number away from the leftover.
A192 JR C,DivideWords_Skip Jump to DivideWords_Skip when it does not fit.
DivideWords_Fits A194 INC C Keep that bit in the result.
A195 DJNZ DivideWords_NextBit Decrease the bit counter by one and loop back to DivideWords_NextBit until the counter is zero.
A197 JR DivideWords_Done Jump to DivideWords_Done.
The second number did not fit.
DivideWords_Restore A199 SLA C Bring the next bit into the leftover.
A19B RLA
A19C ADC HL,HL
A19E ADD HL,DE Add the second number back.
A19F JR C,DivideWords_Fits Jump to DivideWords_Fits when the leftover is too big for a word.
DivideWords_Skip A1A1 DJNZ DivideWords_Restore Decrease the bit counter by one and loop back to DivideWords_Restore until the counter is zero.
A1A3 ADD HL,DE Add the second number back.
Nothing in the game uses this result.
DivideWords_Done A1A4 EX DE,HL HL holds the result of the division, and DE holds what is left.
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