Using assembly in Codewarrior C

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Using assembly in Codewarrior C

6,525 Views
SSK
Contributor I
Hello Forum members,
 
I am using Codewarrior V4.6 build 6345 for MC9S12GC32CPB16. My project is having some of the files in assembly for speed optimization. These assembly files need to access some constants and variables from C code and viceversa (from assembly to C). How do I pass/retrieve constants, variables to/from C and assembly? Can I use header files (.h) with assembly code for defining constants etc?
 
Thanks in advance.
Labels (1)
Tags (1)
0 Kudos
7 Replies

1,196 Views
J2MEJediMaster
Specialist I
Check out some of the project files located in the directory (CodeWarrior Examples). There are some code examples that use a mix of C and assembly.

---Tom

0 Kudos

1,196 Views
SSK
Contributor I
Many thanks forum members.
 
The assembler manual helped me to use assembly and C with Codewarrior.
Can I use header file (.h) in assembly code? Some of the examples from Codewarrior directory do use INCLUDE directive but with .INC file. Can this INCLUDE be used with .H files in assembly? Assembler is giving error if I try to use the same. Please clarify the problem.
 
Thanks in advance.
0 Kudos

1,196 Views
CrasyCat
Specialist III
Hello
 
You cannot include a .h file, which contains ANSI C syntax in an assembly source file.
You need to convert the declaration you need into something the macro assembler is able to understand.
 
For HC08 and HC12 you can get some help from the compiler.
If you use the pragma CREATE_ASM_LISTING ON and CREATE_ASM_LISTING OFF  in your ANSI C source file, the compiler can help you in creating an assembly include file containing the corresponding declaration.
 
Please take a look at the {Install}\Help\PDF\Manual_Compiler_HCI.pdf manual, chapter "Using the Compiler", section "Compiler Pragmas" -> "#pragma CREATE_ASM_LISTING: Create an Assembler Include File Listing" for more information.
 
If you decide to go this way, make sure the ANSI C files are build prior to the assembly source file.
You can adjust sequence in which the files are built in the project Link Order view.
 
CrasyCat
0 Kudos

1,196 Views
SSK
Contributor I
Hello,
 
I tried to use the header file in the form of .inc file that is my header file is having some defines. I incorporated these defines in .inc file using EQU. Then I used the saved .inc file in assembly to address the variable. It seems that it is working - no assembler error. Whether this is a correct way to use header file in the form of .inc file in an assembly code?
 
Thanks in advance.
0 Kudos

1,196 Views
CrasyCat
Specialist III
Hello
 
I would say that is the normal way of programming.
 
You can use any other extension for your assembly include file. Just make sure the content of the included file follows the Assembly language definition.
 
It should only contain valid assembler directives and/or instructions.
 
CrasyCat
0 Kudos

1,196 Views
bigmac
Specialist III
Hello,
 
Within your .asm file(s), you could export the names of specific assembly sub-routines required to be externally accessible to other files, either .asm or .c, using the XDEF assembler directive.  For example,
  XDEF  routine1, routine2
 
For any simple global variables defined within a .c file, that need to be imported for use by the assembly code,  use the XREF directive.  For example,
  XREF  gvar1, gvar2
Probably not a good idea to import a structure or a multi-dimensional array.
 
To provide the equates within the .asm file, for the specific device you are using, you will probably also need the line -
   INCLUDE  "derivative.inc"
or the .inc file that is incorporated by the project wizard.
 
Then within a .c or .h file, you will need prototypes for the assembly routines.  For example -
extern void *routine1(void);
 
When it came to using the sub-routines within a C function, I have previously use inline assembly,
__asm  JSR  routine1;
but presumably other methods are feasible.
 
This was previously applied to HCS08 coding, but I assume that the HCS12 will have similar assembler directives.
 
Regards,
Mac
 


Message Edited by bigmac on 2008-06-18 12:28 AM
0 Kudos

1,196 Views
CrasyCat
Specialist III
Hello
 
You can find information about mixing C and assembler in the assembler reference manual.
 
Please take a look at {Install}\Help\PDF\Assembler_HC12.pdf, in chapter "Using the HC(S)12 Assembler" section "Mixed C and Assembler Applications".
 
CrasyCat
0 Kudos