Those familiar with x86 assembly programming are very used to the typical function prologue / epilogue:
push ebp ; Save old frame pointer.
mov ebp, esp ; Point frame pointer to top-of-stack.
sub esp, [size of local variables]
...
mov esp, ebp ; Restore frame pointer and remove stack space for locals.
pop ebp
ret
This same sequence of code can also be implemented with the ENTER
and LEAVE
instructions:
enter [size of local variables], 0
...
leave
ret
The ENTER
instruction's second operand is the nesting level, which allows multiple parent frames to be accessed from the called function.
This is not used in C because there are no nested functions; local variables have only the scope of the function they're declared in. This construct does not exist (although sometimes I wish it did):
void func_a(void)
{
int a1 = 7;
void func_b(void)
{
printf("a1 = %d
", a1); /* a1 inherited from func_a() */
}
func_b();
}
Python however does have nested functions which behave this way:
def func_a():
a1 = 7
def func_b():
print 'a1 = %d' % a1 # a1 inherited from func_a()
func_b()
Of course Python code isn't translated directly to x86 machine code, and thus would be unable (unlikely?) to take advantage of this instruction.
Are there any languages which compile to x86 and provide nested functions? Are there compilers which will emit an ENTER
instruction with a nonzero second operand?
Intel invested a nonzero amount of time/money into that nesting level operand, and basically I'm just curious if anyone uses it :-)
References:
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…