Multiplication by 64

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

Multiplication by 64

Post by Ralf »

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?
User avatar
PeterJ
Site Admin
Posts: 6853
Joined: Thu Nov 09, 2017 7:19 pm
Location: Surrey, UK

Re: Multiplication by 64

Post by PeterJ »

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
User avatar
Einar Saukas
Bugaboo
Posts: 3070
Joined: Wed Nov 15, 2017 2:48 pm

Re: Multiplication by 64

Post by Einar Saukas »

Ralf wrote: Sat Nov 03, 2018 7:02 pmSo 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
Ralf
Rick Dangerous
Posts: 2279
Joined: Mon Nov 13, 2017 11:59 am
Location: Poland

Re: Multiplication by 64

Post by Ralf »

Is it faster Einar? Did you count tstates?
User avatar
Ast A. Moore
Rick Dangerous
Posts: 2640
Joined: Mon Nov 13, 2017 3:16 pm

Re: Multiplication by 64

Post by Ast A. Moore »

Ralf wrote: Sat Nov 03, 2018 8:08 pm Is it faster Einar? Did you count tstates?
Fourteen T states faster.
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
Manic Miner
Posts: 281
Joined: Sun Jun 17, 2018 2:54 pm

Re: Multiplication by 64

Post by g0blinish »

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
User avatar
Joefish
Rick Dangerous
Posts: 2041
Joined: Tue Nov 14, 2017 10:26 am

Re: Multiplication by 64

Post by Joefish »

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