9.2 Calling of Assembly Language Routine from C Language

This section explains the points to be noted when calling an assembler function from a C function.

(1)

Identifier

If external names, such as functions and external variables, are described in the C source, the CC-RL adds them prefix "_" (underscore) in the assembly program.

Table 9.1

Identifier

C

Assembly Program

func1 ( )

_func1

 

Add prefix "_" to the identifier when defining functions and external variables with the assembler and remove "_" when referencing them from a C function.

(2)

Stack frame

The CC-RL generates codes on the assumption that the stack pointer (SP) always indicates the lowest address of the stack frame. Therefore, the address area lower than the address indicated by SP can be freely used in the assembler function after branching from a C function to an assembler function. Conversely, if the contents of the higher address area are changed, the area used by a C function may be lost and the subsequent operation cannot be guaranteed. To avoid this, change SP at the beginning of the assembler function before using the stack.

At this time, furthermore, make sure that the value of SP becomes equal both at the beginning and the end of the assembler function.

Registers can be used freely in assembler functions (register values do not have to be saved before usage and restored after usage).

(3)

Return address passed to C function

The CC-RL generates codes on the assumption that the return address of a function is stored at the top of the stack. When execution branches to an assembler function, the return address of the function is stored at the top of the stack. Execute the ret instruction to return to a C function.

(4)

Prototype declaration of assembler function

In the CC-RL, a prototype declaration is necessary for functions called from a C function. Make a correct prototype declaration even for assembler functions.