Stacks and Subroutines
Lee Marshall
Posts: 106
I have a couple questions:
Firstly:
what gets loaded into cog 0 at startup? the Main Routine? the whole program??
Secondly:
Where is the stack held(address) for cog0 at startup?
i assume it needs a stack, as whenever you start a new cog, you need to provide a stack address.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Holy parallel processing, Batman!
Firstly:
what gets loaded into cog 0 at startup? the Main Routine? the whole program??
Secondly:
Where is the stack held(address) for cog0 at startup?
i assume it needs a stack, as whenever you start a new cog, you need to provide a stack address.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Holy parallel processing, Batman!
Comments
Secondly ... The stack is placed at the end of all your program code and data when downloaded. A word of Hub memory in a pre-defined location indicates where its start is, and this is set by the Propeller Tool as part of the compilation process.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Holy parallel processing, Batman!
2) Neither the boot loader nor the Spin interpreter use a stack. Assembly programs do not require a stack. The call/return mechanism used in the "real" instruction set doesn't use a stack. When you startup a new cog using Spin, the new cog actually gets loaded with a copy of the Spin interpreter. The Spin program makes use of (requires) a stack which is supplied by the COGNEW/COGINIT call. When starting an assembly program, the "stack" parameter is actually a general purpose 14 bit parameter that can be used in any way, but usually is used to pass an address to the assembly program.
from what hippy said, it seems that the propeller tool defines a stack address upon compilation. is there some way to tell what address this is?
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Holy parallel processing, Batman!
The Spin Interpreter running in a Cog does not itself use a stack held in Hub memory. Spin code doesn't "run", only in a notional sense; a Cog runs the Interpreter which uses what the Spin bytecode is to determine what to do and that includes manipulating the stack used by Spin program.
The first 16 bytes of all Propeller object code are initialisation used to configure after bootload and reset. The base of the stack used is held as a word in bytes $000A (LSB) and $000B (MSB) of the .eeprom image.
The Propeller Tool knows the last address used by the program. The default stack (used by your program when it first starts) begins just beyond the end of your program. Its address is stored by the Propeller Tool in locations $E/$F in the program and the Spin interpreter picks it up from there during its initialization. The address in locations $A/$B is normally the "base" of the stack while that in $E/$F is the actual stack pointer value. There are some special cases where they might be different.
Post Edited (Mike Green) : 10/4/2007 5:17:41 PM GMT
is there some way to make the prop, at startup, load code from main ram(asm) into cog 0, rather than load the spin interpreter??
is it part of the initialization bytes at the top of the memory??
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Holy parallel processing, Batman!
What most third party 'Propeller Assemblers' will do is prefix this bit of Spin Code to the assembled code, so once the Propeller is reset, bootloads from Eeprom, the Assembler code runs as if it itself had been bootloaded.
Somewhere on the forum there is an example from Chip of how this Spin Code Prefix can be coded to use minimal Propeller resources.