没有合适的资源?快使用搜索试试~ 我知道了~
MPS430的C与汇编语言的混合编程
需积分: 13 13 下载量 144 浏览量
2009-04-21
17:13:24
上传
评论 1
收藏 169KB PDF 举报
温馨提示
MPS430的C与汇编语言的混合编程,单片机中C与汇编语言的混合编程,实例程序。
资源推荐
资源详情
资源评论
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
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)
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.
On exit, these registers are recovered using pop Rxx instructions and the RETI instruction is
used to reload the status register and return from the interrupt.
Functions written in assembler have to take special care of this.
2 Requirements of Assembler Routines to Support Being Called From C
An assembler routine that is to be called from C must do the following:
• Conform to the calling convention described above.
• Have a PUBLIC entry-point label.
• Be declared as external before any call, to allow type checking and optional promotion of
parameters, as in extern int foo() or extern int foo(int i, int j).
2.1 Local Storage Allocation
If the routine needs local storage, it allocates it in one or more of the following ways:
• On the hardware stack.
• In static workspace, provided of course that the routine is not required to be simultaneously
reusable (re-entrant).
Functions can always use R12 to R15 without saving them and R6 to R11 provided they are
pushed before use. R4 and R5 must not be used for ROM monitor compatible code.
SLAA140.
4 Mixing C and Assembler With the MSP430
If the C code is compiled with -ur45, but the application is not to run in the ROM monitor, then it
is possible to use R4 and R5 in the assembler routine without saving them, since the C code
never uses them.
2.2 Interrupt Functions
The calling convention cannot be used for interrupt functions, since the interrupt may occur
during the calling of a foreground function. Hence, the requirements for an interrupt function
routine are different from those of a normal function routine as follows:
• The routine must preserve all used registers, including scratch registers R12–R15.
• The routine must exit using RETI.
• The routine must treat all flags of the status register as undefined (Carry / Neg. / Zero /
Overflow).
2.3 Defining Interrupt Vectors
As an alternative to defining a C interrupt function in assembly language as described above, the
user is free to assemble an interrupt routine and install it directly in the interrupt vector.
The interrupt vectors are located in the INTVEC segment.
3 Combining C and Assembler Functions
3.1 General Basics
The mechanics to combine C and assembler functions are fairly straightforward. Basically C
code within .c files imports labels exported by the assembler files using the extern keyword.
Assembler codes within .s43 files export labels to the C code using the PUBLIC keyword.
Assembler code import labels exported by C code using the EXTERN keyword. No keyword is
required to export C labels to assembler code.
Once the .c and .s43 files are written, they are added to the workbench project and the project is
built. See the example .c, .s43, and project (.prj) files included with this application note. See the
IAR documentation for a more complete description of this process.
3.2 Calling Assembler Functions Without Parameters to Pass
If no parameters have to be passed between the C code and the assembler function, a simple
call instruction can be used. See example 1.
3.3 Calling Assembler Functions With Parameters to Pass
When it is required to pass parameters from C to the assembler function, the parameters must
be located as described in the Stack Frames and Parameter Passing section.
Example 2 shows how parameters are passed between the C main program and an assembler
function.
剩余20页未读,继续阅读
资源评论
violate
- 粉丝: 0
- 资源: 2
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- 【岗位职责说明书】100000709 财务高级主管.doc
- 【岗位职责说明书】100000708 人力资源高级主管.doc
- 【岗位职责说明书】100000713 安全保卫高级主管.doc
- 【岗位职责说明书】100000803 综合行政主管.doc
- 【岗位职责说明书】100000802 财务部副经理(分公司).doc
- 【岗位职责说明书】100000800 室(职能部室)副经理.doc
- 【岗位职责说明书】100000807 工商法律事务主管.doc
- 【岗位职责说明书】100000808 档案管理主管.doc
- 【岗位职责说明书】100000809 信息管理主管.doc
- 【岗位职责说明书】100000818 会计主管.doc
- 【岗位职责说明书】100000814 绩效考核主管.doc
- 【岗位职责说明书】100000820 审计主管.doc
- 【岗位职责说明书】100000813 薪酬主管.doc
- 【岗位职责说明书】100000812 培训主管.doc
- 【岗位职责说明书】100000815 员工关系主管.doc
- 【岗位职责说明书】100000816 社会保险主管.doc
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功