If the foreground program and any possible interrupt routine have known stack requirements, the "free" area between $0140 and minimum stack pointer value can be found by thorough static analysis.
A more empirical method simply beforehand fills the stack with a signature byte, say $2A, and then does a reset. [1] Do a 'characteristic' run of the client program (sans the stack located stuff), and then
examine with a monitor which bytes still contain $2A. Maybe don't use that couple of unwritten-to bytes at the end of the range. IIRC, that was the method I used to check the available space for the code of my
View3K picture viewer (there, $0140..$01C2).
Alternatively, you can consider lowering the stack pointer right from program start to a lower value, say $A0. In principle, this gives you 96 free bytes in the range $01A0 .. $01FF. BASIC works fine with that reduced stack, just with fewer nestable GOSUBs, FORs and reduced complexity of evaluations (less nested parentheses). Some of the upper $01Fx bytes are used though by BASIC during certain float operations and while editing a BASIC line - all of which are of course of no importance for pure machine code.
[1] Like thus:
Code: Select all
SEI
LDA #$2A
LDX #$00
.loop
PHA
INX
BNE loop
JMP ($FFFC)
Note the stack pointer will simply wrap: in the end those 256 PHAs fill the whole address range $0100..$01FF, regardless the initial SP value. The reset routine in the KERNAL does not by itself clear/init the stack area (it does do so though for $00xx, $02xx, $03xx - see the RAMTAS routine at $FD8D).