拆卸C函数

use*_*297 1 c disassembly

我试图理解以下函数的反汇编代码.

void func(char *string) {
    printf("the string is %s\n",string);
}
Run Code Online (Sandbox Code Playgroud)

下面给出了反汇编代码.

1) 0x080483e4 <+0>:     push   %ebp  
2) 0x080483e5 <+1>:     mov    %esp,%ebp  
3) 0x080483e7 <+3>:     sub    $0x18,%esp  
4) 0x080483ea <+6>:     mov    $0x80484f0,%eax  
5) 0x080483ef <+11>:    mov    0x8(%ebp),%edx  
6) 0x080483f2 <+14>:    mov    %edx,0x4(%esp)  
7) 0x080483f6 <+18>:    mov    %eax,(%esp)  
8) 0x080483f9 <+21>:    call   0x8048300 <printf@plt>
Run Code Online (Sandbox Code Playgroud)

谁能告诉我4-7行是什么意思(不是文字解释).为什么在第3行的堆栈上分配24个字节?

小智 6

基本上这里发生了什么:

4) 0x080483ea <+6> : mov $0x80484f0,%eax
Run Code Online (Sandbox Code Playgroud)

加载的地址"the string is %s\n"进入eax.

5) 0x080483ef <+11>: mov 0x8(%ebp),%edx
Run Code Online (Sandbox Code Playgroud)

将参数移动stringedx.

6) 0x080483f2 <+14>: mov %edx,0x4(%esp)
Run Code Online (Sandbox Code Playgroud)

推的值edxstring进栈,的第二个参数printf

7) 0x080483f6 <+18>: mov %eax,(%esp)
Run Code Online (Sandbox Code Playgroud)

按价值eax"the string is %s\n"进栈的第一个参数printf,然后它会调用printf.

sub $0x18,%esp 没有必要,因为函数没有局部变量,gcc似乎喜欢创造额外的空间但老实说我不知道​​为什么.