A question that delves into the realm of retrocomputing and low-level programming!
For both the Z80 and 6502 processors, the concept of a "standard calling convention" is somewhat fluid due to the diversity of operating systems, programming languages, and application-specific requirements. However, common practices and de facto standards emerged for each processor. Keep in mind that variations were common depending on the specific platform, compiler, or assembler being used.
Z80 Calling Conventions:
The Z80, widely used in home computers like the ZX Spectrum, Amstrad CPC, and in the Game Boy, had several calling conventions in use, but here's a commonly adopted set of practices:
-
Register Usage:
- AF (Accumulator and Flags): Often used for return values, but since it's also used for conditional jumps, its value might need to be preserved or explicitly saved/restored.
- BC, DE, HL: These register pairs were commonly used for passing parameters to subroutines. The order might vary (e.g.,
HL for pointers or most significant parameters, DE for less significant ones, and BC for counters or less critical data).
- IX, IY (Index Registers): Used for indirect addressing and sometimes for passing parameters, especially when the subroutine needed to modify values indirectly.
- Stack (SP): Used for local variable storage, parameter passing in some conventions (especially for larger datasets or variable numbers of parameters), and for the return address.
-
Calling and Returning:
- Call Instruction:
CALL address
- Return Instruction:
RET for simple returns, or RET cc (e.g., RET Z) for conditional returns based on the flag status.
- Parameter Passing: Typically through registers (as mentioned) or on the stack for more complex routines.
- Return Values: Usually in the Accumulator (
A) or in HL for 16-bit values.
6502 Calling Conventions:
The 6502, found in the Commodore 64, Apple II, and NES, among others, had its own set of common practices:
-
Register Usage:
- A (Accumulator): Frequently used for return values.
- X, Y (Index Registers): Used for parameter passing, especially when indexing into tables or arrays, and for return values in some cases.
- Stack (SP): Paramount for parameter passing (due to the limited number of registers), local variable storage, and the return address. Parameters were often pushed onto the stack in reverse order.
-
Calling and Returning:
- Call Instruction:
JSR address
- Return Instruction:
RTS
- Parameter Passing: Primarily through the stack. Parameters were pushed in reverse order (similar to the x86's C calling convention), so the first parameter to be pushed is the last one specified in the call.
- Return Values: Typically in the Accumulator (
A) for byte values or in memory locations designated by the caller.
Note on Variability:
- Custom Conventions: Many applications, especially games or demos on these platforms, optimized for size or speed, leading to custom calling conventions.
- Compiler-Specific: Different compilers (e.g., CC65 for the 6502, or SDCC for the Z80) might enforce their own calling conventions.
- Platform-Specific: Operating systems or platform-specific libraries might also dictate their own standards for interoperability.
When working with legacy code or developing for these platforms, it's crucial to understand or define the calling convention used to ensure compatibility and correctness. While these practices were common, the flexibility of 8-bit programming meant developers often adapted conventions to suit specific needs.