Jump to content
IGNORED

mul6 in assembler?


Recommended Posts

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
Link to comment
Share on other sites

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
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...