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

# Recursion Works

Posts: 6,491
edited 2006-03-31 - 20:45:41
Advanced programmers may be wondering if Spin methods are recursive -- at least with this program, the answer is yes.

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Jon Williams
Applications Engineer, Parallax

• Posts: 614
edited 2006-03-29 - 14:22:25
Wouldn't memory be the determining issue as to how deep the recursion goes?

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Just tossing my two bits worth into the bit bucket

KK
·
• Posts: 6,351
edited 2006-03-29 - 14:36:13
Yes but when spawning a Spin cog, one of it's arguments is a pointer to a stack, which you specify the size of. Thats why recusion works in spin, the use of a stack makes the code re-entrant. Assembler doesn't natively use stacks, to do recursive programming in assembler you would have to write your own stack routines and use them for jmpret locations.

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
·1+1=10
• Posts: 6,491
edited 2006-03-29 - 14:53:24
And this demo is running in Cog 0, so it I believe it's got the entire global RAM (about 7800 longs) to use as a stack.

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Jon Williams
Applications Engineer, Parallax
• Posts: 6,351
edited 2006-03-29 - 15:24:35
Thanks Jon, that was actually a question I asked myself after I posted "whats the effective size for the initial stack".

ARgh, I just had to shoo away our new kitten from my work bench, she was trying to chew off one of the caps on my propeller board. Thankfully its not powered up.

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
·1+1=10
• Posts: 614
edited 2006-03-29 - 17:16:26
So, the programmer would have to make sure the recursive routine wouldn't go deeper then what space permitted.
So, for example, doing a mandlebrot set, the depth of each calculation would have to be limited...
How would you know how deep you can go, other then trial and error?

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Just tossing my two bits worth into the bit bucket

KK
·
• Posts: 6,351
edited 2006-03-29 - 18:05:59

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
·1+1=10
• Posts: 22,614
edited 2006-03-29 - 18:31:15
Paul just posted a link to my stack monitor. It will monitor a stack up to 255 LONGs deep, which may be insufficient when recursion is involved. But you can always shift the result in the last statement by less than sixteen to get a scaled display.

-Phil
• Posts: 6,491
edited 2006-03-29 - 18:39:53
Guys, I'm not a particularly clever programmer so I'm not suggesting that recursion will aways work -- it does in that case (Cog 0, with repeat limits used) and I was simply sharing that. I happened across that routine while looking up C style guidelines and gave it a try; I was happily surprised that it worked, and now have a better understanding that it may not in all cases, even with about 7000 longs available as a stack.

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Jon Williams
Applications Engineer, Parallax
• Posts: 435
edited 2006-03-29 - 18:46:42
Paul Baker said...

ARgh, I just had to shoo away our new kitten from my work bench, she was trying to chew off one of the caps on my propeller board. Thankfully its not powered up.

Perhaps you should leave it powered......
• Posts: 614
edited 2006-03-31 - 19:07:01
LOL @ pwssr... One zap and the cat will NOT do it again... garantieed!
Anyway... I was toying with the idea of a real time 3D mandlebort generator / explorer.
Recursion is the best method of generating a mandlebrot, with limits as well, but other methods do exist.
The biggest unknown is the math. I know it's a matter of interpertation of data, and how you would like to see it applied.
It's always fanicated me, the mandle sets that is, to the point where I use the program POV to create a 3D set of stills as the computations "slid" down a hill.
I think it would be real neat to draw "dots" on the screen in real time computations, "sliding down, twisting" as the calculations proceede.
The math is extensive, and would (might?) most likey require a math coprocessor (like the one Parallax has) for the floating point calculations.

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Just tossing my two bits worth into the bit bucket

KK
·
• Posts: 6,351
edited 2006-03-31 - 19:17:40
Theres an object to do floating point calculations already availible, it would be faster than interfacing with an external serial coprocessor. It would still take quite a while to generate a single set.

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
6+6=10 (Long live the duodecimal system!)
• Posts: 6,491
edited 2006-03-31 - 19:52:45
To Paul's point, Chip wrote an FP library in Spin that does a few of the essentials -- also shows the internals of FP. That said, in Spin it's not as fast as it could be and the library is not particularly feature rich. Our goal is to have an FP library written in Propeller assembly that will have many features and be very fast; ultimately it would be like adding a math coprocessor, but without having to use an external device.

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Jon Williams
Applications Engineer, Parallax
• Posts: 614
edited 2006-03-31 - 20:45:41
Thats good.
TO coin another's phrase, the more I learn, the less I can wait!

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Just tossing my two bits worth into the bit bucket

KK
·