6500 Calling convention
The 6500 calling convention is a set of rules that dictate how functions receive parameters and return values, how the stack is managed, and how registers are used. This ensures that code generated by different compilers can interoperate.
Key Points of the 6500 Calling Convention
Registers:
General-purpose registers: A (Accumulator), X, Y.
Stack pointer: SP.
Program counter: PC.
Status register: P.
Parameter Passing:
Parameters are typically passed in memory or via the stack.
The caller pushes the arguments onto the stack before calling the function.
Return Values:
The primary return value is placed in the Accumulator (A).
Stack Management:
The stack is managed using the Stack Pointer (SP).
The caller is responsible for cleaning up the stack after the function call.
Callee-saved Registers:
The callee must save and restore any registers it uses.
Example
Here is an example of a simple function in 6500 assembly that adds two integers:
Explanation (1)
The function
addtakes two integer arguments from memory locations$00and$01.It adds the values, storing the result in memory location
$02.The
RTSinstruction returns to the caller.
Example with Stack Usage
Here is an example of a function that uses the stack to store local variables:
Explanation (2)
The function
sum_arraytakes two arguments: the array length in memory location$01and the array starting at memory location$02.It saves the Accumulator, X, and Y registers on the stack.
It initializes the sum and index to 0.
It enters a loop to iterate over the array, loading each element, adding it to the sum, and incrementing the index.
After the loop, it restores the registers from the stack and returns.
These examples illustrate the basic principles of the 6500 calling convention, including register usage, parameter passing, and stack management.