Software Stack

Tired of only having 4 Levels of Nested Gosub's ?? Or, maybe you want to write a "Recursive" routine in PicBasic Pro? (MPASM required) Now you can !!!

swStack.inc is an include file that will allow you to create up to a 48 level Software Stack on a 14-bit Core PIC.

The include file gives 2 Macro Commands:

  • swGosub - Software Gosub
  • swReturn - Return from Software Gosub

The Macro's are used almost exactly like PBP Gosub and Return, but since they are Assembly language the syntax is a little bit different.

PBP Example of normal Gosub:

    Sub1:
         Hserout ["Hello"]
    Return

    Gosub Sub1

swGosub Example:

    Sub1:
         Hserout ["Hello"]
    @ swReturn

    @ swGosub _Sub1           ; Note the underscore before Sub1

The Stack size must be set using the Constant "StackSize", prior to including swStack.inc It is limited by the largest Word Array that you can fit into 1 bank, which varies, depending on the PIC chip used. With some modifications, that maximum could be increased.

Status of the Stack can be monitored with these variables that are defined in the swStack.inc file:

  • StackPTR Var Byte ' Pointer to current Top of Stack
  • StackFull Var bit ' if=1, you cannot do another swGosub
  • StackEmpty var bit ' if=1, you cannot do a swReturn
  • SToverflow var bit ' swGosub called when Stack was Full
  • STunderflow var bit ' swReturn called when Stack was Empty

An overflow or underflow does not reset the PIC like a Hardware overflow would. It simply won't execute the Gosub or Return. So you will either need to make sure it never happens, or handle the error in your program. You could also modify the swStack.inc file, and have it reset the PIC on an over/underflow. **

For an example of how to use swStack.inc, see the Test_swStack.pbp program.

Thanks again to JohnB for solving my forward declaration problem.

Hope this helps your program.

Darrel Taylor

Page last modified on March 08, 2018, at 02:24 AM