汇编语言学习笔记(八):深入理解函数调用堆栈

堆栈相关的寄存器

esp: 堆栈指针(stack pointer)
ebp: 基制指针(base pointer)

堆栈操作

  • push 栈顶地址减少4个字节(32位)
  • pop 栈顶地址增加4个字节
ebp在C语言中用作记录当前函数调用基址

在这里插入图片描述

其它关键寄存器

  • cs:eip:总是指向下一条的指令地址
    • 顺序执行: 总是指向地址连续的下一条指令
    • 跳转/分支:执行这样的指令的时候,cs:eip的值会根据程序需要被修改
    • call:将当前cs:eip的值压入栈顶,cs:eip指向被调用函数的入口地址
    • ret从栈顶弹出原来保存在这里的cs:eip的值,放入cs:eip
    • 发生中断时

函数调用堆栈的框架

在这里插入图片描述
call指令的执行过程:

  • eip中下一条指令的地址A保存在栈顶
  • 设置eip指向被调用程序代码开始处

调用过程

执行了call指令后,堆栈可能是这样的:
在这里插入图片描述
接着执行

pushl %ebp

ebp压入堆栈:
在这里插入图片描述
之后执行

movl %esp,%ebp

在这里插入图片描述
此时espebp都指向了同一个位置,相当于创建了一个新的空堆栈。

接着就是函数内部的操作了,可能有一些压栈出栈的操作:
在这里插入图片描述
退出函数:

movl %ebp,%esp

实际上把栈清空:
在这里插入图片描述
再执行一条指令

popl %ebp

此时栈又回到了原来的状态:
在这里插入图片描述
最后执行:

ret

在这里插入图片描述
这个函数调用的流程就结束了。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
中文名: MIPS汇编语言程序设计 原名: MIPS Assembly Language Programming 作者: Robert Britton 资源格式: PDF 出版社: Prentice Hall书号: 9780131420441发行时间: 2003年06月07日 地区: 美国 语言: 英文 简介: 这本书的用户将获得一个当代计算机体系结构的基本概念的理解,具有精简指令集计算机(RISC)的开始。一个计算机建筑的理解需要开始组织与现代计算机的基本知识。 MIPS架构体现了当代所有的RISC架构的基本设计原则。这本书提供了如何现代电脑的功能组件放在一起,以及如何在一台计算机的机器语言级作品的理解。 精心编写的,显然是有组织,这本书涵盖了MIPS架构的基础知识,包括算法开发,数字系统,函数调用,可重入函数,内存映射I / O,异常和中断,和浮点指令。 对于在系统,系统开发,系统的分析和系统维护领域的员工。 Users of this book will gain an understanding of the fundamental concepts of contemporary computer architecture, starting with a Reduced Instruction Set Computer (RISC). An understanding of computer architecture needs to begin with the basics of modern computer organization. The MIPS architecture embodies the fundamental design principles of all contemporary RISC architectures. This book provides an understanding of how the functional components of modern computers are put together and how a computer works at the machine-language level. Well-written and clearly organized, this book covers the basics of MIPS architecture, including algorithm development, number systems, function calls, reentrant functions, memory-mapped I/O, exceptions and interrupts, and floating-point instructions. For employees in the field of systems, systems development, systems analysis, and systems maintenance. 目录: CHAPTER 1: The MIPS Architecture CHAPTER 2: Pseudocode CHAPTER 3: Number Systems CHAPTER 4: PCSpim The MIPS Simulator CHAPTER 5: Algorithm Development CHAPTER 6: Function Calls Using the Stack CHAPTER 7: Reentrant Functions CHAPTER 8: Exception Processing CHAPTER 9: A Pipelined Implementation CHAPTER 10: Embedded Processors APPENDIX A: Quick Reference APPENDIX B: ASCII Codes APPENDIX C: Integer Instruction Set APPENDIX D: Macro Instructions APPENDIX E: A Trap Handler

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值