the following snippet is often used when said to be allocate storage for local variables
当被称为为局部变量分配存储时,通常使用以下代码段
addl $8,%esp //allocate 8-byte storage
push %ebx //store some value onto the stack
why not simply push the value onto the stack,but rather allocated some space in advance?
为什么不简单地将值推入堆栈,而是提前分配一些空间?
2 个解决方案
#1
If you're asking why doesn't a compiler generate push instructions for local storage rather than direct manipulation of the stack pointer, it's a matter of efficiency.
如果你问为什么编译器不会为本地存储生成推送指令而不是直接操作堆栈指针,那就是效率问题。
Automatic variables (in C anyway) are uninitialized, so the code sequence would be (I'm going to use subl since I'm used to stacks growing down in memory):
自动变量(无论如何都是C)是未初始化的,所以代码序列将是(我将使用subl,因为我习惯于在内存中堆积成长):
C Code Actual assembly Your suggestion
void x(void) {
int a; subl 8,%esp push 0
int b; push 0
: : : blah blah blah blah
}
My answer is that it's unnecessary and inefficient in cases such as:
我的答案是,在以下情况下,这是不必要的和低效的:
C Code Actual assembly Your suggestion
void x(void) {
int a[100]; subl 400,%esp push 0
push 0
push 0
: : :
push 0
: : : blah blah blah blah
What you propose may make sense for something like:
您的建议可能对以下内容有意义:
C Code Your suggestion
void x(void) {
int a = 7; push 7
int b = 9; push 9
int c[4]; subl 16,%esp
: : : blah blah
}
#2
So that the allocated space is a fixed size, which is simplest for the compiler as the memory can be accessed with "DWORD PTR [ebp+(offset)]". The usual function prologue is something like:
因此,分配的空间是固定大小,这对于编译器来说是最简单的,因为可以使用“DWORD PTR [ebp +(offset)]”访问存储器。通常的功能序言是这样的:
(Intel syntax) push ebp add ebp, 8 mov ebp, esp
(Intel语法)push ebp add ebp,8 mov ebp,esp
for a function with 8 bytes of local variables.
对于具有8个字节的局部变量的函数。
#1
If you're asking why doesn't a compiler generate push instructions for local storage rather than direct manipulation of the stack pointer, it's a matter of efficiency.
如果你问为什么编译器不会为本地存储生成推送指令而不是直接操作堆栈指针,那就是效率问题。
Automatic variables (in C anyway) are uninitialized, so the code sequence would be (I'm going to use subl since I'm used to stacks growing down in memory):
自动变量(无论如何都是C)是未初始化的,所以代码序列将是(我将使用subl,因为我习惯于在内存中堆积成长):
C Code Actual assembly Your suggestion
void x(void) {
int a; subl 8,%esp push 0
int b; push 0
: : : blah blah blah blah
}
My answer is that it's unnecessary and inefficient in cases such as:
我的答案是,在以下情况下,这是不必要的和低效的:
C Code Actual assembly Your suggestion
void x(void) {
int a[100]; subl 400,%esp push 0
push 0
push 0
: : :
push 0
: : : blah blah blah blah
What you propose may make sense for something like:
您的建议可能对以下内容有意义:
C Code Your suggestion
void x(void) {
int a = 7; push 7
int b = 9; push 9
int c[4]; subl 16,%esp
: : : blah blah
}
#2
So that the allocated space is a fixed size, which is simplest for the compiler as the memory can be accessed with "DWORD PTR [ebp+(offset)]". The usual function prologue is something like:
因此,分配的空间是固定大小,这对于编译器来说是最简单的,因为可以使用“DWORD PTR [ebp +(offset)]”访问存储器。通常的功能序言是这样的:
(Intel syntax) push ebp add ebp, 8 mov ebp, esp
(Intel语法)push ebp add ebp,8 mov ebp,esp
for a function with 8 bytes of local variables.
对于具有8个字节的局部变量的函数。