发表于
2008-4-27 8:33:35
C51: ABSOLUTE FUNCTION ADDRESS
QUESTION
How do I locate a C function at an absolute address? I succeeded in variables using _at_, but it doesn't work for functions.
ANSWER
You must use the BL51 linker to locate functions at fixed addresses. To do so, make a note of the function name and the source file in which it is saved. The compiler creates a symbol name for the function using the following format:
?PR?function_name?file_name
So, the function named func in main.c would have a symbol name of:
?PR?FUNC?MAIN
Everything is uppercase to comply with the OMF51 standard. There may be derivatives of this naming convention. For example, if the function accepts arguments that can all be passed in registers, the function name is prefixed with an underscore ('_') and the above would be:
?PR?_FUNC?MAIN
You may look in the linker's M51 map file to figure out the symbolic names for your functions. Once you know the symbolic name, it is a simple matter of telling the linker where to locate it using the CODE directive. For example:
BL51 main.obj code(?pr?func?main(2000h))
Tells the linker to locate the function func at code address 0x2000. If you use the uVision2 IDE you may enter the starting address for segments under
Project - Options for Target. For the BL51 Linker/Locater enter the following code segment with the address under
BL51 Locate - Code. For the LX51 Linker/Locater enter it under
LX51 Locate - User Segments:
?pr?func?main(2000h)
http://www.keil.com/support/docs/359.htm以下部分来自C51BBS:
C51怎样将子程序段定位在1个固定的地址位置?
问:C51 怎样将1个子程序段定位在1个固定的地址位置?
以下2问题均要用C51解决
1。 怎样将1个子程序段定位在1个固定的地址位置?
例如将 INT BCD2HEX(INT XX)定位在1000H
2。 HOW在EEPROM 中固定的位置存放1字符串?
如在200H处放“COPYRIGHT 2001-11”
答: 函数定位与变量定位...
1、函数定位:
假如要把C源文件 tools.c 中的函数
int BIN2HEX(int xx)
{
...
}
放在CODE MEMORY的0x1000处,先编译该工程,然后打开该工程的M51文件,在
* * * C O D E M E M O R Y * * *
行下找出要定位的函数的名称,应该形如:
CODE xxxxH xxxxH UNIT ?PR?_BCD2HEX?TOOLS
然后在:
Project->Options for Target ...->BL51 Locate:Code
中填写如下内容:
?PR?_BCD2HEX?TOOLS(0x1000)
再次Build,在M51中会发现该函数已放在CODE MEMORY的0x1000处了
2、赋初值的变量定位:
要将某变量定位在一绝对位置且要赋初值,此时用 _at_ 不能完成,则如下操作:
在工程中建立一个新的文件,如InitVars.c,在其中对要处理的变量赋初值(假设是code变
量):
char code myVer = {"COPYRIGHT 2001-11"};
然后将该文件加入工程,编译,打开M51文件,若定义的是code型,则在
* * * C O D E M E M O R Y * * *
下可找到:
CODE xxxxH xxxxH UNIT ?CO?INITVARS
然后在:
Project->Options for Target ...->BL51 Locate:Code
中填入:
?CO?INITVARS(0x200)
再次编译即可。
相应地,如为xdata变量,则InitVars.c中写:
char xdata myVer = {"COPYRIGHT 2001-11"};
然后将该文件加入工程,编译,打开M51文件,在
* * * X D A T A M E M O R Y * * *
下可找到:
XDATA xxxxH xxxxH UNIT ?XD?INITVARS
然后在:
Project->Options for Target ...->BL51 Locate:Xdata
中填入:
?XD?INITVARS(0x200)
再次编译即可。相应地,若定义的是data/idata等变量,则相应处理即可。
3、若有多个变量或函数要进行绝对地址定位,则应按地址从低到高的顺序排列。http://www.c51bbs.com/show.aspx?id=61&cid=9