Keil Logo

Data Overlaying

Most C compilers store function arguments and local variables in a portion of the hardware stack called a stack frame. A base pointer is set on entry to a function and is used to access function arguments (on one side of the base pointer) and local variables (on the other side of the pointer). While this technique works well for devices in which the stack can be arbitrarily large is performs poorly on the 8051. The 8051 hardware stack is limited to a maximum of 256 bytes. As such, using stack frames on the 8051 is very wasteful of the limited memory available.

The Keil C51 C Compiler works with the LX51 Linker to store function arguments and local variables in fixed memory locations using well-defined names (so that function arguments are easily passed and accessed). The linker analyzes the structure of the program and creates a call tree which it uses to overlay the data segments containing local variables and function arguments. This technique works extremely well and provides the same efficient use of memory as a conventional stack frame. It is sometimes called a compile-time stack since the stack layout is fixed by the compiler and linker. An advantage of the compile-time stack is that arguments and local variables are accessed directly using fixed memory addresses (which is very fast on the 8051). Typical stack-based accesses are indirect and execute more slowly on the 8051.

While this technique is mostly automatic, there are a few situations that require special attention.

  • Indirect Function Calls (Function Pointers).
  • Tables or Arrays of Function Pointers.
  • Recursive Functions (functions that call themselves).
  • Reentrant Functions (functions invoked by more than one thread of execution — for example, a function called by main and an interrupt service routine.

Each of these situations requires special attention on your part and may require use of the linker OVERLAY directive.

  Arm logo
Important information

This site uses cookies to store information on your computer. By continuing to use our site, you consent to our cookies.

Change Settings

Privacy Policy Update

Arm’s Privacy Policy has been updated. By continuing to use our site, you consent to Arm’s Privacy Policy. Please review our Privacy Policy to learn more about our collection, use and transfers
of your data.