这个 movl 指令是必要的吗?
Is this movl instruction necessary?
我看了课本"Computer Systems A programmer's perspective"。它给出了一个示例程序:
/* read input line and write it back */
void echo(){
char buf[8]; /* way too small !*/
gets(buf);
puts(buf);
}
它的汇编代码是:
1 echo:
2 pushl %ebp
3 movl %esp, %ebp
4 pushl %ebx
5 subl , %esp
6 leal -12(%ebp), %ebx
7 movl %ebx, (%esp)
8 call gets
9 movl %ebx, (%esp) // This does not look useful for me
10 call puts
11 addl , %esp
12 popl %ebx
13 popl %ebp
14 ret
第 9 行在这里似乎没有用,因为第 7 行已经将 buf 存储在堆栈的顶部。然后它调用gets。当得到return时,buf就会在栈顶。
我的问题是:
第9行在这里没用吗?
编辑:这是 Linux。
My question is: Is line 9 useless here?
没有。您不能假设 gets
不会更改堆栈上的值。是它的参数,允许修改。
%ebx
另一方面 is callee-save。 gets
函数必须保留它。
当您调用 gets 函数时,它可能会修改参数的值(在您的情况下为 %ebx)。因为你没有提到代码的其他部分,所以不能说那个..但是在函数调用后再次将 buff 存储到堆栈顶部是很好的..
我看了课本"Computer Systems A programmer's perspective"。它给出了一个示例程序:
/* read input line and write it back */
void echo(){
char buf[8]; /* way too small !*/
gets(buf);
puts(buf);
}
它的汇编代码是:
1 echo:
2 pushl %ebp
3 movl %esp, %ebp
4 pushl %ebx
5 subl , %esp
6 leal -12(%ebp), %ebx
7 movl %ebx, (%esp)
8 call gets
9 movl %ebx, (%esp) // This does not look useful for me
10 call puts
11 addl , %esp
12 popl %ebx
13 popl %ebp
14 ret
第 9 行在这里似乎没有用,因为第 7 行已经将 buf 存储在堆栈的顶部。然后它调用gets。当得到return时,buf就会在栈顶。
我的问题是: 第9行在这里没用吗?
编辑:这是 Linux。
My question is: Is line 9 useless here?
没有。您不能假设 gets
不会更改堆栈上的值。是它的参数,允许修改。
%ebx
另一方面 is callee-save。 gets
函数必须保留它。
当您调用 gets 函数时,它可能会修改参数的值(在您的情况下为 %ebx)。因为你没有提到代码的其他部分,所以不能说那个..但是在函数调用后再次将 buff 存储到堆栈顶部是很好的..