Multiply A * 48

Pagina 2/3
1 | | 3
jltursan wrote:
```ld l,a
ld h,0
ld l,a
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.

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).

RvS wrote:

Provided the value is in (hl)

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

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

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

The wonders of RLD!

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
ld h,0
ld l,a
add hl,hl		; HL = A[0..31] * 48 (69T)```

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
ld h,0
ld l,a
```

Compare with:

```add a,a  ; *2
...
```

@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)

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
ld h,0
ld l,a
```

Compare with:

```add a,a  ; *2
...
```

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

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 :-(

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.

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

Pagina 2/3
1 | | 3