zoukankan      html  css  js  c++  java
  • 汇编代码看过程调用

    源代码如下:

    #include <stdio.h>
    
    int add(int x,int y)
    {
        int t = x + y;
        return t;
    }
    
    int main(void)
    {
        int x=4, y=1;
        return add(x,y);
    }

    经过 gcc -S hello.c

    产生汇编代码如下:

    add:
        pushl   %ebp
        movl    %esp, %ebp
        subl    $16, %esp
        movl    12(%ebp), %eax
        movl    8(%ebp), %edx
        leal    (%edx,%eax), %eax
        movl    %eax, -4(%ebp)
        movl    -4(%ebp), %eax
        leave
        ret
    main:
        pushl   %ebp  //其实有两步,subl $4 %esp,mov %ebp (%esp),保存当前的栈低
        movl    %esp, %ebp  //得到新栈低,将当前栈顶赋予栈低
        subl    $24, %esp
        movl    $4, -4(%ebp)  //x入栈
        movl    $1, -8(%ebp)  //y入栈
        movl    -8(%ebp), %eax  
        movl    %eax, 4(%esp)  //实参x入栈
        movl    -4(%ebp), %eax 
        movl    %eax, (%esp)   //实参y入栈
        call    add
        leave
        ret

    若采用优化 gcc -O2  -S hello.c

    产生汇编代码如下:

    add:
        pushl   %ebp
        movl    %esp, %ebp
        movl    12(%ebp), %eax
        addl    8(%ebp), %eax
        popl    %ebp
        ret
    main:
        pushl   %ebp  //保存当前栈低
        movl    $5, %eax
        movl    %esp, %ebp  //得到新栈低
        popl    %ebp  //过程调用结束,恢复旧栈低
        ret
  • 相关阅读:
    逆元(费马小定理求法)
    CodeForces
    lower_bound and upper_bound
    HDU 4825 Xor Sum
    1030: [JSOI2007]文本生成器
    1070: [SCOI2007]修车
    agc 027 B
    P2664 树上游戏
    CF 314 E. Sereja and Squares
    4237: 稻草人
  • 原文地址:https://www.cnblogs.com/GODYCA/p/2839398.html
Copyright © 2011-2022 走看看