Shop OBEX P1 Docs P2 Docs Learn Events
Using multiple cogs and stack — Parallax Forums

Using multiple cogs and stack

rtarbellrtarbell Posts: 7
edited 2007-10-04 17:38 in Propeller 1
Hello all,

I have read through some of the tutorials and basic programming using Spin and the Propeller, and I wanted to ask about the syntax using multiple cogs:

looking in an example posted in this forum,

VAR
Long Stack0[noparse][[/noparse]20] 'Sets up a stack space for a Cog(processor)
Long Stack1[noparse][[/noparse]20] 'Sets up a stack space for a second Cog(processor)
'long Stack0[noparse][[/noparse]20]: Allocates 20 longs for the stack.
'long Stack1[noparse][[/noparse]20]: Allocates 20 longs for the stack.

The above two lines set up two separate stacks, then:

CogNew(BlinkingLED_A16 (WaitPeriod_Arg), @Stack0)
CogNew(BlinkingLED_A17 (WaitPeriod_Arg), @Stack1)

What does the @Stack0 and @Stack1 commands do? What would happen if I don't include them?
When using separate cogs and making separate stacks for each cog, how can I tell how large I should make the stack for the corresponding cog?

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2007-10-04 17:07
    1) "@Stack0" is simply the address of the array "Stack0". The 2nd parameter of the COGNEW statement is a parameter which, in this case, is the address of the area to be used for the run-time stack for the cog being launched. You can't "not include them". You must supply a 2nd parameter. Whatever its value, the Spin interpreter will use that as the starting stack address.

    2) It's best to allow something large (like 20 or 50). There are ways to calculate the amount of stack needed based on the complexity of expressions used in the program, the depth of calls, the number of parameters passed in those calls, etc. It's complicated, not exact, and will vary depending on the control flow in the program (which IF/THEN/ELSE paths get used). There are some sample programs that will fill the stack area with a particular pattern of data and watch it continually to see how much is actually used, then display the result.
  • RaymanRayman Posts: 14,162
    edited 2007-10-04 17:28
    How much does the Spin interpreter in Cog0 reserve? It must have a stack space too. I see you can override the default using the "_STACK" compiler directive. But, I don't see anywhere what the default value is. I guess there is none?

    I guess it just starts using memory, starting at the end of the program. I just realized that I have a few less longs left than I though in my app!

    I think real trouble would occur if you write code that uses recursion. (I think recursion is allowed.).
  • RaymanRayman Posts: 14,162
    edited 2007-10-04 17:30
    I just saw some answers two threads down...
  • Mike GreenMike Green Posts: 23,101
    edited 2007-10-04 17:38
    The default stack begins at the end of the program and continues to the end of memory (with no checking for overflow!)

    Yes, you might get into trouble with recursion. Recursion is very useful, but can be difficult to debug if something goes wrong.

    FemtoBasic uses recursion for its handling of expressions and there's a check in the expression handling routines for excess recursion.
Sign In or Register to comment.