I am wondering if anybody knows why the most Apple II assembly subroutines save/restore registered via the stack (method 1 below), instead of using variables (method 2 below)?
And, what are the advantages and disadvantages are for each method?
Thanks a lot for any insights!
I'm interested in knowing because when writing code where speed is a priority tend to use variables and wondering if I'm missing an important consideration.
The parenthetical numbers in the comment fields below are the clock cycles for each opcode according to the following reference guide (it's on an atari site but since both Apple and Atari use 6502 my understanding is that the opcodes cycle times are the same)
http://www.atarimax.com/jindroush.atari.org/aopc.html
Here is what I've come up with so far:
Advantages (Stack)
======================
Less Memory
Slightly better code readability (I think mainly because it's the standard so
it's immediately obvious what's going on)
Advantages (Variables)
=======================
Speed
=====METHOD 1 (Stack)=====
;SAVE REGISTERS
TXA ;(2)
PHA ;(3)
TYA ;(2)
PHA ;(3)
<SUBROUTINE CODE>
;RESTORE REGISTERS
PLA ;(4)
TAY ;(2)
PLA ;(4)
TAX ;(2)
Total clock cycles: !22
Total Bytes: !16
=====METHOD 1 (Variables)=====
;SAVE REGISTERS
STX SAVED.XREG.LOCAL ;(4)
STY SAVED.YREG.LOCAL ;(4)
<SUBROUTINE CODE>
;RESTORE REGISTERS
LDX SAVED.XREG.LOCAL ;(4)
LDY SAVED.YREG.LOCAL ;(4)
Total clock cycles: !16
Total bytes !24 (for code) + 2 (for variables) = !26