




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、Assembly and C ross Compiling in Keil uvision,It is depend on the compile Tool, And the direction is privided in the help files of the compile Tool void dummy(void) /* 这是无返回值的函数*/ unsigned char x,y; x = 1; y = foo(x); /*这是一个空函数,没有定义操作*/ ,#pragma SRC directs the C compiler to generate assembly code w
2、hen the file is compiled. The extension of the assembly file will be src. If you view the src file you will see how to call the function foo from assembly. It shows the registers or memory locations used to pass the function arguments and which registers or memory locations are used to return a valu
3、e. In addition it also gives you the correct function naming convention to use, which is essential for interfacing assembly to C. You can then use the src file as a template to write the function call in your own assembly code. Note that you must also include the EXTRN directive for the function, eg
4、: EXTRN CODE (_foo),Interfacing C to Assembler,You can easily interface your programs to routines written in 8051 Assembler. The A51 Assembler is an 8051 macro assembler that emits object modules in OMF51 format. By observing a few programming rules, you can call assembly routines from C and vice ve
5、rsa. Public variables declared in the assembly module are available for your C programs. There are several reasons to call an assembly routine from your C program. You have assembly code already written that you wish to use. You need to improve the speed of a particular function. You want to manipul
6、ate SFRs or memory-mapped I/O devices directly from assembly. This topic describes how to write assembly routines that can be directly interfaced to C programs. For an assembly routine to be called from C, it must be aware of the parameter passing and return value conventions used in C functions. Fo
7、r all practical purposes, it must appear to be a C function.,CALLING ASSEMBLY ROUTINES FROM C,QUESTION: 从C中调用汇编函数 In the C51 compiler manual, there is example of an assembly module calling a C function. Is there any example of a C program calling an assembly routine? ANSWER: There are no examples in
8、 the book,but it is easy to create your own. The asm routine must know how parameters are passed, values returned, and the naming conventions of segments. The steps you must follow to create an example are outlined below: Write a simple function in C that passes parameters and returns values the way
9、 you want your assembly routine to. Use the SRC directive (#PRAGMA SRC at the top of the file) so that the C compiler generates a .SRC file instead of a .OBJ file. Compile the C file. Since the SRC directive was specified, the .SRC file is generated. The .SRC file contains the assembly code generate
10、d for the C code you wrote. Rename the .SRC file to a .A51 file. Edit the .A51 file and insert the assembly code you want to execute into the body of the assembly function shell included in the .A51 file.,For example, the following code,#pragma SRC unsigned char my_assembly_func ( unsigned int argum
11、ent) /*这是有返回值的函数*/ / Insert dummy lines to access all args and retvals return (argument + 1); when compiled generates the following assembly SRC file.,For example, the following code,NAME TESTCODE ?PR?_my_assembly_func? TESTCODE SEGMENT CODE PUBLIC _my_assembly_func ; ; #pragma SRC ;unsigned char my
12、_assembly_func ( RSEG ?PR?_my_assembly_func? TESTCODE USING 0 _my_assembly_func: ;- Variable argument?040 assigned to Register R6/R7 - ; ;SOURCE LINE # 2 ; unsigned int argument) ; ; SOURCE LINE # 4 / Insert dummy lines to access all args and retvals ; return (argument + 1);,; SOURCE LINE # 5 MOV A,
13、R7 INC A MOV R7,A ; ; SOURCE LINE # 6 ?C0001: RET ;END OF _my_assembly_func END,C51: MIXING C AND ASSEMBLY,Information in this article applies to: C51 Version 6.02 Vision Version 2.06 ,ANSWER,The following example program shows how to mix C and assembly in your 8051 programs. This example starts wit
14、h a MAIN C function which calls a routine in assembly which then calls a C function. The MAIN C module appears as follows:,QUESTION Do you have any examples of how to mix C and assembly?,extern void a_func (void); void main (void) a_func (); The function a_func is an assembly routine: NAME A_FUNC ?P
15、R?a_func?A_FUNC SEGMENT CODE EXTRN CODE (c_func) PUBLIC a_func RSEG ?PR?a_func?A_FUNC a_func: USING 0 LCALL c_func RET END,Note that this assembly routine calls c_func which is a C function: void c_func (void) . The actual code for the assembly module was generated using the SRC pragma and the follo
16、wing C source file: extern void c_func (void); void a_func (void) c_func (); ,C51: ADDING C FUNCTIONS TO EXISTING ASSEMBLER CODE,Information in this article applies to: C251 Version 1 and Later C51 Version 5 and Later QUESTION Im using the Keil C51 Tools and have successfully written 8051 programs o
17、nly in assembly language. Now I need add C code to my assembly program. I want to keep as much as possible of my existing assembly code. Are there some guidelines on how to mix assembly and C code using the Keil C51 compiler? How can I access memory locations from my C programs that I have reserved
18、in assembly code? ,ANSWER,The Keil C51 Compiler generates relocateable object files that can interface easily to relocateable assembler programs. The file KeilC51ASMTEMPLATE.A51 shows you how to structure relocateable assembly modules. This file defines several variables, for example: PUBLIC data_va
19、riable data_seg_name SEGMENT DATA ; segment for DATA RAM. RSEG data_seg_name ; switch to this data segment data_variable: DS 1 ; reserve 1 Bytes for data_variable xdata_seg_name SEGMENT XDATA ; segment for XDATA RAM RSEG xdata_seg_name ; switch to this xdata segment xdata_array: DS 500 ; reserve 500
20、 Bytes for xdata_array You access these variables in your C code with:,Answer,extern unsigned char data_variable; extern unsigned xdata xdata_array500; void test (void) xdata_arraydata_variable = 1; / write 1 to memory addressed by xdata_array+data_variable; ,To reduce data RAM requirements of your
21、application, the BL51/Lx51 Linker/Locater performs call tree analysis of the complete application program. Therefore, the linker needs to be able to distinguish between constants and program code and it must also be able to determine the local data segments for your program code. For this reason you
22、 should change the segment naming conventions of your assembler program to the segment names used by the C51 Compiler. Detailed information can be found in Application Note 149: Data Overlaying and Code Banking with A51 Assembler Modules.,For each interrupt function written in assembly, you must cre
23、ate a separate segment for the interrupt vector using CSEG AT.,CSEG AT 03H ; EXT0 interrupt vector SETB mybit ; interrupt function code RETI CSEG AT 0BH ; Timer 0 interrupt vector LJMP timer0isr ?BI?timer0?isr_module SEGMENT BIT OVERLAYABLE ; bit segment RSEG ?BI?timer0?isr_module isrbit: DBIT 1 ?PR?timer0?
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 济南中考二模数学试卷
- 近几年初一下数学试卷
- 2025年城市更新视角下历史文化街区保护与城市更新项目投资研究报告
- 2022年霍林郭勒市四年级语文第四单元考试试卷
- 肿瘤精准医疗在临床实践中的应用效果与2025年应用前景报告
- 2022年东宁市六年级语文第四单元考试试卷
- 2022年高密市六年级语文第六单元考试试卷
- 教育隐私保护与数据安全研究-洞察及研究
- 2022年登封市一年级语文第三单元考试试卷
- 咖啡连锁品牌2025年市场扩张战略布局与品牌竞争情报研究报告
- 航天航空科普知识单选题100道及答案解析
- 化妆造型全套课件
- 电竞比赛规则与裁判执行标准
- 新版中国食物成分表
- GB/T 15843.2-2024网络安全技术实体鉴别第2部分:采用鉴别式加密的机制
- 煤矿整合关闭风险评估方案
- 七年级上学期有理数训练题
- 陪诊服务协议
- 产品代理合同协议(2024版)
- 高考英语核心高频688词
- 保洁员工劳务合同范本
评论
0/150
提交评论