Date : Fri, 16 Dec 2011 15:23:46 +0000 (WET)
From : BBCMICRO@... (Peter Coghlan)
Subject: 32016 + 32082
>>
>>BBC BASIC uses the 6502 stack like any 6502 program but to avoid overflowing
>>it when processing (for example) recursive procedure or function calls, BASIC
>>saves the contents of the 6502 stack on its own larger stack and reinitialises
>>the 6502 stack pointer each time it processes a procedure or function,
>>restoring it on return.
>
>The interesting thing, though, is that it doesn't do this to
>avoid ordinary stack overflow due to deeply nested PROC/FN
>calls, but primarily for performance reasons. Theoretically,
>the BASIC stack would be a perfect replacement for the 6502
>stack, if it was used exclusively and consistently.
>
>But in the PROC/FN call overhead, where formal and actual
>parameters are bound and implicit LOCALs (i.e. formal
>parameters with pre-existing names) are saved, processing
>would be pretty slow if all the pushes and pops were done
>via the BASIC stack. And only because, even in that process,
>nesting can occur (since an actual parameter expression can
>contain FNs, which can contain yet other FNs, etc.), it needs
>to account for that, by pushing and popping the entire 6502
>stack (which is far quicker in the end).
>
It's been a while since I delved deeply into the BASIC ROM but Mark Plumbley
in his "BASIC ROM User Guide for the BBC Microcomputer and Acorn Electron",
seems to say fairly categorically that parameters and local variables
are pushed directly onto the BASIC stack after the contents of the 6502
stack has already been pushed onto the BASIC stack (however, PTRA, PTRB,
a count of the parameters and a token to indicate whether a PROC or FN
is being processed are subsequently pushed onto the 6502 stack).
As well as explicitly stating that "The 6502 stack is not very big - only 256
bytes - and saving it in this manner allows deep recursion of FNs and PROCs
without overflowing the small 6502 stack", he also states that processing a
PROC or FN results in a JSR to the statement interpreter while ENDPROC and
function return result in the execution of a corresponding RTS. This gives a
strong hint as to the basis for his statement.
Regards,
Peter Coghlan.