C 与汇编程序的约定调用

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                       #
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 原文地址:C语言函数调用栈(一)C语言函数调用栈(二) 0 引言 程序的执行过程可看作连续的函数调用。当一个函数执...
    小猪啊呜阅读 10,213评论 1 19
  • 首先寄存器使用惯例:eip :指令地址寄存器,保存程序计数器的值,当前执行的指令的下一条指令的地址值,16位中为i...
    扎Zn了老Fe阅读 6,099评论 0 0
  • 一、温故而知新 1. 内存不够怎么办 内存简单分配策略的问题地址空间不隔离内存使用效率低程序运行的地址不确定 关于...
    SeanCST阅读 12,379评论 0 27
  • 站在巨人的肩膀上——IDA PRO权威指南阅读笔记 一,窗口 view->open subviews 打开/关闭各...
    SueLyon阅读 14,911评论 0 6
  • 一鹊桥之上牛郎在一头 织女另一头相望于桥相见此刻 二七夕至上情人为最或是烛光晚餐或是路边食摊也或是戈壁野饭有你陪伴...
    岂余心之可惩阅读 962评论 0 1