Shop OBEX P1 Docs P2 Docs Learn Events
bytecode header q.: base of stack vs. initial stack pointer — Parallax Forums

bytecode header q.: base of stack vs. initial stack pointer

mparkmpark Posts: 1,305
edited 2008-08-26 12:22 in Propeller 1
Hippy's MainSpec.txt document on Spin bytecodes describes the 16-byte header of a .eeprom file. One word is described as "Base of stack", another as "Initial stack Pointer". Question: Why are they different?

I've finally figured out that the difference between them is 4 x (number of parameters and local variables·in first PUB, including result variable), but I can't see why that should be.

Doesn't the stack grow upward in memory? Shouldn't the stack pointer start out pointing at the base of the stack? Why does the compiler need to reserve space below the stack?

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Michael Park

PS, BTW, and FYI:
To search the forum, use search.parallax.com (do not use the Search button).
Check out the Propeller Wiki: propeller.wikispaces.com/

Comments

  • CJCJ Posts: 470
    edited 2008-08-25 21:10
    (WOOPS, CJ!!!) I meant to respond and instead, I edited your post. Sorry about that. My intent was just to confirm what you said.)

    That's right. There is a return address already placed on the stack so that if your program 'return's, it branches to 3 bytes of ROM code that are COGSTOP(COGID). This is why the base and pointer vary.

    ·




    Post Edited By Moderator (Chip Gracey (Parallax)) : 8/25/2008 9:34:53 PM GMT
  • hippyhippy Posts: 1,981
    edited 2008-08-25 21:32
    Yes, it's something like that, I've never investigated further. When the PUB Main finishes it executes a 'return' and what's been preset below the stack cause execution to continue somewhere which leads to the CogStop happening. It's really a clever way of trapping stack underflow.

    Considering there's a constant offset between the two I'm not sure why both items appear in the header. I suspect it's got something to do with Cognew/CogInit ( which uses a similar header to launch a method in its own Cog ) or simply to save a byte or two in the Spin interpreter. It may have come about as an alternative to wasting an otherwise unused word which doesn't fit with the long alignment which follows; it's there, use it, one less thing needed to do the interpreter.
  • mparkmpark Posts: 1,305
    edited 2008-08-25 21:52
    But it's not a constant offset. It depends on the number of local variables in the first PUB method.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Michael Park

    PS, BTW, and FYI:
    To search the forum, use search.parallax.com (do not use the Search button).
    Check out the Propeller Wiki: propeller.wikispaces.com/
  • BradCBradC Posts: 2,601
    edited 2008-08-26 10:16
    mpark said...
    But it's not a constant offset. It depends on the number of local variables in the first PUB method.

    But of course.. the method still requires stack space to operate on all its local variables, so that must already be allocated on the stack prior to the method being started.
    The more variables defined, the longer the pre-allocated stack space area.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Pull my finger!
  • hippyhippy Posts: 1,981
    edited 2008-08-26 12:22
    I'd never noticed that, thanks !

    Time to fix some bugs I know I've got based around that mistaken belief ...
Sign In or Register to comment.