1.说明
函数调用时通过栈帧来实现的,栈帧是指为一个单独的函数调用单独分配的那部分栈空间。当运行中的程序调用另一个函数时,就要申请一个新的栈帧,原来函数的栈帧被保存(函数没有执行完,只是调用了另一个函数)。
因为每次函数调用都基于相同的约定,所以每个函数的栈帧的结构都是相同的,但栈帧会随着不同的函数的生命周期而发展,所以需要用到寄存器在每个函数的栈帧中定位。%ebp是基址指针(stack pointer),它总是指向当前帧的底部;%esp是栈指针(base pointer),它总是指向当前帧的顶部。
当被调用的函数运行结束后当前帧会全部收缩(通过移动%esp的指向来实现),同时还原%ebp,回到调用者的帧。
a.执行函数前(已经调用)(prologue):
将参数逆序压入堆栈(这样在使用%ebp+偏移量来访问参数时,可以不考虑参数的数量,直接用%ebp+8, %ebp+12...等访问第1个至第N个参数)
汇编程序:
pushl $3                  # push the n  parameter
pushl $2                  # push the secondary parameter
pushl $1                  # push the first parameter
call fun                  # 
栈的变化:
参数n
.
.
.
参数(3):
参数(2):  
参数(1):  
返回地址:                  <----esp 
保存原来的%ebp至栈中
汇编程序:
pushl %ebp
栈的变化:
.
参数3:
参数2:  
参数1:
返回地址:
%ebp(原)                    <----esp
将现在的%ebp(%ebp应该始终指向当前栈的底部)赋值为%esp
汇编程序:
movl %esp,%ebp
栈的变化:
参数3:
参数2:
参数1:      
返回地址:                                            
%ebp(原)                    <----esp  <----ebp
创建空间(下移%esp指针)用于保存局部变量
汇编程序:
subl $8,%esp
栈的变化:
参数3:
参数2:     
参数1:           
返回地址:            
%ebp(原)                    <----ebp        
|  
|                           <----esp
b.执行函数(body):
c.返回(epilogue):
保存返回值至%eax
汇编程序:
movl -n(%ebp), %eax
丢弃创建的空间(将%esp指针移至当前%ebp)
汇编程序:
movl %ebp, %esp
栈的变化:
参数3:
参数2:  
参数1:   
返回地址:  
%ebp(原)                     <----ebp    <----esp   
| 
|
弹出%ebp的旧值装入现在%ebp中(恢复了调用者的栈帧,%ebp指向调用者的栈帧)
汇编程序:
popl %ebp
栈的变化:
参数3:
参数2:  
参数1:
返回地址:                <----esp
| 
| 
|
ret指令通过把返回地址从堆栈中弹出到程序计数器,从而从该函数返回
汇编程序:
ret
2.实例
一个计算2^3+5^2的程序,定义了函数power
# in order to: show the function that can get the ans of x**n how work
#              and get the result of 2**3+5**2
#
#
# variable: all variable in register, so .data is null
#           
    
    .section .data
    .section .text
    .globl _start
_start:
    pushl $3                  # push the secondary parameter
    pushl $2                  # push the first parameter
    call power                # 
    addl $8, %esp             # stack pointer move afterwards
    pushl %eax                # esp+8 save the first result from eax
    pushl $2                  # push the secondary parameter
    pushl $5                  # push the first parameter
    call power
    addl $8, %esp             # stack pointer move afterwards
    popl %ebx                 # pop the first result to ebx
                              # and the secondary result has in eax
    addl %eax, %ebx           # ebx = eax + ebx
    movl $1, %eax             # '1' is the linux system call exit() code
                              # ebx save the number to back 
    int $0x80          
#
# in order to: function to get the result of x**n
#
# input: first parameter - n
#        secondary parameter - x
#
# outut: getback variable in %eax
#
# variable: %ebx - x
#           %ecx - 3
#
#           -4(%ebp) -  save current number
#           %eax - save temp number
    .type power, @function
power:
    pushl %ebp                # save the old base pointer
    movl %esp, %ebp           # set base pointer to stack pointer 
    subl $4, %esp             # save space for locat save 
                              #       
                              # n              <--12(%ebp) 
                              # x              <--8(%ebp)
                              # backadress     
                              # oldebp         <--(%ebp)
    movl 8(%ebp), %ebx        # x              <--(%esp) -4(%ebp)
    movl 12(%ebp), %ecx       # 
    movl %ebx, -4(%ebp)       # save current result
power_loop_start:
    cmpl $1, %ecx             # 
    je end_power              #
    movl -4(%ebp), %eax       #
    imull %ebx, %eax          #
    movl %eax, -4(%ebp)       #
    decl %ecx                 # ecx-=1
    jmp power_loop_start      #
end_power:
    movl -4(%ebp), %eax       # backnumber in %eax 
    mov %ebp, %esp            #
    popl %ebp                 # 
    ret                       #