This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

How to force the placement of local (auto) variables in memory ?



Hello everyone, 

I am trying to use IAR for 8051 with a code which build with SDCC, and I do not manage to force the memory location of local (auto) variables as you can do with SDCC. It seems that to use memory attributes as __xdata, __idata, etc.., I have to declare them as global variables so outside the function. Am I right or am I just misunderstanding ?

So now I have one project that build with Large memory model but which is not really powerful, and another one with a Small memory model but I don't have enough IDATA space for my variables (Linker error)…

Thank's for your help.

  • There might be some tricks you can use. Take a look at the chapter "Auto variables—on the stack or in a static overlay area" on page 72++ in the IAR EW8051 Compiler Reference Guide. IAR support may also provide some help.

    Other than that, you're right. You cannot set memory attributes for auto variables unless they are also declared as static.

  • Could you explain the motivation behind doing such a thing?

    The easiest way to achieve such a thing (although not exactly what you are asking) would be to declare some storage in e.g. xdata statically and use it through pointers locally in a function.

    uint8 __xdata pVar;
    
    void someFunc (void)
    {
    
      uint8 * uVar = &pVar;
      *uVar = 0xC5;
    
    }

    Regards,
    Svend

  • Thank you for your quick answers.

    Well, I have chosen the Small memory model so all my auto variables are going to be located in the idata area, but i don't have enough space, so I am looking for a way to force some local variables to be located in the xdata area.

    I know I can declare them as global variables but it is not what I want to do.

    Other ideas ? 


    Thank's

  • Have you optimized the stack size for your application? All of the auto variable (except those that fit in registers) will in your case be placed on the data stack, which can be maximum 128 bytes. Do you really need more than 128 bytes for auto variables (taking into account nested func calls and isrs)? Larger variables should be static or global and be placed in XDATA. Only those variables that require very quick access should be placed in DATA/IDATA.

  • Yes I tried to change the value of the stack size but I still have the same error "Segement IDATA_I is too long for segment definition. At least 0x3b more bytes needed.."

  • So then the problem is not really the auto variables, but the other stuff you have placed in IDATA. Minimize the stack size to free up some space and move globals and static variables to XDATA (or PDATA).