热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

在函数调用期间递减ESP-decrementingESPduringafunctioncall

IdisassembledasmallprogramthataskstheuserfortheirnamethenoutputsHello+[users_name]

I disassembled a small program that asks the user for their name then outputs "Hello + [user's_name]"

我反汇编了一个小程序,询问用户他们的名字,然后输出“你好+ [用户名]”

This is the disassembled output:

这是反汇编的输出:

Main function:

Main function

Say hello function:

问你好功能:

Say hello function

I noticed that for the main() function, the ESP register is decremented by Ox10 and for the say_hello() function, the ESP register is decremented by Ox20. Why is this the case?

我注意到对于main()函数,ESP寄存器递减了Ox10,而对于say_hello()函数,ESP寄存器递减了Ox20。为什么会这样?

FYI: My processor is an 1.4 GHz Intel Core i5 and I'm running OSX

仅供参考:我的处理器是1.4 GHz Intel Core i5,我正在运行OSX

Original C code:

原始C代码:

void say_hello (void);

int main (){

    printf("Enter your name\n");
    say_hello();
    return 0;
}

void say_hello (void) { 

    char name[5]; 
    gets(name); //this is a unsafe function to use. Results in stack overflow
    printf("Hello %s\n", name); 

}

3 个解决方案

#1


It allocates space on the stack for local variables. First BP it set to the current value of SP, then SP is decremented to make room for the local variables used by the function. As you can see, later [ss:rbp+???] is used to access parts of memory of this reserved space.

它在堆栈上为局部变量分配空间。第一个BP它设置为SP的当前值,然后SP递减,以便为函数使用的局部变量腾出空间。如您所见,稍后[ss:rbp + ???]用于访问此保留空间的部分内存。

This is basically the same as PUSHing some dummy value a repeated number of times onto the stack.

这与在堆栈上重复推送一些虚拟值基本相同。

Before the function leaves, it is crucial that the exact amount is added back to SP, otherwise a wrong return address will be used by the RET instruction, and the program will most likely crash.

在函数离开之前,确切的数量被添加回SP是至关重要的,否则RET指令将使用错误的返回地址,并且程序很可能会崩溃。

#2


The stack is "implemented" by means of the stack pointer, which points into the stack segment. Every time something is pushed on the stack (by means of pushl, call, or a similar stack opcode), it is written to the address the stack pointer points to, and the stack pointer decremented (stack is growing downwards, i.e. smaller addresses). When you pop something off the stack (popl, ret), the stack pointer is incremented and the value read off the stack.

通过堆栈指针“实现”堆栈,堆栈指针指向堆栈段。每次在堆栈上推送某些东西(通过pushl,call或类似的堆栈操作码)时,它会被写入堆栈指针指向的地址,并且堆栈指针递减(堆栈向下增长,即较小的地址) 。当您从堆栈中弹出一些东西(popl,ret)时,堆栈指针会递增,并从堆栈中读取值。

For different function calls, we reserve space for local variables in the stack, so we decrement it and get the space. This is usually done using prologue and epilogue.

对于不同的函数调用,我们为堆栈中的局部变量保留空间,因此我们减少它并获得空间。这通常使用序言和结语来完成。

Prologue

A function prologue typically does the following actions if the architecture has a base pointer (also known as frame pointer) and a stack pointer (the following actions may not be applicable to those architectures that are missing a base pointer or stack pointer) :

如果体系结构具有基指针(也称为帧指针)和堆栈指针(以下操作可能不适用于缺少基本指针或堆栈指针的那些体系结构),则函数序言通常执行以下操作:

  • Pushes the old base pointer onto the stack, such that it can be restored later (by getting the new base pointer value which is set in the next step and is always pointed to this location).
  • 将旧的基指针推送到堆栈上,以便稍后可以恢复(通过获取在下一步中设置的新基本指针值并始终指向此位置)。

  • Assigns the value of stack pointer (which is pointed to the saved base pointer and the top of the old stack frame) into base pointer such that a new stack frame will be created on top of the old stack frame (i.e. the top of the old stack frame will become the base of the new stack frame).
  • 将堆栈指针的值(指向保存的基本指针和旧堆栈框架的顶部)分配到基本指针中,以便在旧堆栈框架的顶部创建新的堆栈框架(即旧堆栈框架的顶部)堆栈帧将成为新堆栈帧的基础)。

  • Moves the stack pointer further by decreasing or increasing its value, depending on whether the stack grows down or up. On x86, the stack pointer is decreased to make room for variables (i.e. the function's local variables).
  • 通过减少或增加其值来进一步移动堆栈指针,具体取决于堆栈是向下还是向上增长。在x86上,堆栈指针被减少以为变量腾出空间(即函数的局部变量)。

Epilogue

Function epilogue reverses the actions of the function prologue and returns control to the calling function. It typically does the following actions (this procedure may differ from one architecture to another):

函数结尾反转函数序言的动作并将控制返回给调用函数。它通常执行以下操作(此过程可能因架构而异):

  • Replaces the stack pointer with the current base (or frame) pointer, so the stack pointer is restored to its value before the prologue
  • 用当前基(或帧)指针替换堆栈指针,以便在序言之前将堆栈指针恢复为其值

  • Pops the base pointer off the stack, so it is restored to its value before the prologue
  • 将基指针弹出堆栈,因此在序言之前将其恢复为其值

  • Returns to the calling function, by popping the previous frame's program counter off the stack and jumping to it
  • 返回到调用函数,方法是将前一帧的程序计数器从堆栈中弹出并跳转到它

#3


As far as I rememeber, such decrements are mostly used to "reserve" place on stack or to guarantee even memory alignment.

据我所知,这种减少主要用于在堆栈上“保留”位置或保证甚至内存对齐。

What does it mean to align the stack?

对齐堆栈意味着什么?


推荐阅读
  • 扫描线三巨头 hdu1928hdu 1255  hdu 1542 [POJ 1151]
    学习链接:http:blog.csdn.netlwt36articledetails48908031学习扫描线主要学习的是一种扫描的思想,后期可以求解很 ... [详细]
  • 本文介绍如何使用Objective-C结合dispatch库进行并发编程,以提高素数计数任务的效率。通过对比纯C代码与引入并发机制后的代码,展示dispatch库的强大功能。 ... [详细]
  • 本文将介绍如何编写一些有趣的VBScript脚本,这些脚本可以在朋友之间进行无害的恶作剧。通过简单的代码示例,帮助您了解VBScript的基本语法和功能。 ... [详细]
  • 本文深入探讨了 Java 中的 Serializable 接口,解释了其实现机制、用途及注意事项,帮助开发者更好地理解和使用序列化功能。 ... [详细]
  • 在金融和会计领域,准确无误地填写票据和结算凭证至关重要。这些文件不仅是支付结算和现金收付的重要依据,还直接关系到交易的安全性和准确性。本文介绍了一种使用C语言实现小写金额转换为大写金额的方法,确保数据的标准化和规范化。 ... [详细]
  • UNP 第9章:主机名与地址转换
    本章探讨了用于在主机名和数值地址之间进行转换的函数,如gethostbyname和gethostbyaddr。此外,还介绍了getservbyname和getservbyport函数,用于在服务器名和端口号之间进行转换。 ... [详细]
  • 题目Link题目学习link1题目学习link2题目学习link3%%%受益匪浅!-----&# ... [详细]
  • Explore how Matterverse is redefining the metaverse experience, creating immersive and meaningful virtual environments that foster genuine connections and economic opportunities. ... [详细]
  • 主要用了2个类来实现的,话不多说,直接看运行结果,然后在奉上源代码1.Index.javaimportjava.awt.Color;im ... [详细]
  • 题目描述:给定n个半开区间[a, b),要求使用两个互不重叠的记录器,求最多可以记录多少个区间。解决方案采用贪心算法,通过排序和遍历实现最优解。 ... [详细]
  • 本文详细介绍了Java编程语言中的核心概念和常见面试问题,包括集合类、数据结构、线程处理、Java虚拟机(JVM)、HTTP协议以及Git操作等方面的内容。通过深入分析每个主题,帮助读者更好地理解Java的关键特性和最佳实践。 ... [详细]
  • 本文详细介绍了如何构建一个高效的UI管理系统,集中处理UI页面的打开、关闭、层级管理和页面跳转等问题。通过UIManager统一管理外部切换逻辑,实现功能逻辑分散化和代码复用,支持多人协作开发。 ... [详细]
  • Splay Tree 区间操作优化
    本文详细介绍了使用Splay Tree进行区间操作的实现方法,包括插入、删除、修改、翻转和求和等操作。通过这些操作,可以高效地处理动态序列问题,并且代码实现具有一定的挑战性,有助于编程能力的提升。 ... [详细]
  • 利用存储过程构建年度日历表的详细指南
    本文将介绍如何使用SQL存储过程创建一个完整的年度日历表。通过实例演示,帮助读者掌握存储过程的应用技巧,并提供详细的代码解析和执行步骤。 ... [详细]
  • 本文详细探讨了JDBC(Java数据库连接)的内部机制,重点分析其作为服务提供者接口(SPI)框架的应用。通过类图和代码示例,展示了JDBC如何注册驱动程序、建立数据库连接以及执行SQL查询的过程。 ... [详细]
author-avatar
kikokikolove
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有