Heaven/TQA Posted July 5, 2007 Share Posted July 5, 2007 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? Quote Link to comment Share on other sites More sharing options...
Bruce Tomlin Posted July 5, 2007 Share Posted July 5, 2007 (edited) 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 July 5, 2007 by Bruce Tomlin Quote Link to comment Share on other sites More sharing options...
Rybags Posted July 5, 2007 Share Posted July 5, 2007 (edited) 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 July 5, 2007 by Rybags Quote Link to comment Share on other sites More sharing options...
Rybags Posted July 5, 2007 Share Posted July 5, 2007 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. Quote Link to comment Share on other sites More sharing options...
Heaven/TQA Posted July 5, 2007 Author Share Posted July 5, 2007 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... Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.