1. 在工程中添加所需的.lib文件,然后运行编译,生成.M51文件:
2. 打开.M51文件,找到使用函数的segment name:
3. 根据segment name,在KeilC 的BL51选项卡下按照自己的需求进行指定地址设置,指定地址指令如下:
设置完成后,rebulid工程,在新生成的.M51文件中确认是否按要求更改函数地址:
注意事项:
与直接分配main函数中的子函数到指定地址的操作不同处在于,BL51中的指令部分:
是: ?PR?ADD1?CHAR(0X3900)
而不是: ?PR?ADD1?MAIN(0X3900)
因为add1()函数是char.obj中的函数(char.obj是lib中的一个obj),不是main.obj中的函数!
KEIL C将程序编译的过程如下:
1. 通过EDIT编写.c文件或汇编文件;
2. 经过compiler(C51或A51)将.c文件或汇编文件生成.obj文件。此时的.obj文件是relocatable;
3. 此时可以将.obj文件生成.lib文件,供以后的main函数.c文件调用;
第三步指的是1、2步中的.c文件不带main函数,即前两步中的文件是用来作lib程序的。如果前两步的.c文件是主函数,第三步可忽略。
4. 通过Linker工具(BL51),将第2步生成的.obj文件生成有固定地址的obj文件;
5. 通过编译器将第4步生成的obj module转换成需要的.hex文件或debuger文件或仿真文件。
BL51 Introduction
The BL51 Linker/Locator creates an absolute object module by linking together object modules created using the Keil A51 Assembler, C51 Compiler, Intel ASM-51 Assembler, and Intel PL/M-51 Compiler. Object modules created by these tools are relocatable and cannot be directly executed (even if they consist of only one source module). They must be linked and converted into an absolute object modules using the linker.
The following figure shows how C51 object modules flow through the Keil tool chain.
The object module generated by the BL51 Linker is an absolute object module. All relocatable sections are assigned and located at fixed addresses.
The object module includes information required for initializing global variables, zero-initializing global variables, program code and constants, as well as symbolic information, line number information, and other debugging details.
The object module may be used by the OH51 Object-HEX Converter to create an Intel HEX file for programming into ROM. It may be loaded directly into an emulator or the Keil µVision Simulator/Debugger for testing and debugging.
Note
- By default, the object file generated by the linker has no file extension.
reference: http://www.keil.com/support/man/docs/bl51/bl51_intro.htm
http://www.keil.com/support/man/docs/bl51/bl51_ln_objfile.htm