Shop OBEX P1 Docs P2 Docs Learn Events
Stack (SPIN & PASM) — Parallax Forums

Stack (SPIN & PASM)

ImmNekImmNek Posts: 29
edited 2009-01-21 19:44 in Propeller 1
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
Immanuel

Comments

  • Cluso99Cluso99 Posts: 18,069
    edited 2009-01-20 11:07
    To start with use a stack size of at least 100 longs i.e.
    long Stack[noparse][[/noparse]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)
    http://forums.parallax.com/showthread.php?p=753439

    My cruising website http://www.bluemagic.biz
  • ImmNekImmNek Posts: 29
    edited 2009-01-20 12:02
    @ 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?
  • shanghai_foolshanghai_fool Posts: 149
    edited 2009-01-20 12:11
  • ImmNekImmNek Posts: 29
    edited 2009-01-20 12:25
    @ 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
  • simonlsimonl Posts: 866
    edited 2009-01-20 14:38
    @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)

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Cheers,
    Simon

    www.norfolkhelicopterclub.com

    You'll always have as many take-offs as landings, the trick is to be sure you can take-off again wink.gif
    BTW: I type as I'm thinking, so please don't take any offence at my writing style smile.gif
  • Andrew E MileskiAndrew E Mileski Posts: 77
    edited 2009-01-20 21:24
    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 BakerPaul Baker Posts: 6,351
    edited 2009-01-20 21:37
    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
  • Cluso99Cluso99 Posts: 18,069
    edited 2009-01-21 03:44
    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)
    http://forums.parallax.com/showthread.php?p=753439

    My cruising website http://www.bluemagic.biz
  • ImmNekImmNek Posts: 29
    edited 2009-01-21 19:44
    @ 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.
Sign In or Register to comment.