Table of Contents        Previous topic       Next topic       

C COMPILER->In Line Assembler Code->Accessing Local Variables and Function Parameters->OverView

The location and method of accessing local variables and function parameters depends upon the memory model being used and whether or not the function is reentrant or non-reentrant. Using the _asm syntax with the variable name withing curly braces is the preferred method of access.

It makes no difference if a variable is a local variable or a function parameter, they are accessed in the same way.  In the following paragraphs the term local variable will include function parameters.

Local variables are positioned in memory relative to a stack frame.  If the function is reentrant, the stack frame is dynamic and is created at run time at entry to the function and destroyed at exit from the function.  If the function is non-reentrant the stack frame is static.  It is determined by the linker and remains fixed at run time.

For the 8051, a static stack frame is much more efficient because the compiler can reference directly into the stack frame.  A dynamic stack requires the compiler to load the stack pointer and then add or subract an offset.  Exactly the same contraints apply to embedded assembler code that is accessing local variables  for static stack frames the compiler can give you the exact address whereas for dynamic stack frames the compiler can only give you the offset.

The following examples illustrate the methods of accessing local variables.  The objective in the examples is to load the value of the variable into either the accumulator or the carry flag.

The predefined macros _X_I51TM, _X_I51MM, _X_I51SM, _X_I51LM and _X_I51NR can be used determine the memory model and reentrant/non-reentrant attribute.