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 by the CC-RH, they are prefixed with "_" (underscore) when they are output to the assembler.

Table 9.1

Identifier

C

Assembler

func1 ( )

_func1

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-RH 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 source 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, however, make sure that the value of SP is retained before and after calling.

When using a register variable register in an assembler function, make sure that the register value is retained before and after the assembler function is called. In other words, save the value of the register variable register before calling the assembler function, and restore the value after calling.

The register for register variable that can be used differ depending on the register mode.

Table 9.2

Registers for Register Variables

Register Modes

Register for Register Variable

22-register mode

r25, r26, r27, r28, r29

32-register mode

r20, r21, r22, r23, r24, r25, r26, r27, r28, r29

(3)

Return address passed to C function

The CC-RH generates codes on the assumption that the return address of a function is stored in link pointer lp (r31). When execution branches to an assembler function, the return address of the function is stored in lp. Execute the jmp [lp] instruction to return to a C function.