Jump to content



0

mul6 in assembler?


4 replies to this topic

#1 Heaven/TQA OFFLINE  

Heaven/TQA

    Quadrunner

  • 8,105 posts
  • Location:Baden-Württemberg, Germany

Posted Thu Jul 5, 2007 9:50 AM

how would you do a*6 in 6502?

i need that to calc an offset for sprite background buffer... i have to expand from *4 to *6 but do not want to go for *8 instead...

any quick code to do that?

#2 Bruce Tomlin OFFLINE  

Bruce Tomlin

    River Patroller

  • 3,531 posts
  • CD C9 01
  • Location:Austin, TX

Posted Thu Jul 5, 2007 10:34 AM

Multiplying by a constant is just a series of shifts and adds.

To multiply by 6, you add *2 and *4. So it's shift, save, shift, add.

ASLA (A=A*2)
STA temp (save A*2)
ASLA (A=A*4)
ADC temp (A*4+A*2=A*6, and assuming that the carry was clear from the previous ASLA)

Multiply by ten is *2 + *8.

ASLA
STA temp
ASLA
ASLA
ADC temp

Of course it gets a bit trickier when your result is greater than 255. On the Z80, 16-bit constant multiplies are easy when you use a bunch of ADD HL,HL and ADD HL,DE instructions, but on the 6502 it's a lot of extra work to do 16-bit shifts and adds, and you will probably need 4 bytes of temp.

Edited by Bruce Tomlin, Thu Jul 5, 2007 10:35 AM.


#3 Rybags ONLINE  

Rybags

    Quadrunner

  • 10,312 posts
  • Location:Australia

Posted Thu Jul 5, 2007 10:42 AM

Shift left to get 2*, keep result
Shift left again to get 4*, add to result

How big will the initial number be? If it's a single byte, then maybe table lookup would be economical.

Or maybe, another way of looking at it:
TAY ; save original number
AND #$3F
TAX
LDA MUL6TABL,X
STA RESULT
LDA #0
CPX #43 ; 6*43 is an overflow, so we set the high byte to 1
BCC NOTBIG
LDA #1
NOTBIG STA RESULT+1
TYA ; get number back
AND #$C0
ROL A
ROL A
ROL A
TAX
LDA MUL6TABH,X
ADC RESULT+1
STA RESULT+1
RTS
MUL6TABL .byte 0,6,12,18 ... ; etc... table has 64 entries, each one = (N*6) AND 255
MUL6TABH .byte 0,1,3,3 ; 4 entries

I think that should work. Could be optimised a bit.

Cost - about 100 or so bytes.
Time - might just be quicker to do the shift method

Edited by Rybags, Thu Jul 5, 2007 10:44 AM.


#4 Rybags ONLINE  

Rybags

    Quadrunner

  • 10,312 posts
  • Location:Australia

Posted Thu Jul 5, 2007 10:47 AM

TAY ; save original number
AND #$3F
TAX
LDA MUL6TABL,X
STA RESULT
TYA ; get number back
AND #$C0
ROL A
ROL A
ROL A
TAY
LDA MUL6TABH,Y
CPX #43
ADC #0
STA RESULT+1
RTS
MUL6TABL .byte 0,6,12,18 ... ; etc... table has 64 entries, each one = (N*6) AND 255
MUL6TABH .byte 0,1,3,3 ; 4 entries

optimized a bit - CPX sets the carry to what we want for the add anyway, so saved some time.

#5 Heaven/TQA OFFLINE  

Heaven/TQA

    Quadrunner

  • 8,105 posts
  • Location:Baden-Württemberg, Germany

Posted Thu Jul 5, 2007 11:15 AM

thanks guys... the result will be not greater than 255 as my sprite buffer is one page... so i can handle 42 sprites with that...which i never will need...

:)




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users