View Full Version : Stack (SPIN & PASM)

01-20-2009, 05:42 PM
Hi on the other side ;-D

I can't find a good description of the Propeller's stack handling. I'm coming from the language C and I know about the stack in this context (context-switches and so one). But on the Propeller, it's not clear for me. I know, that there is needed some stack when I call methods. But I don't know any thing, how big the stack size have to be for my code and how I can calculate this. Can somebody explane this to me or give some literatur links, which i didn't found by myself?

Please help me, because I have to understand this completle for a properly design of my apps written in SPIN.

Thanks a lot

01-20-2009, 06:07 PM
To start with use a stack size of at least 100 longs i.e.
long Stack[100]

I am more into pasm than spin, so cannot answer properly, so will leave that for someone else to answer better. However, some users have preset the stack to zeros and after execution have tested the stack (in reverse) to see where it got to. Then add a margin. Hopefully, you can exercise your code to utilise the stack. Unfortunately, there is really no definitive answer -(

Prop Tools under Development or Completed (Index)

My cruising website http://www.bluemagic.biz

01-20-2009, 07:02 PM
@ Cluso99:
Thanks a lot for your answer. Unfortunately this is exactly what I know by myself. I hope there is a definitive answer.

@ all:
When there is no definitive answer, then I have to test my code to define the stack size. Then I have the problem, that I don't know how to test my code properly. Can anyone give a method to do this properly?

01-20-2009, 07:11 PM
You can try this. I have not yet used it so I don't know if it works.

01-20-2009, 07:25 PM
@ shanghai_fool:
Thanks a lot, I will try it.

@ all:
Is there no definitive answer? So I will write all my code directly in PASM ;-D

01-20-2009, 09:38 PM
@ImmNek: I can't remember where I've seen an explanation of calculating stack size (might be in the manual, but I don't have that with me). I did find this though

rayslogic said...

How big does the stack for Cognew have to be?

The spin interpreter requires some stack space for passing parameters and return addresses during function calls.
There is no universal number. It depends on how deep you call functions and how many parameters you pass to functions. Recursion makes the calculation especially difficult.
Very simple programs can get away with 9 longs as in the manual's example. Medium programs (without recursion and limited nested function calls) can probably get away with ~50 longs. For larger, more complex programs, you will probably have to carefully monitor stack usage.
If you overflow the stack your other variables and program and RAM will be overwritten. There is code posted in the forum that writes specific data to the stack space and monitors it for changes to see how much is being used.
From Jeff Martin: There’s also an object called “Stack” that comes with the Propeller Tool, and is also available on the Propeller Object Exchange, to help determine an object’s stack usage.

(From: www.rayslogic.com/propeller/FAQ.htm (http://www.rayslogic.com/propeller/FAQ.htm))


www.norfolkhelicopterclub.com (http://www.norfolkhelicopterclub.com)

You'll always have as many take-offs as landings, the trick is to be sure you can take-off again http://forums.parallax.com/images/smilies/wink.gif
BTW: I type as I'm thinking, so please don't take any offence at my writing style http://forums.parallax.com/images/smilies/smile.gif

Andrew E Mileski
01-21-2009, 04:24 AM
I think the stack pointer is in cog memory, and inaccessible via SPIN byte code.

However, it should be possible to fill the stack with a known value, say $DEADBEEF, and see how many entries are overwritten.

Paul Baker
01-21-2009, 04:37 AM
Phil Pilgram posted a stack monitor to calculate the size needed for a particular Spin cognew: http://forums.parallax.com/showthread.php?p=577317

In regards to PASM, there really isn't a stack, the way calling a function works is that when the call is performed (say routine dothis), the return command indicated by the label dothis_ret is modified to efectively be JMP PC+1 where PC is the index of the call command which invoked the routine. (refer to page 360 of the manual)ĚSo there is no native stack to speak of in PASM.

Paul Baker (mailto:pbaker@parallax.com)

01-21-2009, 10:44 AM
Andrew, the stack is in hub and you give the spin program a pointer to this when you launch a spin program. So it is accessible from any cog (spin or pasm).

Prop Tools under Development or Completed (Index)

My cruising website http://www.bluemagic.biz

01-22-2009, 02:44 AM
@ simonl:
Thank you for this information. In the manual is now description how to calculate the size of the stack. I hope in a next version there is more stack related stuff. There are only some very simple examples and the stack size is always 9 longs and this are VERY simple examples.

@ Paul Baker:
I know, that in assambly there is't a stack. This wouldn't make any sense, because you have to move every parameter or other registers by yourselfe and there aren't any functions or methods like in a high-level language or an interpreted language. That's exactly why I joked and said I will write all my code in PASM, because then I don't have any problems related to the stack.
Thanks a lot for the link to the tool created by Phil, I will have a look on it.

@ Andrew E Mileski:
Nice idea to overview the stack pointer. I just have to find out it's address and set it to a value and look up for changes.

@ all:
Thank you all for your information. I see, there is no easy way to calculate the stack size for my SPIN-code, but there are some ways and thats the main. So, now I have to do the rest by myself.