这个movl指令是否必要?

drd*_*dot 2 c assembly

我读了教科书"计算机系统程序员的观点".它给出了一个示例程序:

/* read input line and write it back */
void echo(){
  char buf[8]; /* way too small !*/
  gets(buf);
  puts(buf);
}
Run Code Online (Sandbox Code Playgroud)

汇编代码是:

1  echo:
2   pushl %ebp
3   movl  %esp, %ebp
4   pushl %ebx
5   subl  $20, %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  $20, %esp
12  popl  %ebx
13  popl  %ebp
14  ret
Run Code Online (Sandbox Code Playgroud)

第9行似乎没用,因为第7行已经在堆栈顶部存储了buf.然后它调用gets.返回时,buf将位于堆栈的顶部.

我的问题是:第9行在这里没用吗?

编辑:这是Linux.

Pas*_*uoq 5

我的问题是:第9行在这里没用吗?

不能.您不能假设gets不会更改堆栈上的值.它是它的参数,允许修改它.

%ebx另一方面是callee-save.该gets功能必须保留它.