0 Members and 2 Guests are viewing this topic.
ld a,d xor h ;now the sign flag is set if the result needs to be negative or positive. push af xor h ;now a=d again, but we have the sign flag set appropriately jp p,$+9 ;if the sign is minus, negate DE xor a sub e ld e,a sbc a,a ;c flag is set, so this makes A=$FF sub d ld d,a ld a,h or a jp p,$+9 xor a sub l ld l,a sbc a,a ;c flag is set, so this makes A=$FF sub h ld h,a call HL_Div_DE ;return the result in HL pop af ret p xor a sub l ld l,a sbc a,a sub h ld h,a ret
;===============================================================HL_Div_BC_Signed:;===============================================================;Performs HL/BC;Speed: 1350-55a-2b; b is the number of set bits in the result; a is the number of leading zeroes in the absolute value of HL, minus 1; add 24 if HL is negative; add 19 if BC is negative; add 28 if the result is negative ;Size: 68 bytes;Inputs:; HL is the numerator; BC is the denominator;Outputs:; DE is the quotient; HL is the remainder; BC is not changed;Destroys:; A;=============================================================== ld a,h xor b push af;absHL xor b jp p,$+9 xor a \ sub l \ ld l,a sbc a,a \ sub h \ ld h,a;absBC: bit 7,b jr z,$+8 xor a \ sub c \ ld c,a sbc a,a \ sub b \ ld b,a ld de,0 adc hl,hl jr z,EndSDiv ld a,16 add hl,hl dec a jp nc,$-2 ex de,hl jp jumpinLoop1: add hl,bc ;--Loop2: dec a ;4 jr z,EndSDiv ;12|23 sla e ;-- rl d ;--jumpin: ; adc hl,hl ;15 sbc hl,bc ;15 jr c,Loop1 ;23-2b ;b is the number of bits in the absolute value of the result. inc e ;-- jp Loop2 ;--EndSDiv: pop af \ ret p xor a \ sub e \ ld e,a sbc a,a \ sub d \ ld d,a ret
;===============================================================HL_Div_BC_Signed:;===============================================================;Performs HL/BC;Speed: 1315-2b cycles; **same cycles as the regular HL_Div_BC; add 24 if HL is negative; add 24 if BC is negative; add 28 if only one is negative ;Size: 60 bytes;Inputs:; HL is the numerator; BC is the denominator;Outputs:; DE is the quotient; HL is the remainder; BC is not changed; A is 0; z flag is set; c flag is reset;=============================================================== ld a,h xor b push af;absHL xor b jp p,$+9 xor a \ sub l \ ld l,a sbc a,a \ sub h \ ld h,a;absBC: xor b jp p,$+9 xor a \ sub c \ ld c,a sbc a,a \ sub b \ ld b,a add hl,hl ld a,15 ld de,0 ex de,hl jp jumpin;89+15*80+26 = 1315-2bDiv_Loop_1: add hl,bc ;--Loop2: dec a ;4 jr z,EndSDiv ;12|7jumpin: sla e ;8 rl d ;8 adc hl,hl ;15 sbc hl,bc ;15 jr c,Loop1 ;23-2b inc e ;-- jp Loop2 ;--EndSDiv: pop af \ ret p xor a \ sub e \ ld e,a sbc a,a \ sub d \ ld d,a ret