1 ;; Originally from GBDK by Pascal Felber.
185 ; Fall through to .div16
196 ;; If divisor is non-zero, carry=0
197 ;; If divisor is 0, carry=1 and both quotient and remainder are 0
199 ;; Register used: AF,BC,DE,HL
202 ;; Determine sign of quotient by xor-ing high bytes of dividend
203 ;; and divisor. Quotient is positive if signs are the same, negative
204 ;; if signs are different
205 ;; Remainder has same sign as dividend
206 LD A,B ; Get high byte of dividend
207 LD (.srem),A ; Save as sign of remainder
208 XOR D ; Xor with high byte of divisor
209 LD (.squot),A ; Save sign of quotient
210 ;; Take absolute value of divisor
212 JR Z,.chkde ; Jump if divisor is positive
213 SUB A ; Substract divisor from 0
216 SBC A ; Propagate borrow (A=0xFF if borrow)
219 ;; Take absolute value of dividend
222 JR Z,.dodiv ; Jump if dividend is positive
223 SUB A ; Substract dividend from 0
226 SBC A ; Propagate borrow (A=0xFF if borrow)
229 ;; Divide absolute values
232 RET C ; Exit if divide by zero
233 ;; Negate quotient if it is negative
236 JR Z,.dorem ; Jump if quotient is positive
237 SUB A ; Substract quotient from 0
240 SBC A ; Propagate borrow (A=0xFF if borrow)
244 ;; Negate remainder if it is negative
247 RET Z ; Return if remainder is positive
248 SUB A ; Substract remainder from 0
251 SBC A ; Propagate remainder (A=0xFF if borrow)
260 ; Fall through to divu16
264 ;; Check for division by zero
267 JR NZ,.divide ; Branch if divisor is non-zero
268 LD BC,#0x00 ; Divide by zero error
271 SCF ; Set carry, invalid result
274 LD L,C ; L = low byte of dividend/quotient
275 LD H,B ; H = high byte of dividend/quotient
276 LD BC,#0x00 ; BC = remainder
277 OR A ; Clear carry to start
278 LD A,#16 ; 16 bits in dividend
280 ;; Shift next bit of quotient into bit 0 of dividend
281 ;; Shift next MSB of dividend into LSB of remainder
282 ;; BC holds both dividend and quotient. While we shift a bit from
283 ;; MSB of dividend, we shift next bit of quotient in from carry
284 ;; HL holds remainder
285 ;; Do a 32-bit left shift, shifting carry to L, L to H,
288 RL L ; Carry (next bit of quotient) to bit 0
289 RL H ; Shift remaining bytes
291 RL B ; Clears carry since BC was 0
292 ;; If remainder is >= divisor, next bit of quotient is 1. This
294 PUSH BC ; Save current remainder
295 LD A,C ; Substract divisor from remainder
301 CCF ; Complement borrow so 1 indicates a
302 ; successful substraction (this is the
303 ; next bit of quotient)
304 JR C,.drop ; Jump if remainder is >= dividend
305 POP BC ; Otherwise, restore remainder
312 DEC A ; DEC does not affect carry flag
314 ;; Shift last carry bit into quotient
315 LD D,B ; DE = remainder
318 LD C,L ; C = low byte of quotient
320 LD B,H ; B = high byte of quotient
321 OR A ; Clear carry, valid result
327 .ds 0x01 ; Sign of quotient
329 .ds 0x01 ; Sign of remainder
331 .ds 0x01 ; Counter for division