0 Members and 3 Guests are viewing this topic.
mult_de_bc ld hl, 0 sla e ; optimised 1st iteration rl d jr nc, $+4 ld h, b ld l, c ld a, 15_loop: add hl, hl rl e rl d jr nc, $+6 add hl, bc jr nc, $+3 inc de dec a jr nz, _loop ret
div_ac_de: ld hl, 0 ld b, 16_loop: sll c rla adc hl, hl sbc hl, de jr nc, $+4 add hl, de dec c djnz _loop ret
2 3 +6 9--------
2 3 +6 9-------- 12
1 2 3 +6 9-------- 2
1 2 3 +6 9-------- 9 2
86 120 +188 233----------
86 120 +188 233---------- 353
1 86 120 +188 233---------- 97
1 86 120 +188 233---------- 275 97
1 86 120 +188 233---------- 1 19 97
;HL + DE add hl, de;HL - DE or a ;clear carry flag sbc hl, de
negHL: push de ex de, hl ld hl, 0 or a sbc hl, de pop de ret
xor a sub l ld l,a sbc a,a sub h ld h,a
DivFP: ;IN: hl, de ;OUT: hl = hl / de bit 7, h jr nz, _DivFP_FirstNeg bit 7, d jr nz, _DivFP_AnsNeg jr z, _DivFP_AnsPos_DivFP_FirstNeg: bit 7, h jr nz, _DivFP_AnsPos_DivFP_AnsNeg: ld a, 1 push af jr DivFP_Cont_DivFP_AnsPos: ld a, 0 push af_DivFP_Cont res 7, h res 7, d ld a, h ld h, l ld l, 0 call Div24by16 pop af cp 1 call z, NegHL retDiv24by16: push hl ;INPUTS: ahl = dividend de = divisor ;1 pop ix ;OUTPUTS: ahl = quotient de = divisor ;0 ld hl,0 ld b,24_Div24by16loop: add ix,ix rla adc hl,hl jr c,_Div24by16setbit or a sbc hl,de add hl,de jr c,_Div24by16skip_Div24by16setbit: or a sbc hl,de inc ix_Div24by16skip: djnz _Div24by16loop push ix ;1 pop hl ;0 retNegHL: xor a sub l ld l,a sbc a,a sub h ld h,a
MulFP: ;Multiplies 2 16bit fixed-point numbers ;IN: de, bc ;OUT: de * bc in hl ;DESTROYS: bit 7, d jr nz, _MulFP_FirstNeg bit 7, b jr nz, _MulFP_AnsNeg jr z, _MulFP_AnsPos_MulFP_FirstNeg: bit 7, b jr nz, _MulFP_AnsPos_MulFP_AnsNeg: ld a, 1 push af jr _MulFP_Cont_MulFP_AnsPos: ld a, 0 push af_MulFP_Cont res 7, b res 7, d call Mul16 ld l, h ld h, e pop af cp 1 call z, NegHL retMul16: ; DEHL=BC*DE ld hl,0 ld a,16Mul16Loop: add hl,hl rl e rl d jp nc,NoMul16 add hl,bc jp nc,NoMul16 inc de ; This instruction (with the jump) is like an "ADC DE,0"NoMul16: dec a jp nz,Mul16Loop retNegHL: xor a sub l ld l,a sbc a,a sub h ld h,a ret
All 1 byte numbers: 0 = 00000000 1 = 00000001 57 = 00111001127 = 01111111220 = 11011100253 = 11111101254 = 11111110255 = 11111111256 = 00000000 ;it carried back to 0
One byte again: 127 = 01111111 15 = 00001111 2 = 00000010 1 = 00000001 0 = 00000000 -1 = 11111111 ;just like the looping up above, but in reverse -2 = 11111110 -3 = 11111101 -4 = 11111100 -57 = 11000111-126 = 10000010-127 = 10000001-128 = 10000000
DivFP: ;IN: hl, de ;OUT: hl = hl / de bit 7, d ;start of signed stuff jr nz, _DivFP_FirstNeg bit 7, h jr nz, _DivFP_AnsNeg jr z, _DivFP_AnsPos_DivFP_FirstNeg: bit 7, h jr nz, _DivFP_AnsPos_DivFP_AnsNeg: ld a, 1 push af jr _DivFP_Cont_DivFP_AnsPos: ld a, 0 push af_DivFP_Cont: bit 7, h jr z, _DivFP_HLPos call NegHL_DivFP_HLPos: bit 7, d jr z, _DivFP_DEPos call NegDE_DivFP_DEPos ;end of signed stuff and start of the unsigned divide ld a, h ld h, l ld l, 0 call Div24by16 pop af cp 1 call z, NegHL retDiv24by16: push hl ;INPUTS: ahl = dividend de = divisor ;1 pop ix ;OUTPUTS: ahl = quotient de = divisor ;0 ld hl,0 ld b,24_Div24by16loop: add ix,ix rla adc hl,hl jr c,_Div24by16setbit or a sbc hl,de add hl,de jr c,_Div24by16skip_Div24by16setbit: or a sbc hl,de inc ix_Div24by16skip: djnz _Div24by16loop push ix ;1 pop hl ;0 retNegHL: xor a sub l ld l,a sbc a,a sub h ld h,a retNegBC: xor a sub c ld c,a sbc a,a sub b ld b,a retNegDE: xor a sub e ld e,a sbc a,a sub d ld d,a ret