Helping each other learn SPIN2 and PASM2 - compiling best practices and language idioms...
Stephen Moraco
Posts: 316
I'm starting this thread with thoughts of seeding a "Best practices in SPIN2 and PASM2 document" for us all to use and to which those of us learning SPIN2 and PASM2 can contribute (hence this thread)
Also, if any language elements come forward from earlier SPIN and PASM but run on the P2 then they're fair game for answer content here too!
We are all learning at various rates. Some of us are much further along than others in these two languages. I'm thinking that it will be counterproductive to ask every one of these questions of Chip directly even tho' he's been amazing when asked. We will always look forward to his answering when he wishes to. If we can help each other by sharing what we have already learned then Chip doesn't have to chime in unless he sees us missing something obvious or we're just going astray.
Please help us keep our discussions in this thread to these two languages only! By contributing to this thread, we are trying to bolster what we know and trying to share best practices while writing SPIN2 and PASM2 code!
Oh, my term "language idiom" I use to mean a tiny snippet of code that we know just works so we commonly use these tiny patterns as a best practice. Additionally, if you find nice a nice short snippet that should be considered for idiomatic use, please post it here for us all to enjoy while we start using it!
-Stephen
Also, if any language elements come forward from earlier SPIN and PASM but run on the P2 then they're fair game for answer content here too!
We are all learning at various rates. Some of us are much further along than others in these two languages. I'm thinking that it will be counterproductive to ask every one of these questions of Chip directly even tho' he's been amazing when asked. We will always look forward to his answering when he wishes to. If we can help each other by sharing what we have already learned then Chip doesn't have to chime in unless he sees us missing something obvious or we're just going astray.
Please help us keep our discussions in this thread to these two languages only! By contributing to this thread, we are trying to bolster what we know and trying to share best practices while writing SPIN2 and PASM2 code!
Oh, my term "language idiom" I use to mean a tiny snippet of code that we know just works so we commonly use these tiny patterns as a best practice. Additionally, if you find nice a nice short snippet that should be considered for idiomatic use, please post it here for us all to enjoy while we start using it!
-Stephen
Comments
I have a number in a variable/register. I want to generate a set of 1 bits having the length of the number.
(e.g., the number is 5 so I want to end up with $00001f (5 one bits.) Does this make sense?
in other words, what's the simplest way to generate a mask from the number of bits to be set in the mask?
Maybe the pasm looks something like:
' in bitCount, out bitMask
which with our new inline assembly makes our spin look like
NOTE: this will of course need guards. (e.g., don't call this routine with a request for zero bits and we probably should limit it to 32 bits max, etc.!)
I'm searching through the instruction spreadsheet thinking there may be advanced instructions that can help with this.... While this is pretty good are there better answers?
PPS: DECOD and ENCOD are part of that group. They suit it though, feels right there.
PPPS: Cluso has a similar topic that is stickied already, although that's not questions but rather the fruits of the answers - https://forums.parallax.com/discussion/169542/p2-links-for-where-to-obtain-tools-sample-test-code-reference-only/p1
We quickly got into the nitty gritty of coding by doing simple and fun stuff - like Magic Cubes, and Alpha-Numeric "Bubble-up" sorting. New terms like Hollerith / punched paper cards (good riddance ), adders, half-adders, lots of Binary stuff... It served us well. And that was fifty (50 ) years ago.
Today;s exercise: Magic Cubes in SPIN2 Us old timers know how to do it - Newbies, not so much...
Per the suggestions, bmask is helpful, but the you need to make a small adjustment to get a mask with a specific number of ones. Here's my test code:
The output is:
I think it's a great idea as well. Best of both worlds. The ease of HLL's along with the speed of assembly where that is needed. Like like eating our cake and having it as well.
Jon, you beat me to posting this single-pager test framework. Thanks!
I agree, I find that as I'm learning how pasm instructions work I end up generating many of these small test files.
One difference, and accomplishing the same thing as yours, is I end up using Chips' debug() for these little test snippets more than I do serial out. I find that I haven't developed yet, my personal style for which I use when. I'm just soaking it all in for now. ;-)
Just so we have a visual comparison... here's what my snippet looked like when testing the bmask suggestion offered earlier in this thread:
(This is NOT a competing solution as both work perfectly well. This is just here so that when we speak of these techniques a new reader of this thread will have an idea of what we speaking about.)
Our point in sharing these things in this thread is to over-time show code examples that help us develop SPIN2 and PASM2 more quickly and with more already tested code (these idioms) which we develop and then reuse these in our code wherever applicable.
What is "frac", It Wasn't listed anywhere else in the file!
Not understanding what is going on inside the parenthesis.
It is unsigned fraction binary operator.
EDIT: Err: Quotient is meant to be 32r32 format, same as QDIV. Not sure how that works in practice.
You don't need ... ... because the interpreter clears the result variable before executing any of the method code. It's not hurting, it's just redundant.
If the value for bmask is 0, it returns 0, hence this 2-line version works.
A few minutes later...
Darn, that doesn't work with 32 bits, which is a valid mask. Need some lunch.