This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Any way for declaring symbolic constants as an external number in C51 source code ?

Is there any way to declare a symbolic constant as 'an external number' in Keil C51 source code so that the compiler generates object file using instructions with # symbol prefix (e.g. mov r0,#symbol) and therefore the value of this symbol can be supplied on the linker level (from another obj file, where the symbol is given a value and declared as public) ?

(Including file with #defines into the source code does not solve the issue - symbols must be defined at the compile time.)

Note: in assembler it is easy ...

file1.a51 : (file1.obj defines particular values of needed constants for a concrete project)


...
public SYMBOL
SYMBOL  equ  5
...

file2.a51 : (file2.obj can be stored in a library being 'universal' and the SYMBOL value will be supplied by linker)

...
extrn number (SYMBOL)
...
  mov  a,#SYMBOL
...

Parents
  • I think Scott is true. Your suggestion looks (at the first sight) as if it solved one half of the issue - the compiler generates instructions like 'mov a,#...'. But remember that 'c' is declared as an external pointer to idata (or anywhere else). Then it should be defined as a public item in a different C module and what is important - initialized to an arbitrary required value. However, this is normally linker's job. What is wrong - the item is not a simple number then, it occupies some space in memory. (This second half of the problem can be solved defining and 'publishing' symbol 'c' in an asm module using equ statement, linker is then satisfied with the value and won't use any memory space for it. It's a bit violent but one of my colleagues says "a little bit of violence is not any fascism yet" :-)
    Nevertheless, I have been interested in purely C-based solution - if it exists...

    Stan

Reply
  • I think Scott is true. Your suggestion looks (at the first sight) as if it solved one half of the issue - the compiler generates instructions like 'mov a,#...'. But remember that 'c' is declared as an external pointer to idata (or anywhere else). Then it should be defined as a public item in a different C module and what is important - initialized to an arbitrary required value. However, this is normally linker's job. What is wrong - the item is not a simple number then, it occupies some space in memory. (This second half of the problem can be solved defining and 'publishing' symbol 'c' in an asm module using equ statement, linker is then satisfied with the value and won't use any memory space for it. It's a bit violent but one of my colleagues says "a little bit of violence is not any fascism yet" :-)
    Nevertheless, I have been interested in purely C-based solution - if it exists...

    Stan

Children
  • Sorry, I was replying to your orignal question of generating a "mov r0,#symbol" where the symbol is supplied on the linker level. I just assumed you had a assembly program that generated these constants.

    I know of no standard "C" way of doing what you ask.

    The only way I know is ugly. You have to export the constant as an address. The following will export the constant 0x1234.

    char xdata symbol _at_ 0x1234;
    

    However the linker now thinks there is a char allocated. It would have been nice if the following would work.

    char xdata symbol[] _at_ 0x1234;
    

    This would have let you have your cake and eat it too.