Multiply A * 48

Pagina 2/3
1 | | 3

Van Briqunullus

Champion (355)

afbeelding van Briqunullus

22-03-2021, 21:50

jltursan wrote:
ld l,a			
add a,a			
add a,l			
ld h,0			
ld l,a			
add hl,hl		
add hl,hl		
add hl,hl		
add hl,hl		; HL = A * 48 (76T)

If A < 32 then you could do one more add a,a after add a,l and remove one add hl,hl.

Van pgimeno

Champion (289)

afbeelding van pgimeno

23-03-2021, 00:47

NYYRIKKI wrote:
Manuel wrote:

oh, repeated add hl, hl is faster than shifting?

Yes, ADD HL,HL is 11(+1) cycles while SLA L & RL H is 16(+2) cycles.

I think you mean 16(+4).

Van Metalion

Paragon (1375)

afbeelding van Metalion

23-03-2021, 08:50

RvS wrote:

Provided the value is in (hl)

xor a
rld
ld l,(hl)
ld h,a
ld d,h
ld e,l
add hl,hl
add hl,de

In 72 cycles. Not as fast, but no memory usage.

At last a useful usage of RLD/RRD !!
Thank you.

Van jltursan

Prophet (2520)

afbeelding van jltursan

23-03-2021, 09:14

The wonders of RLD! Big smile

Quote:

If A < 32 then you could do one more add a,a after add a,l and remove one add hl,hl.

True, a bit faster:

ld l,a			
add a,a	
add a,a		
add a,l			
ld h,0			
ld l,a			
add hl,hl		
add hl,hl		
add hl,hl		; HL = A[0..31] * 48 (69T)

Van pgimeno

Champion (289)

afbeelding van pgimeno

23-03-2021, 10:11

Careful, the 'add a,l' should be between the other two 'add a,a'. Otherwise you're multiplying by 5, not by 6.

ld l,a
add a,a  ; *2
add a,l  ; *3
add a,a  ; *6
ld h,0
ld l,a
add hl,hl  ; *12
add hl,hl  ; *24
add hl,hl  ; *48

Compare with:

add a,a  ; *2
add a,a  ; *4
add a,l  ; *5
...

Van santiontanon

Paragon (1418)

afbeelding van santiontanon

23-03-2021, 14:35

@Metalion Haha, exactly!!! I've been trying to find a use for those instructions for a long time! (other than the deprecated original intent of BCD numbers, of course)

Van jltursan

Prophet (2520)

afbeelding van jltursan

23-03-2021, 15:19

pgimeno wrote:

Careful, the 'add a,l' should be between the other two 'add a,a'. Otherwise you're multiplying by 5, not by 6.

ld l,a
add a,a  ; *2
add a,l  ; *3
add a,a  ; *6
ld h,0
ld l,a
add hl,hl  ; *12
add hl,hl  ; *24
add hl,hl  ; *48

Compare with:

add a,a  ; *2
add a,a  ; *4
add a,l  ; *5
...

Right!, same speed; but different result Big smile. I haven't tested yet so this saves me some head scratching, thanks!

Van Bengalack

Champion (390)

afbeelding van Bengalack

24-03-2021, 12:44

Metalion wrote:

At last a useful usage of RLD/RRD !!

Kind of nice, but the fact that it works on *(hl)* and not *hl*, limits the use severely, as far as I see it. The operation will modify memory and not only registers, (the latter) which normally would be what I want :-(

Van Grauw

Ascended (9904)

afbeelding van Grauw

24-03-2021, 13:04

I can’t fathom why the Z80 engineers would make complex special instructions like RLD / RRD, rather than implement something useful like stack-relative indexing or even nibble swapping. I guess BCD support was a big thing for processor marketing in the 70s.

Van jltursan

Prophet (2520)

afbeelding van jltursan

24-03-2021, 14:27

RLD could be great to perform a 4px scroll given a RAM buffer...Smile

Pagina 2/3
1 | | 3