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

Stack space

LuckyLucky Posts: 98
edited 2010-04-04 07:54 in Propeller 1
If the method your launching into a new cog is spin, the cognew command requires a stack pointer, but if its assembly code it doesn't? Why doesn't assembly routines need a stack pointer?

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
"The man who smiles when things go wrong has thought of someone to blame it on."


-Lucky[size=-1][/size]

Comments

  • jazzedjazzed Posts: 11,803
    edited 2010-04-04 05:38
    PASM does not use a stack. Subroutines in PASM are called using the jmpret instruction.
    The second parameter for cognew with PASM is the value given to the new COG's PAR register.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    May the road rise to meet you; may the sun shine on your back.
    May you create something useful, even if it's just a hack.
  • pullmollpullmoll Posts: 817
    edited 2010-04-04 05:38
    Lucky said...
    If the method your launching into a new cog is spin, the cognew command requires a stack pointer, but if its assembly code it doesn't? Why doesn't assembly routines need a stack pointer?
    Because in PASM there is no stack pointer and thus no need for a stack. Calls to subroutines are executed by modifying the accompanying return instruction, which is why recursion isn't possible in PASM without additional measures, e.g. creating a stack in hub RAM.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    He died at the console of hunger and thirst.
    Next day he was buried. Face down, nine edge first.
  • MagIO2MagIO2 Posts: 2,243
    edited 2010-04-04 07:54
    The call is only a compiler-instruction. If you have a "call #somewhere" instruction, the compiler searches for a "somewhere_ret" label. There you have to put the ret instruction. When the call is executed, the current program counter + 1 is stored in the source-field of the ret instruction.

    There are 2 lessons to learn:
    1. You can't have multiple ret instructions in a subroutine as only the one behind the x_ret label will relly return then.
    For example:
    call #doSomething
    ...
    doSomething
    ' check condition
    ...
    IF_C ret
    ' if not C do something
    ...
    doSomething_ret
    ret

    This would not work as desired. If you want to exit the subroutine before, you have to jump to the _ret

    2. call is self-modifying code. So you should not have empty subroutines as placeholder during development:

    ' the main code already calls an empty subroutine
    call #doSomething
    ....

    doSomething
    doSomething_ret
    ret

    A self-modified instruction can't be executed as next instruction, as the unmodified version is already loaded into the execution pipeline. So, you'd have to put a NOP in between doSomething and doSomething_ret.
Sign In or Register to comment.