Multiplication by 64

The place for codemasters or beginners to talk about programming any language for the Spectrum.
Post Reply
Ralf
Manic Miner
Posts: 876
Joined: Mon Nov 13, 2017 11:59 am
Location: Poland

Multiplication by 64

Post by Ralf » Sat Nov 03, 2018 7:02 pm

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

User avatar
PeterJ
Site Admin
Posts: 770
Joined: Thu Nov 09, 2017 7:19 pm
Location: Surrey, UK

Re: Multiplication by 64

Post by PeterJ » Sat Nov 03, 2018 7:10 pm

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

User avatar
Einar Saukas
Manic Miner
Posts: 491
Joined: Wed Nov 15, 2017 2:48 pm

Re: Multiplication by 64

Post by Einar Saukas » Sat Nov 03, 2018 7:45 pm

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
Manic Miner
Posts: 876
Joined: Mon Nov 13, 2017 11:59 am
Location: Poland

Re: Multiplication by 64

Post by Ralf » Sat Nov 03, 2018 8:08 pm

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

User avatar
Ast A. Moore
Manic Miner
Posts: 716
Joined: Mon Nov 13, 2017 3:16 pm

Re: Multiplication by 64

Post by Ast A. Moore » Sat Nov 03, 2018 8:38 pm

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.

User avatar
g0blinish
Dizzy
Posts: 63
Joined: Sun Jun 17, 2018 1:54 pm

Re: Multiplication by 64

Post by g0blinish » Sun Nov 04, 2018 2:52 pm

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: 315
Joined: Tue Nov 14, 2017 10:26 am

Re: Multiplication by 64

Post by Joefish » Mon Nov 05, 2018 12:15 pm

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

Post Reply