How do you decide how much memory can be forked off the stack for your own code?

Basic and Machine Language

Moderator: Moderators

Post Reply
User avatar
aitsch
Vic 20 Amateur
Posts: 62
Joined: Sun Mar 08, 2020 12:54 pm
Location: Germany NS

How do you decide how much memory can be forked off the stack for your own code?

Post by aitsch »

if I am correctly informed, you can store own code on the stack from address $0140 upwards.
How do you decide how much memory can be forked off the stack for your own code?

In my current project I have 77 bytes on the stack and I have no idea whether this is okay :roll:

What is your procedure for determining the maximum uncritical area?
User avatar
Mike
Herr VC
Posts: 5129
Joined: Wed Dec 01, 2004 1:57 pm
Location: Munich, Germany
Occupation: electrical engineer

Re: How do you decide how much memory can be forked off the stack for your own code?

Post by Mike »

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).
Post Reply