So I have a value in HL register pair and would like to multiply it by 64

The obvious solution would be

Code: Select all

```
ADD HL,HL ; * 2
ADD HL,HL ; * 4
ADD HL,HL ; * 8
ADD HL,HL ; * 16
ADD HL,HL ; * 32
ADD HL,HL ; * 64
```

Maybe people could share their tricks

So I have a value in HL register pair and would like to multiply it by 64

The obvious solution would be

Would be anything faster and shorter possible?

So I have a value in HL register pair and would like to multiply it by 64

The obvious solution would be

Code: Select all

```
ADD HL,HL ; * 2
ADD HL,HL ; * 4
ADD HL,HL ; * 8
ADD HL,HL ; * 16
ADD HL,HL ; * 32
ADD HL,HL ; * 64
```

0 x

I'm a machine code beginner, but is that not what the rotate and shift instructions are for?

http://sgate.emt.bme.hu/patai/publicati ... part4.html

http://sgate.emt.bme.hu/patai/publicati ... part4.html

0 x

- Einar Saukas
- Manic Miner
**Posts:**951**Joined:**Wed Nov 15, 2017 2:48 pm

If you want to multiply HL by 64, and store result into HL:

Code: Select all

```
xor a ; HL = hhhhhhhh llllllll, A = 00000000
rr h
rr l
rra ; HL = 0hhhhhhh hlllllll, A = l0000000
rr h
rr l
rra ; HL = 00hhhhhh hhllllll, A = ll000000
ld h,l
ld l,a ; HL = hhllllll ll000000
```

1 x

- Ast A. Moore
- Dynamite Dan
**Posts:**1309**Joined:**Mon Nov 13, 2017 3:16 pm

Fourteen T states faster.

0 x

Author of

and

not best solution, but simple way is to build lookup table 512b:

mul64 db low (N*64)

mul64+256 db high(N*64)

mul64 db low (N*64)

mul64+256 db high(N*64)

Code: Select all

```
;A=N
ld l,a
ld h,mul64/256
ld a,(hl)
inc h
ld h,(hl)
ld l,a
```

0 x

Multiplying by 64 is a shift left by 6 bits.

As these examples show, it's quicker to shift up by a whole byte (multiply by 256), then shift right by 2 bits (divide by 4).

As these examples show, it's quicker to shift up by a whole byte (multiply by 256), then shift right by 2 bits (divide by 4).

0 x