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

Stack

Trying to understand the mechanics of this thing.

Testing FlexBasic on a P1-Quickstart for the first time (nice :smile:)

Running the supplied Blink2.bas which throws one of the blink procedures into a cog and so requires a
dim shared cpustack(8)

Then I added another blink procedure in another cog and I created another stack
dim shared cpustack2(8)

But the only way for this to run:
var x = cpu(blinker(LED1, 500), @cpustack(0))
var y = cpu(blinker(LED3, 125), @cpustack2(1))

I can't use "0" as the start of stack memory for the extra cog.
What is actually happening here?

Still haven't figured out how to list code in this thing so...bitmap.

Craig

Comments

  • I’m not near my P2 stuff at the moment, but a couple of things come to mind that might help.

    1). Add OPTION EXPLICIT to force formal var declaration.
    2). Increase HEAPSIZE to 512 or so.
    3). Increase each of the stacks to 32 or so. Elbow room!

    You can type 3 backticks on their own line to make the forum start and stop displaying code sections.

    Maybe this helps.

  • @JRoark

    Ah-ha...wasn't doing the backticks on their own line thing :+1:

    Bit puzzled as to why that stuff is necessary(?)
    This is a simple P1 program in PropBasic.

    DEVICE P8X32A, XTAL1, PLL16X
    FREQ 80_000_000
    
    Background TASK AUTO
    
    PROGRAM Start
    
    temp1  VAR LONG
    
    Start:
      WATCH temp1
      DO
        FOR temp1=0 to 10
          PAUSE 10
        NEXT 
        FOR temp1=9 to 1 step -1
          PAUSE 10
        NEXT 
      LOOP  
    END
    
    
    TASK Background
    temp2 VAR LONG
    
      DO
        WATCH temp2
        FOR temp2=0 to 10
          PAUSE 10
        NEXT 
        'UNWATCH temp2
        FOR temp2=9 to 1 step -1
          PAUSE 10
        NEXT
      LOOP  
    ENDTASK
    

    Craig

  • You say you "can't" use 1; what are the symptoms? That the other LED doesn't blink? If so the problem is likely that the stack size is too small (which is my fault, I should have had a bigger one in a demo). Try increasing it to 16 or even 32.
    As for why the stack declaration is even necessary: FlexBasic has a lot of features (e.g. recursion) that rely on a stack being present. PropBasic is great for what it is, but it's a much more restricted subset of BASIC.

  • @ersmith said:
    You say you "can't" use 1; what are the symptoms? That the other LED doesn't blink? If so the problem is likely that the stack size is too small (which is my fault, I should have had a bigger one in a demo). Try increasing it to 16 or even 32.

    As shown, all 3 LEDs works as intended but if I set @cpustack2(1) to @cpustack2(0), as-is the other stack (@cpustack(0)) then none of the 2 cogs run

    Increasing the stack (16) does make it work with @cpustack(0) and @cpustack2(0) :+1:

    I found two examples of the demo, one in a document (manual?) and the other as a source file. One had a stack of 8 and the other had a stack of 32.

    None of this is an issue, per se. :smile:

    Craig

  • BeanBean Posts: 8,129

    The only time PropBASIC uses a stack is when you generate LMM (Large Memory Model) code.
    It uses it for subroutine call return addresses only.
    So, yeah, PropBASIC does not support recursion.

    Bean

  • pik33pik33 Posts: 2,366

    Stacks too short. If it works with stack2(1) , try dim cpustack(9)

  • @pik33 said:
    Stacks too short. If it works with stack2(1) , try dim cpustack(9)

    :+1: Many thanks.
    Yes, actually making it work is not the problem and I understand the purpose of the stack. What is not clear is why there are examples that show "(1)" and others that show "(0)" :neutral:

    Just trying to understand what is physically happening.

    Craig

  • @Mickster said:

    @pik33 said:
    Stacks too short. If it works with stack2(1) , try dim cpustack(9)

    :+1: Many thanks.
    Yes, actually making it work is not the problem and I understand the purpose of the stack. What is not clear is why there are examples that show "(1)" and others that show "(0)" :neutral:

    There may be some examples that date back to when the default array base was 1 (so the first element of array A was A(1)). Nowadays the default is 0 (so A(0) is the first element), but this is settable with OPTION BASE 1 or by declaring the array with an explicit lower bound like DIM A(1 to 10) as INTEGER.

  • @ersmith
    I figured it was something like that after reading an old thread about what should be the default base. :+1:

    Craig

Sign In or Register to comment.