Shop OBEX P1 Docs P2 Docs Learn Events
program calling for BS2 — Parallax Forums

program calling for BS2

ArchiverArchiver Posts: 46,084
edited 2001-05-10 22:25 in General Discussion
Hi Tracy,

A single byte stack does make life easy but may be too limited for a large
program
spread over multiple banks. I have also tried some recursive programming for
which
a multilevel stack is required. Putting the ParentID and ReturnID into one
byte simplifies
much. Using a nibble for the ReturnID and using one branch instruction does
not only affect return points. More specific: the sum of return points plus
function
entries is limited to 16. So I could have 3 calls and 13 functions in one
program.
This might be enough. Using bit3 of the ParentID to distinguish between
calling and
returning allows me to have 16 calls plus 16 function entries per program.
This requires
the two branch lists and the code at the start of each program. It really is
a matter
of requirements and what you want.

Just to see how much optimization the one branch approach might give I
adapted the
scheme to your suggestion but preserved the multilevel stack.
I got rid off the directrun var, I use the first entry in the branch list
for a direct run.
A direct run must therefore be executed using TargetID! Leaves 15 entries
per program for
calls and functions.

The returnvalues are lost, except for bit3 of the runID which I use to
return a flag
to the parent program. The nesting level is increased to 15!
The overhead for the multilevel stack compared to a one level stack is one
stackpointer
increment statement per call plus one stackpointer decrement statement per
program,
which is really minimal. The use of variables is reduced to one byte and one
nibble.


Greetings, peter
Any more suggestions are welcome.


'Method for program gosub using scratchpad as program stack

'scratchpad usage (TOS stands for Top Of Stack)
'TOS+ 0 = not used
'TOS+ 1 = entryID/parentID 1
'TOS+ 2 = entryID/parentID 2
'TOS+ 3 = entryID/parentID 3
'TOS+ 4 = entryID/parentID 4
'TOS+ 5 = entryID/parentID 5
'TOS+ 6 = entryID/parentID 6
'TOS+ 7 = entryID/parentID 7
'TOS+ 8 = entryID/parentID 8
'TOS+ 9 = entryID/parentID 9
'TOS+10 = entryID/parentID 10
'TOS+11 = entryID/parentID 11
'TOS+12 = entryID/parentID 12
'TOS+13 = entryID/parentID 13
'TOS+14 = entryID/parentID 14
'TOS+15 = entryID/parentID 15


'program 0

TopOfStackBS2 con 63-16 'top of stack fot BS2
TopOfStackBSX con 127-16 'top of stack for SX, E, P

TOS con TopOfStackBSX 'pick the right top of stack

'global variables
TargetID var byte 'entry index and program to run
runID var TargetID.lownib 'program to run
entryID var TargetID.highnib 'entry index
StackPointer var nib 'stackpointer

ProgramID con 0

Begin0:
branch entryID,[noparse][[/noparse]main0,entry01,...,entry0F] 'first entry for direct run

main0:
'init
main0loop:

' ...

StackPointer=StackPointer+1
put TOS+StackPointer,$10+ProgramID 'constant: entryID in highnib, runID in
lownib
TargetID=$E4 'constant: entryID in highnib, runID in lownib
run TargetID 'execute entry 14 in program 4, then return to entry01
entry01:
if TargetID.bit3 then ... 'check return value
' ...

StackPointer=StackPointer+1
put TOS+StackPointer,$20+ProgramID 'constant
TargetID=$67 'constant
run TargetID 'execute entry 6 in program 7, then return to entry02
entry02:
if TargetID.bit3 then ... 'check return value

' ...

TargetID=$03
run TergetID 'run program 3 directly (no call but jump)

main0end:
goto main0loop

'function entries for program calls

entry03:
gosub function_00
goto ReturnFromProgramCall_0

entry04:
gosub function_01
goto ReturnFromProgramCall_0

' ...

ReturnFromProgramCall_0:
get TOS+StackPointer,TargetID 'get TargetID
StackPointer=StackPointer-1 'as all are retrieved, adjust stack here
runID.bit3=0 'set return value
run TargetID 'return to parent program

'subroutines

function_00:
return

function_01:
return

end

'Program 1
'etc.

Comments

  • ArchiverArchiver Posts: 46,084
    edited 2001-05-10 22:25
    Do you actually have to keep track of where you left off in every memory
    space, or do you just want to go back where you came from after you run
    another program?

    Original Message

    > A single byte stack does make life easy but may be too limited for a large
    > program
    > spread over multiple banks. I have also tried some recursive programming
    for
    > which
    > a multilevel stack is required. Putting the ParentID and ReturnID into one
    > byte simplifies
    > much. Using a nibble for the ReturnID and using one branch instruction
    does
    > not only affect return points. More specific: the sum of return points
    plus
    > function
    > entries is limited to 16. So I could have 3 calls and 13 functions in one
    > program.
    > This might be enough.
Sign In or Register to comment.