User`s manual

Dynamic C Users Manual digi.com 183
11.4.4.3 Functions in Extended Memory
If the
xmem
keyword is present, Dynamic C compiles the function to extended memory. Otherwise, Dynamic C
determines where to compile the function. Functions compiled to extended memory have a 3-byte return address
instead of a 2-byte return address.
Because the compiler maintains the offsets automatically, there is no need to worry about the change of
offsets. The @SP approach discussed previously as a means of accessing stack-based variables works
whether a function is compiled to extended memory or not, as long as the C-language names of local vari-
ables and arguments are used.
A function compiled to extended memory can use IX as a frame reference pointer as well. This adds an
additional two bytes to argument offsets because of the saved IX value. Again, the IX+offset approach dis-
cussed previously can be used because the compiler maintains the offsets automatically.
11.5 C Calling Assembly
Dynamic C does not assume that registers are preserved in function calls. In other words, the function
being called need not save and restore registers.
11.5.1 Passing Parameters
When a program calls a function from C, it puts the first argument into HL (if it has one or two bytes) with
register H containing the most significant byte. If the first argument has four bytes, it goes in BC:DE (with
register B containing the most significant byte). Only the first argument is put into the primary register,
while all arguments—including the first, pushed last—are pushed on the stack.
11.5.2 Location of Return Results
If a C-callable assembly function is expected to return a result (of primitive type), the function must pass
the result in the “primary register.” If the result is an int, unsigned int, char, or a pointer, return
the result in HL (register H contains the most significant byte). If the result is a long, unsigned
long, or float, return the result in BCDE (register B contains the most significant byte). A C function
containing embedded assembly code may, of course, use a C return statement to return a value. A
stand-alone assembly routine, however, must load the primary register with the return value before the
ret instruction.
11.5.3 Returning a Structure
In contrast, if a function returns a structure (of any size), the calling function reserves space on the stack
for the return value before pushing the last argument (if any). Dynamic C functions containing embedded
assembly code may use a C return statement to return a value. A stand-alone assembly routine, how-
ever, must store the return value in the structure return space on the stack before returning.
Inline assembly code may access the stack area reserved for structure return values by the symbol
@RETVAL, which is an offset from the frame reference point.