Welcome to the Parallax Discussion Forums, sign-up to participate.

Performing modulus in PASM

Posts: 123
edited 2018-01-31 - 00:33:52
I have a dynamic value, n, that I need to perform modulus on within a cog's PASM code in order to calculate a hub RAM memory offset.

Is there a fast/low instruction count trick to performing this operation? Google and forum searches haven't helped.

Thank you!

• Posts: 123
I apologize, my re-attempt at Googling somehow was more helpful... I'm sure there's some Law out there that states "Any problem searched extensively for will be solved by a re-search performed immediately after posting on a forum about it"
• Posts: 21,233
Yes indeed. Internet or not, often as soon as you can form the question in your mind, your mind has the answer!

As to the "modulus" thing. That is easy or hard depending if your modulus is a power of 2 or not.

• Posts: 123
Heater. wrote: »
Yes indeed. Internet or not, often as soon as you can form the question in your mind, your mind has the answer!

As to the "modulus" thing. That is easy or hard depending if your modulus is a power of 2 or not.

Fortunately it was modulus-8, so I was in luck on that front!
• Posts: 6,444
edited 2018-01-31 - 18:58:04
For future reference then, modulus any number is just as easy, just a bit esoteric.
```cmpsub offset, modulus .  ' keeps offset in the range 0 to (modulus-1)
```

• Posts: 1,981
Of course that instruction assumes the value is already in the range 0 .. 2*modulus-1.
• Posts: 6,444
edited 2018-02-01 - 22:03:29
Good point. I was thinking of offset into a circular buffer or paged memory (as in data logging) where the address increments by one or by a small value less than the modulus. Quite a different thing, more complicated, for random access.

I bookmarked the page that Escher linked to at Stanford.edu, bithacks. It reminds me of Hacker's Delight, wherein chapters 9 and 10 cover a lot of the same ground. A great reference.