## Multiplication by 64

The place for codemasters or beginners to talk about programming any language for the Spectrum.
Ralf
Dynamite Dan
Posts: 1173
Joined: Mon Nov 13, 2017 11:59 am
Location: Poland

### Multiplication by 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

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
``````
Would be anything faster and shorter possible?
0 x

PeterJ
Posts: 1220
Joined: Thu Nov 09, 2017 7:19 pm
Location: Surrey, UK

### Re: Multiplication by 64

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
0 x

Einar Saukas
Manic Miner
Posts: 760
Joined: Wed Nov 15, 2017 2:48 pm

### Re: Multiplication by 64

Ralf wrote:
Sat Nov 03, 2018 7:02 pm
So I have a value in HL register pair and would like to multiply it by 64
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

Ralf
Dynamite Dan
Posts: 1173
Joined: Mon Nov 13, 2017 11:59 am
Location: Poland

### Re: Multiplication by 64

Is it faster Einar? Did you count tstates?
0 x

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

### Re: Multiplication by 64

Ralf wrote:
Sat Nov 03, 2018 8:08 pm
Is it faster Einar? Did you count tstates?
Fourteen T states faster.
0 x
Every man should plant a tree, build a house, and write a ZX Spectrum game.

Author of A Yankee in Iraq, a 50 fps shoot-’em-up—the first game to utilize the floating bus on the +2A/+3,
and zasm Z80 Assembler syntax highlighter.

g0blinish
Microbot
Posts: 124
Joined: Sun Jun 17, 2018 1:54 pm

### Re: Multiplication by 64

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

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

Joefish
Manic Miner
Posts: 481
Joined: Tue Nov 14, 2017 10:26 am

### Re: Multiplication by 64

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