Page 1 of 1

Multiplication by 64

Posted: Sat Nov 03, 2018 7:02 pm
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?

Re: Multiplication by 64

Posted: Sat Nov 03, 2018 7:10 pm
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

Re: Multiplication by 64

Posted: Sat Nov 03, 2018 7:45 pm
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

Re: Multiplication by 64

Posted: Sat Nov 03, 2018 8:08 pm
by Ralf
Is it faster Einar? Did you count tstates?

Re: Multiplication by 64

Posted: Sat Nov 03, 2018 8:38 pm
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.

Re: Multiplication by 64

Posted: Sun Nov 04, 2018 2:52 pm
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

Re: Multiplication by 64

Posted: Mon Nov 05, 2018 12:15 pm
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).