Hello, sci4me.
The implementation of a 'variable' takes on different forms, depending on the context.
On a small microprocessor like the 6502, room for a stack frame is rather limited and awkward to access
(the 65816 is much more capable in this area), so a typical function parameter is passed in a register
or register pair. This works for most simple tasks, like printing an integer to stdout or similar. Parameters
that won't fit in a register pair may be passed on the stack, or 'by reference', using a register pair as an
address to a parameter block.
It is much more common to treat the 6502's zero-page (addresses $0000 to $00ff) as a 'global' variable
pool, where each resident program (the monitor, interpreter, operating system, editor, user program)
stakes a claim to some of these locations. Failure to cleanly share and cooperate over this resource
results in lock-ups and crashes for the affected program(s), but proper documentation and careful
planning can keep these to a minimum.
A program that resides in RAM and wants to remain self-contained may reserve an area within itself
for variable storage, but this is a bit slower than zero-page, and simply won't work in the case of
pointers, unless self-modification is employed.
Programs that wish to use recursion will have to make very frugal use of the system stack, implement
their own software stack, or figure out a way to solve the problem using iteration.
These are rather general statements, but should give you an idea of what is involved. Perhaps others
could link you to some example outputs from their own libraries, but my VTL02 source shows how this
can be done in zero-page for a program that pretty much has complete control of the machine. VTL02
grabs 128-bytes of zero-page ($0080 to $00ff) for its own use, and ignores the fact that Applesoft and
DOS will be clobbered, since it does not depend on them for anything, and assumes that it will remain in
full control of the machine until the next RESET. It does have to cooperate with the monitor for character
input and output, so it knows to stay away from the zero-page variables used by these routines to keep
track of things like the current row and column of the cursor, the display height and width, the pointers
used for scrolling the text display, and (outside of zero-page) the text display RAM itself.
Happy programming!
Mike
Attachment:
vtl02a2b.asm [26.02 KiB]
Downloaded 76 times