mixedC_asm430

mixedC_asm430 - Application Note SLAA140 March 2002 Mixing...

Info iconThis preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon
Application Note SLAA140 – March 2002 1 Mixing C and Assembler With the MSP430 Stefan Schauer MSP430 ABSTRACT This application note describes how C and assembler code can be used together within an MSP430 application. The combination of C and assembler benefits the designer by providing the power of a high-level language as well as the speed, efficiency, and low- level control of assembler. Contents 1 Definition of the IAR C-Compiler for Passing Variables Between Functions . .......................... 2 2 Requirements of Assembler Routines to Support Being Called From C. ................................. 3 3 Combining C and Assembler Functions . .................................................................................... 4 4 Building Libraries . ........................................................................................................................ 6 5 Using Watch Windows With Assembler Variables . .................................................................... 8 Figures Figure 1. Parameter Passing From C . ................................................................................................. 2 Tables Table 1. Location of Passed Parameters. .......................................................................................... 3
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
SLAA140. 2 Mixing C and Assembler With the MSP430 1 Definition of the IAR C-Compiler for Passing Variables Between Functions 1.1 Calling Convention - Register Usage With the IAR C-Compiler The compiler uses two groups of processor registers. The scratch registers R12 to R15 are used for parameter passing and hence are not normally preserved across the call. The other general-purpose registers, R4 to R11, are mainly used for register variables and temporary results and must be preserved across a call. Within C this is handled automatically. Note that the –ur45 option prevents the compiler from using registers R4 and/or R5. 1.2 Stack Frames and Parameter Passing Each function call creates a stack frame as follows: Parameters, Except First Two Return Address Saved Registers High Address Low Address Stack Pointer SP Stack Figure 1. Parameter Passing From C The parameters of a called function are passed to an assembler routine in a right to left order. The left most two parameters are passed in registers unless they are defined as a struct or union type, in which case they are also passed on the stack. The remaining parameters are always passed on the stack. See the following example of a call. f(w,x,y,z)
Background image of page 2
Mixing C and Assembler With the MSP430 3 Since the arguments are dealt with in a right to left order, z is loaded onto the stack first, followed by y, and x is either in R14, R15:R14, or on the stack, depending on its type, as is w . The result is returned in R12 (or R13:R12 for a 32 bit type) and in a special area pointed to by R12 if it is a struct or union type. Table 1. Location of Passed Parameters Argument <32 Bit Type 32 Bit Type Struct/Union 4th ( z) On the stack On the stack On the stack 3rd ( y) On the stack On the stack On the stack 2nd ( x) R14 R15:R14 On the stack 1st ( w) R12 R13:R12 On the stack Result R12 R13:R12 Special area 1.3 Interrupt Functions Interrupt functions written in C automatically preserve the scratch registers and SR (status register) as well as registers R4 to R11. The status register is saved as part of the interrupt calling process. Any registers used by the routine are then saved using push Rxx instructions.
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 21

mixedC_asm430 - Application Note SLAA140 March 2002 Mixing...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online