Do you/does it make sense to imitate function calls?

Are you new to 6502, NES, or even programming in general? Post any of your questions here. Remember - the only dumb question is the question that remains unasked.

Moderator: Moderators

User avatar
SusiKette
Posts: 125
Joined: Fri Mar 16, 2018 1:52 pm
Location: Finland

Re: Do you/does it make sense to imitate function calls?

Post by SusiKette » Mon Aug 12, 2019 6:31 am

Oziphantom wrote:
SusiKette wrote:

Code: Select all

ldx #$0110           ; Fixed point in stack
txs
that is a 16 bit load, not on a 6502 your not.. on a 65816, sure make as many stacks as you want around RAM for things. But then you also have ,s so...
Good point. I wrote those values with the absolute stack address in mind, not the stack pointer, and then forgot to change them before posting. They should be correct now.
Avatar is pixel art of Noah Prime from Astral Chain

User avatar
slembcke
Posts: 171
Joined: Fri Nov 24, 2017 2:40 pm
Location: Minnesota

Re: Do you/does it make sense to imitate function calls?

Post by slembcke » Wed Aug 14, 2019 11:31 am

It was kind of touched on by some people, but even cc65 doesn't strictly use the standard C calling convention by default where everything needs to be pushed onto the stack. Normally it uses fastcall. The last argument is placed in the A register (or AX for 16 bit values), and return value is also passed by A. Extra arguments are pushed to the stack.

I like to write a mix of C and assembly, and basically use fastcall for all my assembly routines whether they are exposed to C or not. Most of those only take a single argument, and maybe half of the remainders pass arguments through global zero page variables. As a convention it works out nicely because I only need to document the calling convention for routines that don't use fastcall. Expect the last argument and the return value in A/X. Pass other values on the C stack, or document the globals they are expected in.

This isn't too different from amd64 ABIs today. The System V and MS ABIs both pass a half dozen arguments through registers, and a separate half dozen float arguments. Looking at my own code, it's pretty rare for arguments to spill onto the stack.

supercat
Posts: 161
Joined: Thu Apr 18, 2019 9:13 am

Re: Do you/does it make sense to imitate function calls?

Post by supercat » Thu Aug 15, 2019 8:47 am

slembcke wrote:It was kind of touched on by some people, but even cc65 doesn't strictly use the standard C calling convention by default where everything needs to be pushed onto the stack. Normally it uses fastcall. The last argument is placed in the A register (or AX for 16 bit values), and return value is also passed by A. Extra arguments are pushed to the stack.
The compilers I've seen for compilers for platforms that would be even less capable of emulating a local-variable stack than the 6502 statically allocate local variables (automatic objects) based on a call graph; this requires that the compiler know all functions that may call others directly or indirectly, and disallows recursion. Functions can then simply use local variables for their parameters, with the slight caveat that if the calculation of more than one argument to a function call would require calling other functions, the results of all but one must be stored temporarily (either to compiler-temporary variables or the stack) and then copied to the appropriate arguments.

The linkers bundled with those compilers could automatically process the call graph information, but a tool to process such information into an ASM file for use with linkers that can't do that would be fairly simple. For every function, identify how many bytes of zero-page and non-zero-page RAM are used by the function plus the most demanding functions it calls. For leaf functions (those that don't call any other), this would simply be their own RAM usage. For each other function, find the maximum usage of functions that it calls and add its own RAM usage.

For many programs, using a small amount of zero-page for automatic variable storage could greatly improve performance compared with trying to use a (zp),y pseudo-stack.

Post Reply