Skip to main content

IAR Embedded Workbench for RH850 3.20.x

Calling assembler routines from C

In this section:

An assembler routine that will be called from C must:

  • Conform to the calling convention

  • Have a PUBLIC entry-point label

  • Be declared as external before any call, to allow type checking and optional promotion of parameters, as in these examples:

    extern int foo(void);

    or

    extern int foo(int i, int j);

One way of fulfilling these requirements is to create skeleton code in C, compile it, and study the assembler list file.

Creating skeleton code

The recommended way to create an assembler language routine with the correct interface is to start with an assembler language source file created by the C compiler.

Note

You must create skeleton code for each function prototype.

The following example shows how to create skeleton code to which you can easily add the functional body of the routine. The skeleton source code only needs to declare the variables required and perform simple accesses to them. In this example, the assembler routine takes an int and a char, and then returns an int:

extern int gInt;
extern char gChar;

int Func(int arg1, char arg2)
{
  int locInt = arg1;
  gInt = arg1;
  gChar = arg2;
  return locInt;
}

int main()
{
  int locInt = gInt;
  gInt = Func(locInt, gChar);
  return 0;
}

Note

In this example, we use a low optimization level when compiling the code to show local and global variable access. If a higher level of optimization is used, the required references to local variables could be removed during the optimization. The actual function declaration is not changed by the optimization level.

Compiling the skeleton code

Caution

In the IDE, specify list options on file level. Select the file in the workspace window. Then choose Project>Options. In the C/C++ Compiler category, select Override inherited settings. On the List page, deselect Output list file, and instead select the Output assembler file option and its suboption Include source. Also, be sure to specify a low level of optimization.

Danger

Use these options to compile the skeleton code:

iccrh850 skeleton.c -lA . -On -e

The -lA option creates an assembler language output file including C or C++ source lines as assembler comments. The . (period) specifies that the assembler file should be named in the same way as the C or C++ module (skeleton), but with the filename extension s. The -On option means that no optimization will be used and -e enables language extensions. In addition, make sure to use relevant compiler options, usually the same as you use for other C or C++ source files in your project.

The result is the assembler source output file skeleton.s.

Note

The -lA option creates a list file containing call frame information (CFI) directives, which can be useful if you intend to study these directives and how they are used. If you only want to study the calling convention, you can exclude the CFI directives from the list file.

Caution

In the IDE, to exclude the CFI directives from the list file, choose Project>Options>C/C++ Compiler>List and deselect the suboption Include call frame information.

Danger

On the command line, to exclude the CFI directives from the list file, use the option -lB instead of -lA.

Note

CFI information must be included in the source code to make the C-SPY Call Stack window work.

The output file

The output file contains the following important information:

  • The calling convention

  • The return values

  • The global variables

  • The function parameters

  • How to create space on the stack (auto variables)

  • Call frame information (CFI).

The CFI directives describe the call frame information needed by the Call Stack window in the debugger. For more information, see Call frame information.