13.07.2015 Views

MSP430 Optimizing C/C++ Compiler v 3.0 User's Guide (Rev. C

MSP430 Optimizing C/C++ Compiler v 3.0 User's Guide (Rev. C

MSP430 Optimizing C/C++ Compiler v 3.0 User's Guide (Rev. C

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

www.ti.com6.4.3 Accessing Arguments and Local VariablesInterfacing C and <strong>C++</strong> With Assembly LanguageA function accesses its local nonregister variables indirectly through the stack pointer (SP or R1) and itsstack arguments. The SP always points to the top of the stack (points to the most recently pushed value).Since the stack grows toward smaller addresses, the local data on the stack for the C/<strong>C++</strong> function isaccessed with a positive offset from the SP register.6.5 Interfacing C and <strong>C++</strong> With Assembly LanguageThe following are ways to use assembly language with C/<strong>C++</strong> code:• Use separate modules of assembled code and link them with compiled C/<strong>C++</strong> modules (seeSection 6.5.1).• Use assembly language variables and constants in C/<strong>C++</strong> source (see Section 6.5.2).• Use inline assembly language embedded directly in the C/<strong>C++</strong> source (see Section 6.5.4).6.5.1 Using Assembly Language Modules With C/<strong>C++</strong> CodeInterfacing C/<strong>C++</strong> with assembly language functions is straightforward if you follow the calling conventionsdefined in Section 6.4, and the register conventions defined in Section 6.3. C/<strong>C++</strong> code can accessvariables and call functions defined in assembly language, and assembly code can access C/<strong>C++</strong>variables and call C/<strong>C++</strong> functions.Follow these guidelines to interface assembly language and C:• You must preserve any dedicated registers modified by a function. Dedicated registers include:– Save-on-entry registers (R4-R10)– Stack pointer (SP or R1)If the SP is used normally, it does not need to be explicitly preserved. In other words, the assemblyfunction is free to use the stack as long as anything that is pushed onto the stack is popped back offbefore the function returns (thus preserving SP).Any register that is not dedicated can be used freely without first being saved.• Interrupt routines must save all the registers they use. For more information, see Section 6.6.• When you call a C/<strong>C++</strong> function from assembly language, load the designated registers witharguments and push the remaining arguments onto the stack as described in Section 6.4.1.Remember that a function can alter any register not designated as being preserved without having torestore it. If the contents of any of these registers must be preserved across the call, you mustexplicitly save them.• Functions must return values correctly according to their C/<strong>C++</strong> declarations. Double values arereturned in R12 and R13, and structures are returned as described in Step 1 of Section 6.4.1. Anyother values are returned in R12.• No assembly module should use the .cinit section for any purpose other than autoinitialization of globalvariables. The C/<strong>C++</strong> startup routine assumes that the .cinit section consists entirely of initializationtables. Disrupting the tables by putting other information in .cinit can cause unpredictable results.• The compiler assigns linknames to all external objects. Thus, when you are writing assembly languagecode, you must use the same linknames as those assigned by the compiler. See Section 5.10 for moreinformation.For identifiers to be used only in an assembly language module, ensure that they do not conflict with aC/<strong>C++</strong> identifier. Use a$oraconsistent prefix naming convention to create a different namespace forassembly identifiers.• Any object or function declared in assembly language that is accessed or called from C/<strong>C++</strong> must bedeclared with the .def or .global directive in the assembly language modifier. This declares the symbolas external and allows the linker to resolve references to it.Likewise, to access a C/<strong>C++</strong> function or object from assembly language, declare the C/<strong>C++</strong> object withthe .ref or .global directive in the assembly language module. This creates an undeclared externalreference that the linker resolves.SLAU132C–November 2008 Run-Time Environment 103Submit Documentation Feedback

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!