DRW wrote:
I.e. the old function might have solved the issue that I had back then. (I didn't try it out since I used memcpy instead.) But it would still not have been the answer to my current question because if I rewrite a generic memcpy function myself, I would still want to use actual parameters nevertheless, not global variables.
Both use the global static variables in the same way, the only difference internally is that the assignment of ptr0/ptr1 happens at call time for the "static passing" version, and it happens inside the function for the "stack passing" version. Both functions use those two static variables, and the "stack passing" version is merely wasting its time putting them on the stack and taking them directly off the stack again before putting them in the two static variables.
They are both valid methods of having parameters to a function. Aside from the speed difference, the code difference is a few extra lines of assembly for the "stack passing" method, versus a few extra lines of declaration in a header for the "static passing" method.
The reason you don't see static passing as much is mainly just that on most platforms, local and stack based variables are cheap and fast. This is not the case for 6502, and it's doubly not the case for CC65 which uses its own software C-stack instead of the hardware stack (not sure why it has this design).
So, in the absence of a performance disadvantage, the stack passing idiom is a better default, just because it keeps all your variables locally contained, which is a nice feature, but it's really a very specific/situational advantage. With CC65 you are trading performance for that advantage, though. Are you favouring stack based parameters because you actually need local encapsulation, or is it just because it "feels more like C"?
I'm not saying that you should always do it one way or the other, and of course you should do what seems best for you. I'm just trying to explain why in this specific case the stack passing method seems wholly inferior to me.
Quote:
But if my thread topic is explicitly "How do I use function parameters in Assembly", then the fact that a new function differs in nothing but in the fact that it uses function parameters makes all the difference.
Yes, I wrote the modified version entirely as a demonstration of how to use C-stack based parameters from assembly.