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

Assign a global variable to a register

Hello,

in my application I use often a global variable. Since this variable is use in lots of functions, I'd like to assign it to a specific register (like DR28). It is possible? If yes, how can I do such a thing?

Thanks

  • There is no mechanism to do this in C.

    Jon

  • C51 has a "Global Register Optimisation" option - see the REGFILE directive.
    Dunno if C251 has the same; if it does, you could try it.

    Warning: there's a bug in uVision v2.07 which means the 'Global Register Coloring' checkbox won't work if you have any spaces in your Project Name (everything else works; Keil just forgot the quotes on the REGFILE linker option!)

    NEIDE: "Not Entirely Integrated Development Environment" ;-)

  • If you use a global variable (long, an array etc..), assign it to a local variable in the function at function entry and move the value back to the global variable at function exit. This way you can come very close to your request.

    Here is a portition of code taken from an interrupt routine that is speed optimized (dual channel software tone generator for ISDN S0 bus) 64 PCM tone samples have to be copied to FIFO as fast as possible:

    Two dimensional global array:

    static unsigned int tone_idx[2];
    

    is assigned to a local variable idx that is assigned to a register by the compiler.

             ...
    	 idx = tone_idx[chan];
    	 for (i = 64; i; i--)
    	 {			      /*** DDS - Circular buffer fashion ***/
    	    idx += fr;
    	    if (idx >= 1000) idx -= 1000;
    				      /*** Copy tone samples to FIFOB	 ***/
    	    *fifob = PCM_Tab[idx];
    	 }
    	 tone_idx[chan] = idx;
             ...
    

    This optimization is few times faster than direct using of a global variable while the code size is halved.

    Franc

  • Thank you very much for your answers.

    I've tried it and it reduce my code size.

    Thanks again

    Vincent