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
#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.
- Add a header file called
my_strcopy.hto the project, containing the following line:
#define ONE_CONSTANT 1
- Add this line to the top of
my_strcopy.s, created in the previous example:
my_strcopy.s, replace the occurrences of
#ONE_CONSTANT, for example:
LDRB R2, [R1], #ONE_CONSTANT
my_strcopy.sthrough the C preprocessor. If you tried to build the project without first doing this, Arm assembler 6 would report a syntax error for the
#includestatement you added to
- 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:
- 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.
- 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.