I am trying to think of the most efficient way to multiply a number in 6502. Its obvious that x*y can be done by summing (128*x7+64*x6+32*x5+16*x4+8*x3+4*x2+2*x1+1*x0)*y in which x7-x0 are the respective bits of number of x (e.g. 1 or 0). Rol and Asl will permutate the y values, e.g. 128*1*y can be computed by 7*rol (or in practice 1 ror and result into high bit+bit 7 low bit). Well, you see were I get to.
This has probably been done a thousand times already so maybe any of you have a good link to some optimized code?
For multiplication of z by 400 I found that the code below was pretty efficient (well, a table is faster but also memory consuming):
Code: Select all
zval=mult400+1
zscr word 0 ; result here
mult400 lda #0 ; put z value here before calling
rol a
rol a
rol a
rol a ; swap lower 4->higher 4
tay
rol a ; get carry
tax
tya
and #$f0 ; lower 4 bits
sta zs1+1 ; store low byte
lda zval
and #$1 ; load 128*z into x
rol ; bit 0 to carry
rol ; carry to bit 7 low byte, clear carry
zs1 adc #0 ; add low byte, overflow to carry
txa
and #$f ; higher 4 bits->high byte
adc #$0 ; add carry
sta zs2+1 ; store high byte
lda zval
and #$fe ;load 128*z into a (high byte)
lsr a ;divide by 2 & clear carry
adc zval ; add 256*z to high byte
zs2 adc #0
sta zscr+1 ; store high byte
rts