home  |   the links mine  |   6502 primer   |   large math look-up tables   |   65c02 assembly structure macros   |   simple multitask   |   6502 interrupts   |   0-overhead Forth interrupts   |   stacks treatise   |   RS-232 primer   |   assembly relevant today   |   NMOS-CMOS 6502 differences


6502 STACKS: More than you thought

Forum posts show the need for a treatise on stacks (plural, not just the page-1 hardware stack).  The purpose here is to address the range of subjects regarding stacks, starting with the definition and gradually reaching a stage a little bit past intermediate use, mostly ignoring the loftier subjects like multi-user, multithreading, and multitasking systems (which the 6502 is not very well suited for anyway).

The processor referenced will be the 6502 and 65c02 unless otherwise noted.  There will be references to the 65816 in section 13 and many other isolated places.  The language is 65c02 assembly language.  A few sections have a lot of references to Forth because of how the programmer is in constant contact with its stacks, and because of what we can learn from it to apply in our assembly-language work.  There are other such languages, like RPL (reverse-polish Lisp) and PostScript which I am not qualified to speak on.

The reader should already be familiar with the 6502's programmer's model (ie, register types and sizes, etc.), instruction sets and addressing modes, and of course basic things like hexadecimal number notation, signed two's complement numbers, what a subroutine is, and what an interrupt is.  (There are links to my articles on interrupts at the top of this page.)  There are mentions and examples of macros.  It is not necessary for the reader to be familiar with macros yet, but I want to keep encouraging their use, as they make the programmer more productive, reduce bugs, and make code more maintainable, with absolutely no penalty in run speed or memory consumption in most cases.

Looking at other people's code can be a pain, but OTOH examples are a must; so I try to make them as generic and clear as possible.  Actually, the hardest part of writing this was coming up with simple, general-purpose examples that don't require giving a lot of background in a specialized application in order for the reader to understand them.  I hope to keep adding or improving examples.  Email me with suggestions.


Index to sections:

  1. definition and very basics
  2. subroutine return addresses and nesting
  3. interrupts (plus link to interrupts primer)
  4. virtual stacks and various ways to implement them
  5. stack addressing, both hardware and virtual
  6. passing parameters, and comparison of methods
  7. having a subroutine find inlined data, using the return address
  8. doing math and other operations by stacks in RPN
  9. RPN efficiency
  10. 65c02's added instructions that are useful in stacks
  11. using RTS, RTI, and JSR to synthesize other instructions
  12. where-am-I routines, for self-relocatable code
  13. a peek at the 65816's new instructions and capabilities that are relevant to stacks, and 65c02 code which partially synthesizes some of them
  14. local variables and environments
  15. recursion
  16. enough stack space?
  17. compiling or assembling program structures
  18. stack potpourri
  19. for further reading
Appendix A. StackOps.ASM
Appendix B. 816StackOps.ASM
Appendix C. RPN calculators, a production test application, assembly language, and Forth


Some of the abbreviations used in this treatise are:

Happy programming!

Subject to constant improvements!  If you find errors, typos, dead links, or inconsistencies, or want something else covered or something clarified, or want to suggest additional links, material, examples, photos, or diagrams, please email me at wilsonmines@dslextreme.com.  For discussion, use the forum.  For the reasons I do the website the way I do, see the end of this page.

As usual, there's no warranty on any of this material.  Not all of the subroutines are tested as-is, but other than the very shortest and simplest ones, most are at least a 1:1 translation from known-working material.  If you build a whole system from Appendix A or B, I can almost guarantee you will have some debugging to do.  It will still be worth it for many situations though, since much of the subroutine-writing has already been done for you.  (* * * * * Note: These two appendices are not finished, but I have not been able to work on them recently and I did not want to hold up the publication of the whole stacks treatise just for these.  Appendix A is about 75% done, and viewable.)

A special thanks goes toJeff Laughton who made the attractive animated diagrams!


Next--> definition and very basics

this index page last updated Jul 18, 2016    Written in 2013-2015.    Garth Wilson  wilsonmines@dslextreme.com