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
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.