Shop OBEX P1 Docs P2 Docs Learn Events
Stack Posers — Parallax Forums

Stack Posers

CannibalRoboticsCannibalRobotics Posts: 535
edited 2011-07-13 10:07 in Propeller 1
Two questions about the stack:
1) A stack length object can approximate the stack size by stuffing the stack then watching where the changes are made. What about the top object. Will it just step on child objects as memory is needed?

2) Say you have three simple, non-recursive objects called sequentially. Each one needs 9 longs of stack space. Is the allocation for those 9 or 27. If 27, what causes the pointer to reset or adjust itself back down? (Please put pointer access into the next prop!)

Thanks,
Jim-

Comments

  • Mike GMike G Posts: 2,702
    edited 2011-07-13 06:43
    1) A stack length object can approximate the stack size by stuffing the stack then watching where the changes are made. What about the top object. Will it just step on child objects as memory is needed?
    Yes, the prop does not manage memory.
    2) Say you have three simple, non-recursive objects called sequentially. Each one needs 9 longs of stack space. Is the allocation for those 9 or 27. If 27, what causes the pointer to reset or adjust itself back down? (Please put pointer access into the next prop!)

    The stack resource is released after the process is run. I can't remember where I read this the Propeller manual or the PE Kit or both.
    Please put pointer access into the next prop
    Prop 1 has pointer access. What are you looking for?
  • CannibalRoboticsCannibalRobotics Posts: 535
    edited 2011-07-13 07:02
    Mike G wrote: »
    Prop 1 has pointer access. What are you looking for?

    Where? I'd like to be able to watch the range of the top object to see when and how it's eating it's young.
  • jazzedjazzed Posts: 11,803
    edited 2011-07-13 08:27
    Where? I'd like to be able to watch the range of the top object to see when and how it's eating it's young.

    @result is a pointer that gives the address of result. The result variable is always at the "top of stack" for any spin program. If you only care about the top of stack for the startup spin program, you can watch the contents of hub location $E with stackpointer := WORD[$E] ....
  • Dave HeinDave Hein Posts: 6,347
    edited 2011-07-13 09:11
    Jim,

    You could try this code to monitor the stack. It fills the stack from @result + 40 to $7FFF with a pattern, and then checks for the pattern starting from the long at $7FFC. The start of the stack is given by WORD[10]. WORD[$E] is offset from WORD[10] by the space used by the stack variables in the first method plus 8 bytes for a stack frame. Instead of using $7FFF for the end of the stack you could use something more reasonable like WORD[10] + 800, which would allow for 200 longs on the stack. That would speed up the routines a bit.

    Dave
    con
      _clkmode = xtal1+pll16x
      _clkfreq = 80_000_000
      
    obj
      ser : "FullDuplexSerial"
    
    pub main
      WritePattern
      ser.start(31, 30, 0, 115_200)
      ser.str(string("Hello World", 13))
      ser.dec(StackUsed)
      ser.str(string(" bytes used on the stack", 13))
      Recurse(5)
      ser.dec(StackUsed)
      ser.str(string(" bytes used on the stack", 13))
    
    pub WritePattern
      longfill(@result + 40, $F00DFEED, ($8000 - @result - 40) >> 2)
    
    pub StackUsed | ptr
      ptr := $7FFC
      repeat while long[ptr] == $F00DFEED
        ptr -= 4
      return ptr + 4 - WORD[10]
    
    pub Recurse(num)
      if (num > 0)
        Recurse(num-1)
    
  • CannibalRoboticsCannibalRobotics Posts: 535
    edited 2011-07-13 09:40
    OK, I'm going to delve into the unknowns of the top object memory but first I want to make sure I'm on solid footing with the child objects. (Does it make sense to create a table of address blocks of all of the children then look at the @result location in the top object to see if it has entered restricted space? How do I know the addresses of the actual code?)

    Grade my code, this is in the start PUB of my child objects.
      seed := Seed?                                      ' Random number into seed (a byte) 
      bytefill(@Stack,seed, StackSpace * 4)              ' Fill stackspace: a constant
                                                         ' Launch the cog @Stack
      cognew(xxMain(Rx, Tx,Baud), @Stack) 
    
    

    then I use this to test the stack space used periodically.
    pub TestStack | i,j
      j:= seed                                              ' Initialize j for first repeat step
      i := @Stack + (StackSpace * 4) - 1                    ' Position i at 'top' of stack
      repeat while j == seed and i > @Stack - 1             ' Work down from top looking for non-seed value
        bytemove(@j,i,1)   
        i --
                                                            ' Return answer in longs +- 1 for round-off
      i := (i - @Stack) / 4
      return i 
    
    Am I (should I be) getting the right answers?
    Thanks,
    Jim-
  • CannibalRoboticsCannibalRobotics Posts: 535
    edited 2011-07-13 10:07
    And... If I change the StackSpace variable the answers don't change....
Sign In or Register to comment.