Writing generic subroutines in assembly
Posted: Mon Dec 10, 2007 10:00 pm
I've recently been learning 6502 assembly on my own while I wait for my first 65c02 based computer kit to arrive. Most of the sample code I look at tends to be very small to make it easier to learn. There is one area which (to me) seams missing. What is a good way of performing stack operations in a complex program?
The system stack will get used up very quickly; at least the way I generally write programs. So far my tiny sample programs have used the system stack just fine but anything really useful is going to eat it too quickly:
- C-style subroutine calls
- mathematical expression calculators (RPN calculator)
- Recursion
I saw an interesting technique that divided up the zero page into a sort of 'scratch area' and parameter area using the regular stack only for the subroutine call. This would extend the use a bit but still won't be good for recursion.
I could use the methods I've used on other systems and create my own program stack in regular memory and use JMP for subroutine calls and returns but this isn't as easy to read a JSR/RTS.
I even went to the trouble of creating a couple of subroutines that let me use the JSR/RTS for subroutine calls and returns. In essence I would 'move' data from the system stack to my processor stack right at the start of my subroutine and put it all back at the end. This would let me use JSR/RTS with my own stack but it cost extra processing time.
So, I'm curious what others are doing?
On a related not I haven't found much on how to create code that is relocatable at execution time but I've decided I probably won't need that functionality for a while.
The system stack will get used up very quickly; at least the way I generally write programs. So far my tiny sample programs have used the system stack just fine but anything really useful is going to eat it too quickly:
- C-style subroutine calls
- mathematical expression calculators (RPN calculator)
- Recursion
I saw an interesting technique that divided up the zero page into a sort of 'scratch area' and parameter area using the regular stack only for the subroutine call. This would extend the use a bit but still won't be good for recursion.
I could use the methods I've used on other systems and create my own program stack in regular memory and use JMP for subroutine calls and returns but this isn't as easy to read a JSR/RTS.
I even went to the trouble of creating a couple of subroutines that let me use the JSR/RTS for subroutine calls and returns. In essence I would 'move' data from the system stack to my processor stack right at the start of my subroutine and put it all back at the end. This would let me use JSR/RTS with my own stack but it cost extra processing time.
So, I'm curious what others are doing?
On a related not I haven't found much on how to create code that is relocatable at execution time but I've decided I probably won't need that functionality for a while.