PDA

View Full Version : cognew stack, can someone please explain this?



Cobalt
11-09-2006, 05:01 AM
The question is simple, and I hope that the answer is simple too:

When launching a new cog, you use the cognew command (we're talking spin). The command requires you define it a stack to use, for example:

I have a program that I want to launch a cog to do some talking with Phil's Voice synth object while other things are going on in the main cog. It would look something like this:

cognew(t.say("Hello"), @stack)

thats for example, I don't know how "Hello" would sound...

anyhow, the @ stack would be defined in the VAR block, but how do i determine what type of var (i.e. byte, word, long) to use, and what its size would be? (ex. long stack[10])

I looked over the manual, and refered to the beginning of it where it gives the basics of spin language by making multiple LED's flash. It just makes no sence to me as to how to figure out the size of the stack. http://forums.parallax.com/images/smilies/eyes.gif

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
while alive = 1
wakeup
program(propeller)
eat(3)
sleep(7)

Mike Green
11-09-2006, 06:29 AM
The stack area is always made up of long values. The size you need depends on the program. Imagine the deepest procedure call possible in your program (a calls b calls c calls d ...). You need about 4 longs for each level (x calls y) plus the number of parameters at each level added together plus a couple for temporary storage if the call is part of an expression plus some for the phase of the moon and the predicted weather. Add some just to make sure and there you have it.

There are programs that have been posted here that will initialize a stack space to known values, then watch the running program to see how much is actually used over a period of time.

Don Pomplun
01-22-2007, 01:59 PM
I was just about to ask the same question which you have (sort of) answered. When I off-loaded a trivial task into another cog, giving it 6 Longs of stack, the new process worked OK, but the (previously working) TV_Text quit. Of course I barked up all the wrong trees before increasing the stack to 10, and then all went well. So now I just keep decreasing it by one until things quit, then add 1 again? Your answer looked promising until I got to the moon part ;=)
All black magic, eh ?
-- Don

rokicki
01-22-2007, 03:59 PM
I think it's three longs for each level (the return value, and then four words for the return PC, the return frame
pointer, the return var ptr, and the return object pointer).

But in general, be generous with stack until you're short of memory. That is, start with something like 100 words
and only decrease it if you really need to.

Also, beware of doing things like

pub myfunc(a, b) | va[100]

that is, putting too much on the stack.