Calling Pure Assembly Language Functions

If you want C code to call assembly language files, you must specify a SECTION mapping for your code, for appropriate linking. You must also specify a memory space location. Usually, this means that the ORG directive specifies code to program memory (P) space.

In the definition of an assembly language function, the GLOBAL directive must specify the current-section symbols that need to be accessible by other sections.

The following listing is an example of a complete assembly language function. This function writes two 16-bit integers to program memory. A separate function is required for writing to P: memory, because C pointer variables allow access only to X: data memory.

The first parameter is a short value and the second parameter is the 16-bit address.

Listing: Sample code - Creating an assembly language function
                            ;”my_asm.asm”
  SECTION user              ;map to user defined section in CODE
  ORG P:                    ;put the following program in P
                            ;memory

  GLOBAL  Fpmemwrite        ;This symbol is defined within the
                            ;current section and should be
                            ;accessible by all sections
Fpmemwrite:
  MOVE   Y1,R0              ;Set up pointer to address
  NOP                       ;Pipeline delay for R0
  MOVE   Y0,P:(R0)+         ;Write 16-bit value to address
                            ;pointed to by R0 in P: memory and
                            ;post-increment R0
  rts                       ;return to calling function

  ENDSEC                    ;End of section
  END                       ;End of source program

The following listing shows the C calling statement for this assembly language function.

Listing: Sample code - Calling an assembly language function from C
void pmemwrite( short, short );/* Write a value into P: memory */

void main( void )
{
  // ...other code

  // Write the value given in the first parameter to the address
  // of the second parameter in P: memory
  pmemwrite( (short)0xE9C8, (short)0x0010 );

  // other code...
}