# Routine for Multiplying by Odd Numbers

## Recommended Posts

I've been researching and I've kind of came up with nothing but some theories in my own mind.

I would like to take a number and multiply it by 7.

I assume I'll use ROL 3 times to multiply by 6 then I'll do addition to add the number in one last time... Or something like that. Could someone post an example?

##### Share on other sites

Uh...ASL three times and you'd be multiplying by 8. Then subtract your original value.

How big are the numbers going to end up? You would need to check the carry status after each shift if the product has the possibility of being over 255.

So you could use a combination of ASL's and BCC's (if carry is set, you could bump up a register via INX/INY).

Or if you have enough cycle time, you could just make a generic adding routine (that way you can use the same routine for other combos).

[ 06-10-2002: Message edited by: Nukey Shay ]

##### Share on other sites

My values should all stay within a page. Thanks for you help .

##### Share on other sites

a)

sta temp

:3 asl @

sub temp

b)

tax:lda mulBy7LookupTable,x

##### Share on other sites

The lookup table technique might suit your needs better if cycle timing is critical.

Ben

##### Share on other sites

The standard (= universal, but slow) way is to

- shift the sum left.

- shift one number right

- if the carry is set, add the other number to the sum

- loop until the first number is 0

lda #0

sta sum

lda #23

sta var1

lda #5

sta var2

.loop:

asl sum

lsr var2

bcc skip

lda sum

clc

sta sum

lda var2

skip:

bne loop

##### Share on other sites

Still, it's a little complex for 2x7. j/k

"Don'cha looooove assembly." -Andy MayBinary

##### Share on other sites

quote:

Originally posted by Nukey Shay:

Still, it's a
little
complex for 2x7

Ok, I should have written: standard = slow, large, complex but universal.

But with some optimizations you can get it a lot smaller (and even more complex ).

BTW : What does j/k mean?

##### Share on other sites

Just kidding...but on some boards I get a lot of giggles when I post it, so I dunno.

##### Share on other sites

If talking about general multiplication routine, then:

a * b = f(a+b) - f(a-b)

where f(x) = x * x / 4 (using lookup table).

##### Share on other sites

:wink: this trick is a little bit far beyound 6502 newbies...i never understood this method (my math skills are poor) but this works perfect and is one of the fastes methods to do multiplying... check also

c=hacking (search google...) oops...its an atari board...

hve

ps. germany : cameroon 1:0 playing 10 against 11...fingers crossed...

##### Share on other sites

fifaworldcup rocks!

##### Share on other sites
If talking about general multiplication routine, then:

a * b = f(a+b) - f(a-b)

where f(x) = x * x / 4 (using lookup table).

Remember seeing that somewhere before, but can anyone explain why that works or point me to a derivation - I could do with "Maths for Dummies" as my solitary math brain cell must have died.

Thanks...

PS Ireland through with 3-0 as well as Germany through!

##### Share on other sites

Let ^ be "raise to power".

(a+b)^2=a^2+2*a*b+b^2

(a-b)^2=a^2-2*a*b+b^2

(a+b)^2-(a-b)^2=4*a*b

a*b=((a+b)^2-(a-b)^2)/4

The good thing is that it works for any a and b, including negative.

And I use this method in my Vector engine, of course.

fox

PS. Polish football players suck!

##### Share on other sites

Hehe:-)

You guys with your giant throbbing craniums are driving me nuts. I've been programming professionally on Business software for the last 7 years (In VB, nothing like Assembler of Course) but I haven't had to do more than simple division.. I can barely do simple math anymore (Stupid Calculators :-)).

This is for my Adventure hacks. I can save 32 bytes by removing the storage byte for the Black and White color on each room in the game..

The problem comes in when searching for the rooms in the tables, it takes the previous searched item and multiplies by 8 right now to find the next room and it loops until it finds the room it needs. If I remove the black and white storage byte I'll have to multiply by 7 instead.. With most of the multiplication above, I would probably use up most of the bytes I save anyway.

If I don't find a real benefit to getting rid of the Black and White color, I may decide to use it for something else special. :-)

At first I'll try the simpleist way possible, I'm replacing some previous code so I may be able to make changes without effecting screen updates... Besides I think this table and room search is being fired while the player is between rooms, the screen update might not be an issue, since the whole screen is being changed.

Thanks again for all your help!

##### Share on other sites

Actually, you'll only need to use up a couple bytes to transfer the index to a \$temp addy, and a couple more later (for the SBC, \$temp instruction). It's already using the 3 ASL's, right?

##### Share on other sites

think about it... you are spending hours time of how to make 7 byte multiplying instead of 6502 friendly 8 byte multiplay...

i don't actually know how much bytes you really save with this but you are right that you definitly waste it with more overhead...

if you don't need a FAST "7" multiplayer stick to a "standard 6502" multiply routine... Fox' method (well...it is standard maths so not fox has invented this method... ) works well for FAST near realtime calculations which you need for 3d stuff on 8bit computers...

or you make lookup tables...

but for a hack i would prefer a "simple" working version for your hack...no "general" method...

hve

ps. argentina gone! cool... germany will definitly win worldcup 2002!!!

##### Share on other sites
Hehe:-)

This is for my Adventure hacks.  I can save 32 bytes by removing the storage byte for the Black and White color on each room in the game..

The problem comes in when searching for the rooms in the tables, it takes the previous searched item and multiplies by 8 right now to find the next room and it loops until it finds the room it needs.

>snip

Thanks again for all your help!

Which routine are you refering to? It would be much easier to help with looking in the code myself.

If you mean the RoomNumToAddress routine, then I can assure you, there are a lot of bytes to be saved in the code too.

##### Share on other sites

In my version, not the version I have here, I've allready eliminated Black and white checks everywhere else, Item colors included.. Even the code that uses the black and white color has been eliminated. The only thing left is the code in the room deffinition table.

There are around 30 rooms and each one steals a byte for the black and white color storage. I can safe 30 bytes off the batch, plus one for each new room I decide to add. I'm gonna print out this entire set of messages and try and see if I can figure out how to make them work..

Storing my original number and subtracting it at the end definately sounds the easiest for me to figure out.

By the way, in the adventure code, can you tell why all the maps are only 7 pixels/lines high instead of 8 ??? Is this some kind of Extra Low Resolution thing that going on?? Is it actually a property of the Atari, or does it have to do with the way the scan lines are being read?

##### Share on other sites

If you send me what you got now, I'll can help you optimizing a lot of bytes away. And offline I have more time to explain what I am doing or you can do.

By the way,  in the adventure code, can you tell why all the maps are only 7 pixels/lines high instead of 8 ???  Is this some kind of Extra Low Resolution thing that going on??  Is it actually a property of the Atari, or does it have to do with the way the scan lines are being read?

That is because of the weird aspect ratio of the 2600 (160x~200). 7 vertical pixels equal to about 4 horizontal pixels. So rotating objects in all games I know (including Thrust) change theirs sizes a little bit.

##### Share on other sites

Wow, that would be great.

I've got everything at home. I wanted to bring the labeled but un-modified version in for the Stella list anyway. I'll bring both versions in so you can check it out..

There's no need for you to spend alot of time on it though.

##### Share on other sites

how fascinating 6502 source code can be in year 2002... like egypt hyroglyphs...or japanese characters...or binary values...

hve

##### Share on other sites

Exactly...in another 5000 years people will wonder just what the hell we had in mind. Hopefully, they'll be able to decypher it enough to enjoy a good round of Warlords.

##### Share on other sites

Sounds like you're in safe hands now Joel. If anyone can do it on 2600, Thomas can.

Thanks Fox for that explanation. I actually managed to follow that for once! Don't suppose you know any good tricks for division as well ?

##### Share on other sites

division can be supstituted with a multiply by 1/x

1 / 2 = 1*1/2

## 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.

×   Pasted as rich text.   Paste as plain text instead

Only 75 emoji are allowed.

×   Your previous content has been restored.   Clear editor

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

• ### Recently Browsing   0 members

×
• Forums
• Clubs

• All Activity

• #### Subscriptions

×
• Create New...