stackframe 不会从堆栈中删除?

stackframe dosen't get eliminated from the stack?

我编写了一个将输入打印到标准输出的 c 程序。然后我把它转换成汇编语言。顺便说一句,我使用的是 AT&T 语法。

这是简单的 C 代码。

#include <stdio.h>

int main()
{

int c; 


  while ((c = getchar ()) != EOF) 
    {

    putchar(c);

}

    return 0;
}

int c 是局部变量。

然后我把它转成汇编语言

.file   "question_1.c"
    .text
    .globl  main
    .type   main, @function 

//prolog

main:
    leal    4(%esp), %ecx
    andl    $-16, %esp
    pushl   -4(%ecx)
    pushl   %ebp
    movl    %esp, %ebp
    pushl   %ecx
    subl    , %esp    // we add 20 bytes to the stack
    jmp .L2
.L3:
    subl    , %esp
    pushl   -12(%ebp)
    call    putchar
    addl    , %esp
.L2:
    call    getchar
    movl    %eax, -12(%ebp)
    cmpl    $-1, -12(%ebp)
    jne .L3

//assumption this is the epilog
    movl    [=11=], %eax
    movl    -4(%ebp), %ecx
    leave
    leal    -4(%ecx), %esp
    ret
    .size   main, .-main
    .ident  "GCC: (Ubuntu 4.9.4-2ubuntu1) 4.9.4"
    .section    .note.GNU-stack,"",@progbits

通常在 epilog 中我们应该添加 20,因为在 prolog 中我们减去 20。 那么堆栈框架还在那里吗? 还是我漏掉了一个关键点?

我也有一个关于主要功能的问题。通常函数通常是 "called" 但它在汇编代码中出现在哪里?

提前谢谢你。

就在main标签之后,leal 4(%esp), %ecx%ecx中保存了四个加上堆栈指针。在例程结束时,leal -4(%ecx), %esp 将比保存值少 4 的值写入堆栈指针。这样直接恢复原来的值,而不是把减去的量加起来。