Jump to content
IGNORED

Routine for Multiplying by Odd Numbers


Joel D. Park

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?

Link to comment
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 ]

Link to comment
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

adc var1

sta sum

lda var2

skip:

bne loop

Link to comment
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?

Link to comment
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...

Link to comment
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!

Link to comment
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.

I wrote an article about this (with code examples) in a Polish disk magazine.

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

 

fox

 

PS. Polish football players suck! :evil:

Link to comment
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!

Link to comment
Share on other sites

:D 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...

 

lookup=i*7 instead????

 

hve

 

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

Link to comment
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.

Link to comment
Share on other sites

Yep, roomtonumaddress code..

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?

Link to comment
Share on other sites

Yep, roomtonumaddress code.

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.

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