Mixing C and assembler modules
Examining the calling convention
Adding an assembler module to the project
Setting up the project
In some projects it might be necessary to write certain pieces of source code in assembler language. The tutorial first demonstrates how the compiler can be helpful in examining the calling convention, which you must be familiar with when calling assembler modules from C/C++ modules or vice versa. Furthermore, this tutorial demonstrates how you can easily combine source modules written in C with assembler modules, but the procedure is applicable to projects containing source modules written in C++, too, if your product package supports C++.
This tutorial assumes that you are familiar with the basics of the IAR Embedded Workbench® IDE described in the previous tutorials.
Examining the calling convention
When you write an assembler routine that is called from a C routine, you must be aware of the calling convention that the compiler uses. If you create skeleton code in C and let the compiler produce an assembler output file from it, you can study the produced assembler output file and find the details of the calling convention.
In this example you will make the compiler create an assembler output file from the file Utilities.c.
1
Create a new project in the same workspace tutorials as used in the previous tutorial project, and name the project project2.
2
Add the files Tutor.c and Utilities.c to the project.
To display an overview of the workspace, click the Overview tab available at the bottom of the Workspace window. To view only the newly created project, click the project2 tab. For now, the project2 view should be visible.
3
To set options, choose Project>Options, and select the General Options category. On project level, the default factory settings should be used in this tutorial. Click OK.
4
Choose Project>Options. You will notice that only the C/C++ Compiler and Custom Build categories are available.
5
In the C/C++ Compiler category, select Override inherited settings and verify these settings:
Level: None (Best debug support)
Note: In this example you must use a low optimization level when you compile the code, to show local and global variable accesses. If you use a higher level of optimization, the required references to local variables might be removed. However, the actual function declaration is not changed by the optimization level.
6
Click OK and return to the Workspace window.
7
Compile the file Utilities.c. You can find the output file Utilities.sxx in the subdirectory projects\debug\list.
8
To examine the calling convention and to see how the C or C++ code is represented in assembler language, open the file Utilities.sxx.
You can now study where and how parameters are passed, how to return to the program location from where a function was called, and how to return a resulting value. You can also see which registers an assembler-level routine must preserve.
Note: The generated assembler source file might contain compiler-internal information, for example CFI directives. These directives are available for debugging purpose and you should ignore these details.
To obtain the correct interface for your own application functions, you should create skeleton code for each function that you need.
For more information about the calling convention used in the compiler, see the IAR C/C++ Compiler Reference Guide.
Adding an assembler module to the project
This tutorial demonstrates how you can easily create a project containing both assembler modules and C modules. You will also compile the project and view the assembler output list file.
Setting up the project
1
Modify project2 by adding the Utilities.sxx (Utilities.s for ILINK) file that you just created and removing the Utilities.c file.
Note: To view assembler files in the Add files dialog box, choose Project>Add Files and choose Assembler Files from the Files of type drop-down list.
2
Select the project level node in the Workspace window, choose Project>Options. Use the default settings in the General Options, C/C++ Compiler, and Linker categories. Select the Assembler category, click the List tab, and select the option Output list file.
Click OK.
3
Select the file Utilities.sxx in the Workspace window and choose Project>Compile to assemble it.
Assuming that the source file was assembled successfully, the file Utilities.rxx (Utilities.o for ILINK) is created, containing the linkable object code.
Viewing the assembler list file
4
Open the list file by double-clicking the file Utilities.lst available in the Output folder icon in the Workspace window.
The end of the file contains a summary of errors and warnings that were generated.
For further details of the list file format, see the IAR Assembler Reference Guide.
5
Choose Project>Make to relink project2.
6
Start C-SPY to run the project2.dxx (Utilities.out for ILINK) application and see that it behaves like the application in the previous tutorial.
Exit the debugger when you are done.