Prev: 41325 Up: Map Next: 41386
41345: 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 41345 PUSH BC Hold the first number.
41346 LD B,H
41347 LD C,L
41348 PUSH AF
41349 LD HL,0 The leftover starts at zero, for sixteen bits.
41352 LD A,B
41353 LD B,16
Try the next bit of the first number.
DivideWords_NextBit 41355 SLA C Bring that bit into the leftover.
41357 RLA
41358 ADC HL,HL
41360 SBC HL,DE Take the second number away from the leftover.
41362 JR C,DivideWords_Skip Jump to DivideWords_Skip when it does not fit.
DivideWords_Fits 41364 INC C Keep that bit in the result.
41365 DJNZ DivideWords_NextBit Decrease the bit counter by one and loop back to DivideWords_NextBit until the counter is zero.
41367 JR DivideWords_Done Jump to DivideWords_Done.
The second number did not fit.
DivideWords_Restore 41369 SLA C Bring the next bit into the leftover.
41371 RLA
41372 ADC HL,HL
41374 ADD HL,DE Add the second number back.
41375 JR C,DivideWords_Fits Jump to DivideWords_Fits when the leftover is too big for a word.
DivideWords_Skip 41377 DJNZ DivideWords_Restore Decrease the bit counter by one and loop back to DivideWords_Restore until the counter is zero.
41379 ADD HL,DE Add the second number back.
Nothing in the game uses this result.
DivideWords_Done 41380 EX DE,HL HL holds the result of the division, and DE holds what is left.
41381 LD H,A
41382 LD L,C
41383 POP AF Restore AF and BC from the stack.
41384 POP BC
41385 RET Return.
Prev: 41325 Up: Map Next: 41386