0 Members and 1 Guest are viewing this topic.
Quote from: Xeda112358 on October 11, 2011, 08:24:12 pmSo if you want to delay for approximately 'a' seconds you can try this:Code: [Select] ld b,107 halt djnz $-1 dec a jr nz,$-6 retDoes this help?Remember to have interrupts enabled.
So if you want to delay for approximately 'a' seconds you can try this:Code: [Select] ld b,107 halt djnz $-1 dec a jr nz,$-6 retDoes this help?
ld b,107 halt djnz $-1 dec a jr nz,$-6 ret
A_Times_DE:;Input:; A,DE;Outputs:; A is 0; BC is not changed; DE is not changed; HL is the result; z flag is set; c flag is set if the input A is not 0;Notes:; If A is 0, 29 cycles;Speed: 145+6n+21b cycles; n=floor(log(a)/log(2)); b is the number of bits in the number; Testing over all values of A from 1 to 255:; 313.7058824 average cycles; Worst: 355; Best : 166 (non trivial);Size: 25 bytes ld hl,0 ;10 or a \ ret z ;9 cpl \ scf ;8 adc a,a ;4 jp nc,$+7 ;10 ;45Loop: add a,a ;4 jp c,$-1 ;10 ;14(7-n) add hl,de ;11 ;11 (the rest are counted below) add a,a ;4 ;4b ret z ;5|11 ;5b+6 add hl,hl ;11 ;11b-11 jp p,$-4 ;21|20 ;20n+b jp $-7
DE_Times_A:;Inputs:; DE and A are factors;Outputs:; A is not changed; B is 0; C is not changed; DE is not changed; HL is the product;Speed:; 342+6x, x is the number of bits in A; Average: 366 cycles;Size:; 13 bytes ld b,8 ;7 7 ld hl,0 ;10 10 add hl,hl ;11*8 88 rlca ;4*8 32 jr nc,$+3 ;(12|18)*8 96+6x add hl,de ;-- -- djnz $-5 ;13*7+8 99 ret ;10 10
Okay, so Runer was speculating about how to get the best speed out of a math routine, so the first challenge he gave was for 8x8 multiplication with a 16-bit output. I am not doing all that well with the challenge, but here is a variation of what I came up with that actually is a 8x16 multiplication (it requires 4 more cycles to make it 8x8).
ld hl,0 ; 10 or a ; 4 ret z ; 5 scf ; 4skip_zeroes: adc a,a ; 4(9-n) jr nc,skip_zeroes ; 12(9-n) - 5 jp loop_add0 ; 10loop_add: ret z ; 5k + 6 add hl,hl ; 11kloop_add0: add hl,de ; 11(k+1)loop_noadd: add a,a ; 4n jr c, loop_add ; 7n + 5k add hl,hl ; 11(n-k-1) jp loop_noadd ; 10(n-k-1)
; Ref Worst BestMultHbyE: ld l, 0 ; 7 7 7 ld d, l ; 4 4 4 sla h ; 8 8 8 jr nc,$+3 ; 12/7 12 7 ld l,e ; 4 4 ld b, 7 ; 7 7 7_: add hl,hl ; 11 11 11 jr nc,$+3 ; 12/7 7 12 add hl,de ; 11 11 djnz -_ ; 13/8 13 13 ; -5 -5 ; 327 284MultAbyDE: ld hl, 0 ; 10 10 10 ld c, l ; 4 4 4 add a,a ; 4 4 4 jr nc,$+4 ; 12/7 7 12 ld h,d ; 4 4 ld l,e ; 4 4 ld b, 7 ; 7 7 7_: add hl,hl ; 11 11 11 rla ; 4 4 4 jr nc,$+4 ; 12/7 7 12 add hl,de ; 11 11 adc a,c ; 4 4 djnz -_ ; 13/8 13 13 ; -5 -5 ; 385 312
MultHbyE:; L and D must already be 0 sla h ; 8 8 8 jr nc,$+3 ; 12/7 12 7 ld l,e ; 4 4 add hl,hl ; 11 11 11 jr nc,$+3 ; 12/7 7 12 add hl,de ; 11 77 ; *7 *7 *7 ; 223 180MultAbyDE:; HL and C must already be 0 add a,a ; 4 4 4 jr nc,$+4 ; 12/7 7 12 ld h,d ; 4 4 ld l,e ; 4 4 add hl,hl ; 11 11 11 rla ; 4 4 4 jr nc,$+4 ; 12/7 7 12 add hl,de ; 11 11 adc a,c ; 4 4 ; *7 *7 *7 ; 278 205