Sharing header files between C and assembly code

The usual way to define constants in C code is to use #define, or in assembly code to use CMP directives. If your project contains a mixture of C and assembly code, there might be some constant definitions that are common to both. If so, to avoid maintaining two separate lists, you can create one list of common definitions and include them in both your C and assembly code.

To make common definitions, you can use C-style #include and #define directives directly in your assembly source code. You can pass this source code through the armclang C preprocessor. It outputs a preprocessed version of your assembly code which armclang can then assemble.

The following example shows how to do this.

  1. Add a header file called my_strcopy.h to the project, containing the following line:
    #define ONE_CONSTANT 1
  2. Add this line to the top of my_strcopy.s, created in the previous example:
    #include "my_strcopy.h"
  3. In my_strcopy.s, replace the occurrences of #1 with #ONE_CONSTANT, for example:

  4. Pass my_strcopy.s through the C preprocessor. If you tried to build the project without first doing this, Arm assembler 6 would report a syntax error for the #include statement you added to my_strcopy.s.

  5. Open the Project Settings dialog. Then, under C/C++ build, select Settings.
    In the Tool Settings tab, under Arm Assembler 6, select Preprocessor, then tick the box marked Preprocess input before assembling (- x assembler-with-cpp), as shown in the following image:
  6. The -x assembler-with-cpp option tells armclang that the assembly source file requires preprocessing. Now if you try to build the project, it will be able to successfully build it.
  7. If you need to pass other simple command-line options to the C preprocessor, for example –D,-U or –E, specify them in the field shown in Preprocessor window. Deatils of these options can be found in the Arm compiler 6 documentation.