I am using a Cygnal 8051 processor with the Keil compiler. Is there a way to tell the compiler to use the stack for local variables in functions instead of using dedicated memory location ? Thank you for help. Charles
Not for using the hardware stack. You can use the reentrant function attribute to use a software stack. See: http://www.keil.com/support/man/docs/c51/c51_le_reentrantfuncs.htm
Why do you want to do this?
The need to do this is to save memory space. Every time a function uses local variables it uses dedicated memory location either on the internal or external memory. Most compiler assigned local variables to the hardware stack. I understant that the stack is limited with the 8051, but it seems kind of a waste. It would be very helpful if the compiler would allow you to specify the hardware stack for the local variables. Charles
Local variables in the keil (and other) dedicated '51 compilers are saved based on a 'call tree'. In other words, teo functions that can not be called at the same time will have the local variables in the same place. This uses no more memory than saving on the stack and is sooooo much more efficient with the '51 architecture. Erik
"The need to do this is to save memory space." The Keil C51 uses a technique called Overlaying - this effectively achieves at compile-time what other compilers do on the stack at run-time. Let the compiler work the way it was designed - messing with it as you suggest will not save memory, and will seriously impact your peformance!
This knowledgebase article describes the Overlay scheme: http://www.keil.com/support/docs/1887.htm Note the introductory paragraph: "To provide the most efficient use of memory, automatic variables and function arguments are overlaid in memory according to a well-defined procedure." (my emphasis) The is also a good description in Application Note 129: Function Pointers in C51: http://www.keil.com/appnotes/docs/apnt_129.asp And refer to the description of the OVERLAY control in the Linker Manual
Most compiler assigned local variables to the hardware stack. I understant that the stack is limited with the 8051 Not only is the memory space limited, access to this region of memory must be indirect, and is thus less efficient than direct access to the lower half of the internal RAM. You'll get smaller and faster code if you let the compiler overlay auto variables in data space rather than in idata space. Note that compile-time stack analysis also results in more efficient code. Using a stack for procedure calls is something like doing dynamic typing. You have to execute code to add to a stack pointer, and you have to indirect accesses through the stack pointer to find your variables. As with any sort of dynamic feature and level of indirection, this gives you power at a cost. For many programs, the cost is unnecessary because you can resolve the addressing at compile time. For others, the stack is can be useful enough to earn its keep -- hence the reentrant keyword. You may be surprised at how little you actually need run-time allocation for auto variables in most cases. Most compilers use the stack because it's the model assumed by the design of most block-structured programming languages, including C. It's also easy to do. The speed and space costs are shrugged off as just part of the cost of using a high-level language. But you can have your cake and eat it, too, at least to some extent. Put the features of the toolchain to work for you, rather than fight them.