Re: [cc65] cc65 calling conventions

From: Ullrich von Bassewitz <uz1musoftware.de>
Date: 2004-09-05 13:13:12
Hi!

On Thu, Sep 02, 2004 at 12:05:37AM +0200, Sidney Cadot wrote:
> Is there documentation regarding the calling conventions used by CC65?

Some information is available in the docs, but it's spread in several files
and not complete.

> I need to mix C and assembly and I like to play by the rules.

Most of it has already been answered:

Parameters are pushed from left to right onto the parameter stack. In presence
of a prototype, all data is pushed after the necessary promotions have been
applied, which means that the data is converted to the type expected by the
called function. If no prototype has been encountered, the integer promotions
are applied, which means that chars are not passed as chars, but as integers.

In case of a fastcall function, the rightmost parameter is passed in
registers, which means A/A+X/A+X+sreg depending on the data type. Characters
are passed as such, which means that the content of the X is indeterminate.

Returned values are passed in A+X/A+X+sreg depending on the data type.
Standard integer promotions must be applied, which means that characters are
passed back as integers, so the contents of X must be valid.

The following zero page locations are not saved across function calls and can
be used freely by any asm subroutine that is called from C code:

  * tmp1, tmp2, tmp3, tmp4 (1 byte locations)

  * ptr1, ptr2, ptr3, ptr4 (2 byte locations)

  * sreg (two byte location, contains the high word of 32 bit values)

Note: Because these memory cells are not saved across function calls, calling
other C functions from your asm code may destroy them.

Assembler functions from the runtime library have their own usage patterns,
which must be determined by looking at the source. In most cases it depends on
common use. For example, since incsp... is often called on function exit, it
won't touch any CPU register, so it is safe to load the return value of a
function, and then jump to one of the incsp... functions to clean up the stack.

Regards


        Uz      (back from denmark any trying to catch up)


-- 
Ullrich von Bassewitz                                  uz@musoftware.de
----------------------------------------------------------------------
To unsubscribe from the list send mail to majordomo@musoftware.de with
the string "unsubscribe cc65" in the body(!) of the mail.
Received on Sun Sep 5 13:13:16 2004

This archive was generated by hypermail 2.1.8 : 2004-09-05 13:13:25 CEST