- ;; Originally from GBDK by Pascal Felber.
- .area _CODE
-
-__divschar::
- push de
- push bc
- push ix
- ld ix,#0
- add ix,sp
-
- ld c,8(ix)
- ld e,9(ix)
- call .div8
-
- ld l,c
-
- pop ix
- pop bc
- pop de
- ret
-
-__modschar::
- push de
- push bc
- push ix
- ld ix,#0
- add ix,sp
-
- ld c,8(ix)
- ld e,9(ix)
- call .div8
-
- ld l,e
-
- pop ix
- pop bc
- pop de
- ret
-
-__divsint::
- push de
- push bc
- push ix
- ld ix,#0
- add ix,sp
-
- ld c,8(ix)
- ld b,9(ix)
- ld e,10(ix)
- ld d,11(ix)
- call .div16
-
- ld l,c
- ld h,b
-
- pop ix
- pop bc
- pop de
- ret
-
-__modsint::
- push de
- push bc
- push ix
- ld ix,#0
- add ix,sp
-
- ld c,8(ix)
- ld b,9(ix)
- ld e,10(ix)
- ld d,11(ix)
- call .div16
-
- ld l,e
- ld h,d
-
- pop ix
- pop bc
- pop de
- ret
-
- ;; Unsigned
-__divuchar::
- push de
- push bc
- push ix
- ld ix,#0
- add ix,sp
-
- ld c,8(ix)
- ld e,9(ix)
- call .divu8
-
- ld l,c
-
- pop ix
- pop bc
- pop de
- ret
-
-__moduchar::
- push de
- push bc
- push ix
- ld ix,#0
- add ix,sp
-
- ld c,8(ix)
- ld e,9(ix)
- call .divu8
-
- ld l,e
-
- pop ix
- pop bc
- pop de
- ret
-
-__divuint::
- push de
- push bc
- push ix
- ld ix,#0
- add ix,sp
-
- ld c,8(ix)
- ld b,9(ix)
- ld e,10(ix)
- ld d,11(ix)
- call .divu16
-
- ld l,c
- ld h,b
-
- pop ix
- pop bc
- pop de
- ret
-
-__moduint::
- push de
- push bc
- push ix
- ld ix,#0
- add ix,sp
-
- ld c,8(ix)
- ld b,9(ix)
- ld e,10(ix)
- ld d,11(ix)
- call .divu16
-
- ld l,e
- ld h,d
-
- pop ix
- pop bc
- pop de
- ret
-
-.div8::
-.mod8::
- LD A,C ; Sign extend
- RLCA
- SBC A
- LD B,A
- LD A,E ; Sign extend
- RLCA
- SBC A
- LD D,A
-
- ; Fall through to .div16
-
- ;; 16-bit division
- ;;
- ;; Entry conditions
- ;; BC = dividend
- ;; DE = divisor
- ;;
- ;; Exit conditions
- ;; BC = quotient
- ;; DE = remainder
- ;; If divisor is non-zero, carry=0
- ;; If divisor is 0, carry=1 and both quotient and remainder are 0
- ;;
- ;; Register used: AF,BC,DE,HL
-.div16::
-.mod16::
- ;; Determine sign of quotient by xor-ing high bytes of dividend
- ;; and divisor. Quotient is positive if signs are the same, negative
- ;; if signs are different
- ;; Remainder has same sign as dividend
- LD A,B ; Get high byte of dividend
- LD (.srem),A ; Save as sign of remainder
- XOR D ; Xor with high byte of divisor
- LD (.squot),A ; Save sign of quotient
- ;; Take absolute value of divisor
- BIT 7,D
- JR Z,.chkde ; Jump if divisor is positive
- SUB A ; Substract divisor from 0
- SUB E
- LD E,A
- SBC A ; Propagate borrow (A=0xFF if borrow)
- SUB D
- LD D,A
- ;; Take absolute value of dividend
+ ;; Originally from GBDK by Pascal Felber.
+
+ .area _CODE
+
+__divuint_rrx_s::
+ pop af
+ pop hl
+ pop de
+ push de
+ push hl
+ push af
+
+ jr __divu16
+
+__divsuchar_rrx_s::
+ ld hl,#2+1
+ add hl,sp
+
+ ld e,(hl)
+ dec hl
+ ld l,(hl)
+ xor a
+
+ jr signexte
+
+__modsuchar_rrx_s::
+ ld hl,#2+1
+ add hl,sp
+
+ ld e,(hl)
+ dec hl
+ ld l,(hl)
+ xor a
+
+ call signexte
+
+ ex de,hl
+ ret
+
+__divuschar_rrx_s::
+ ld hl,#2+1
+ add hl,sp
+
+ ld e,(hl)
+ ld d, #0
+ dec hl
+ ld l,(hl)
+
+ ld a,l ; Sign extend
+ rlca
+ sbc a
+ ld h,a
+
+ jr __div16
+
+__divschar_rrx_s::
+ ld hl,#2+1
+ add hl,sp
+
+ ld e,(hl)
+ dec hl
+ ld l,(hl)
+
+ ;; Fall through
+__divschar_rrx_hds::
+__div8::
+ ld a,l ; Sign extend
+ rlca
+ sbc a
+signexte:
+ ld h,a
+ ld a,e ; Sign extend
+ rlca
+ sbc a
+ ld d,a
+ ; Fall through to __div16
+
+ ;; signed 16-bit division
+ ;;
+ ;; Entry conditions
+ ;; HL = dividend
+ ;; DE = divisor
+ ;;
+ ;; Exit conditions
+ ;; HL = quotient
+ ;; DE = remainder
+ ;; If divisor is non-zero, carry=0
+ ;; If divisor is 0, carry=1 and both quotient and remainder are 0
+ ;;
+ ;; Register used: AF,BC,DE,HL
+__divsint_rrx_hds::
+__div16::
+ ;; Determine sign of quotient by xor-ing high bytes of dividend
+ ;; and divisor. Quotient is positive if signs are the same, negative
+ ;; if signs are different
+ ;; Remainder has same sign as dividend
+ ld a,h ; Get high byte of dividend
+ xor d ; Xor with high byte of divisor
+ rla ; Sign of quotient goes into the carry
+ ld a,h ; Get high byte of dividend
+ push af ; Save sign of both quotient and reminder
+
+ ;; Take absolute value of dividend
+ rla
+ jr NC,.chkde ; Jump if dividend is positive
+ sub a ; Substract dividend from 0
+ sub l
+ ld l,a
+ sbc a ; Propagate borrow (A=0xFF if borrow)
+ sub h
+ ld h,a
+ ;; Take absolute value of divisor