如何将调用的lib里的函数放到指定地址

本文介绍如何在KeilC环境中为特定函数分配内存地址,包括.lib文件的使用、.M51文件的生成及解读、BL51选项卡下的地址设置等步骤,并解释了与main函数中子函数地址分配的区别。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值