-->

How to write function at particular memory locatio

2019-09-08 17:42发布

问题:

How to write function at particular memory location in flash memory? Is there any directive for that? Do i need particular linker?

回答1:

If you are using keil ide you can place a function at a specific address using .ARM.__at_address as the section name. To place the function add at 0x20000, specify:

int add(int n1,int n2) __attribute__((section(".ARM.__at_0x20000")));
int add(int n1,int n2)
{
    return n1+n2;
}


回答2:

Do you use the keil toolchain?
If yes, perhaps http://www.keil.com/support/docs/359.htm helps.

Edit: The .obj file is generated by the compiler. I am not sure what you mean with 'how can i configure .obj file'.

The linker mentioned above takes the obj files, links them together and places code and variables.

You should start with a project which compiles and links without errors. Then you have:
- Some c files. One of them with your function.
- A linkfile with the settings for the linker.
- A makefile or some kind of batchfile which calls compiler and linker with the necessary arguments.

If you have that, you can look into the m51 file for the name of the symbol for your function. The m51 file is a textfile generated by the lx51 linker with interesting information about which symbols are there and what the linker has done with them.

The keil documentation for the linker I mentioned says: The compiler creates a symbol name for the function using the following format: ?PR?function_name?file_name.

This means: You will find the names of all functions of your project in the m51 file. If your function is in file file_x and named func_x. The symbol name will be PR?func_x?file_x

In http://www.keil.com/support/man/docs/lx51/lx51_segments.htm you can find some information about the usage of the SEGMENTS directive of the lx51 linker. According to that:

SEGMENTS (PR?func_x?file_x(C:0x1234))

should place your function to address 0x1234 in code memory.

Actually I have no keil toolchain. Therefore I cannot test all that myself. But I am sure that you can manage that yourself if you start with a simple working example, change things step by step and check what happens.

Good Luck.
Helmut



回答3:

Use ORG directive.

For example, for a function to start at location 2000H

ORG 2000H
MY_FUNC:
: YOUR CODE HERE
RET


标签: linker 8051 keil