0x123C语言,and esp, 0xfffffff0

问题

I don't entirely understand the line with comment in it below. I read a few posts on SO and in the gcc manual and learned that it is for stack address alignment but fail to understand how it does so. The code is show below:

(gdb) disas main

Dump of assembler code for function main:

0x08048414 : push ebp

0x08048415 : mov ebp,esp

0x08048417 : and esp,0xfffffff0 ; why??

0x0804841a : sub esp,0x10

0x0804841d : mov DWORD PTR [esp],0x8048510

0x08048424 : call 0x8048320

0x08048429 : mov DWORD PTR [esp],0x8048520

0x08048430 : call 0x8048330

0x08048435 : leave

0x08048436 : ret

End of assembler dump.

The code was generated using gcc (version 4.6.3) on linux. Thanks.

回答1:

and esp, 0xfffffff0 does a bitwise AND between the stack pointer and a constant, and stores the result back in the stack pointer.

The constant is chosen so that its low four bits are zero. Therefore the AND operation will set these bits to zero in the result, and leave the other bits of esp intact. This has the effect of rounding the stack pointer down to the nearest multiple of 16.

回答2:

It looks like it's part of some code to set up shop at the start of main.

Function start: save the base frame pointer on the stack (needed by the leave instruction later):

0x08048414 : push ebp

Now we align the stack pointer to a 16-byte bound, because the compiler (for whatever reason) wants it. This could be that it always wants 16-byte aligned frames, or that the local variables need 16-byte alignment (maybe someone used a uint128_t or they're using a type that uses gcc vector extensions). Basically, since the result will always be less than or equal to the current stack pointer, and the stack grows downward, it's just discarding bytes until it gets to a 16-byte aligned point.

0x08048415 : mov ebp,esp

0x08048417 : and esp,0xfffffff0

Next we subtract 16 from the stack pointer, creating 16 bytes of local variable space:

0x0804841a : sub esp,0x10

puts((const char*)0x8048510);

0x0804841d : mov DWORD PTR [esp],0x8048510

0x08048424 : call 0x8048320

system((const char*)0x8048520);

0x08048429 : mov DWORD PTR [esp],0x8048520

0x08048430 : call 0x8048330

Exit the function (see another answer about what leave does):

0x08048435 : leave

0x08048436 : ret

Example of "discarding bytes": say esp = 0x123C at the start of main. The first lines of code:

0x08048414 : push ebp

0x08048415 : mov ebp,esp

result in this memory map:

0x123C: (start of stack frame of calling function)

0x1238: (old ebp value)

Then:

0x08048417 : and esp,0xfffffff0

forces the last 4 bits of esp to 0, which does this:

0x123C: (start of stack frame of calling function)

0x1238: (old ebp value)

0x1234: (undefined)

0x1230: (undefined)

There's no way for the programmer to rely on a certain amount of memory being between esp and ebp at this point; therefore this memory is discarded and not used.

Finally, the program allocates 16 bytes of stack (local) storage:

Next we subtract 16 from the stack pointer, creating 16 bytes of local variable space:

0x0804841a : sub esp,0x10

giving us this map:

0x123C: (start of stack frame of calling function)

0x1238: (old ebp value)

0x1234: (undefined)

0x1230: (undefined)

0x123C: (undefined local space)

0x1238: (undefined local space)

0x1234: (undefined local space)

0x1230: (undefined local space)

At this point, the program can be sure there are 16 bytes of 16-byte aligned memory being pointed to by esp.

回答3:

i know it was posted long time ago, it might help for others down the line.

1) In modern processors, we know that GCC aligns the stack defaulting to 16-byte alignment.

2) 16 byte ( 128 bit ) is because of SSE2 instructions which have MMX and XMM registers and XMM registers are of 128 bit.

3) so when a function call is made, it is automatically aligned to 16 byte, outside the function it remains to 8 byte.

4) the logic of using 0xfffffff0 is to keep the lower 4 bit to 0 , this is because of simple Boolean math which says that in binary , the multiples of 16 have low 4 bit to zero ( why four bits? 2^4 = 16 ).

来源:https://stackoverflow.com/questions/24588858/and-esp-0xfffffff0

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/368735.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

JUnit:在参数化测试中命名单个测试用例

几年前,我写了有关JUnit参数化测试的文章 。 我不喜欢它们的一件事是JUnit使用数字命名了单个测试用例,因此,如果它们失败,您将不知道是哪个测试参数导致了失败。 以下Eclipse屏幕快照将向您展示我的意思: 但是&#…

如何在 React Native 中写一个自定义模块

前言 在 React Native 项目中可以看到 node_modules 文件夹,这是存放 node 模块的地方,Node.js 的包管理器 npm 是全球最大的开源库生态系统。提到npm,一般指两层含义:一是 Node.js 开放式模块登记和管理系统,另一种是…

小程序canvasu真机上数据图片不能使用

canvas遇到的坑 1.文字换行 2.真机不能使用网络数据图片(真坑) 点击显示效果我就不写了,你们可以自己加一下 全部代码贴出来 css#preview {width: 100%;height: 100%;background: rgba(0, 0, 0, 0.6);position: fixed;z-index: 999;top: 0;ov…

c语言 两条线段位置,C++/STL实现判断平面内两条线段的位置关系代码示例

概念平面内两条线段位置关系的判定在很多领域都有着广泛的应用,比如游戏、CAD、图形处理等,而两线段交点的求解又是该算法中重要的一环。本文将尽可能用通俗的语言详细的描述一种主流且性能较高的判定算法。外积,又称叉积,是向量代…

Thunder团队Beta周贡献分规则

小组名称:Thunder 项目名称:i阅app 组长:王航 成员:李传康、翟宇豪、邹双黛、苗威、宋雨、胡佑蓉、杨梓瑞 分配规则 规则1:基础分,拿出总分的20%(8分)进行均分,剩下的80%…

SiftingAppender:将不同的线程记录到不同的日志文件中

Logback的一项新颖功能是SiftingAppender &#xff08; JavaDoc &#xff09;。 简而言之&#xff0c;它是一个代理附加器&#xff0c;它为给定运行时属性的每个唯一值创建一个子附加器。 通常&#xff0c;此属性来自MDC 。 这是基于上面链接的官方文档的示例&#xff1a; <…

gulp webpack整合

为什么需要前端工程化&#xff1f; 前端工程化的意义在于让前端这个行业由野蛮时代进化为正规军时代&#xff0c;近年来很多相关的工具和概念诞生。好奇心日报在进行前端工程化的过程中&#xff0c;主要的挑战在于解决如下问题&#xff1a;✦ 如何管理多个项目的前端代码&…

SpringBoot 入门第一章

一、前言 Spring Boot 是由 Pivotal 团队提供的全新框架&#xff0c;其设计目的是用来简化新 Spring 应用的初始搭建以及开发过程。该框架使用了特定的方式来进行配置&#xff0c;从而使开发人员不再需要定义样板化的配置。 本系列以快速入门为主&#xff0c;可当作工具小手册阅…

C语言游戏传递小秘密,C语言的那些小秘密之链表

大多数的读者在学习编程语言的时候都不喜欢那些枯燥的文字描述&#xff0c;包括我自己在开始学习编程的时候也是这样&#xff0c;对于代码的热情远远高于文字&#xff0c;所以我在我写东西的时候也不喜欢用枯燥的文字描述来向读者讲解&#xff0c;更喜欢用代码加上适当的文字描…

【转】 VC++6.0 在Win7 64位下调试,Shift+F5无法退出

Win7 64位VC6.0调试代码无法关闭窗口解决方法  VC6.0 在64位Windows7下调试的时候&#xff0c;再结束调试&#xff0c;程序无法退出&#xff0c;只能关闭VC6.0 IDE环境。  问题描述&#xff1a;当我击F5开始一个项目的调试时&#xff0c;程序在我设置的断点处停止&#xff…

使用Infinispan创建自己的Drools和jBPM持久性

我 在这里发表的原始文章&#xff1a; 您好&#xff0c;欢迎来到我打算向您展示如何创建自己的Drools和jBPM持久性实现的帖子。 我已经为流口水对象开发了基于infinispan的持久性方案&#xff0c;并且在此过程中学到了很多东西。 如果您想做某种事情&#xff0c;我打算给您一些…

Html5 填表 表单(二) input type 各种输入, 各种用户选择,上传等等泛输入用户交互

<input> 无限制输入 type 限制输入 type 如下类型 type 后还可以跟一些属性: 如<input typetext maxlength 10> 限制文本的长度为10字节 list 可以用的时候再来查, list就是当一个建议值不够的时候添加到几个. <form> <input typ…

c语言 输出音频 单片机,单片机播放WAV格式音频的理解

CSDN账号注册了3年&#xff0c;一直没有上来过&#xff0c;更不用说写博客了。我不知道博客的具体用途&#xff0c;我只想把它当做一种心得来发表&#xff0c;可能是一些技术上的理解或者生活上的小故事。好了&#xff0c;下面我将记录我对WAV播放器的理解。很久以前就看到过某…

UVALive3989 Ladies' Choice —— 稳定婚姻问题 Gale - Shapely算法

题目链接&#xff1a;https://vjudge.net/problem/UVALive-3989 题解&#xff1a; 题意&#xff1a;有n个男生和n个女生。每个女生对男神都有个好感度排行&#xff0c;同时每个男生对每个女生也有一个好感度排行。问&#xff1a;怎样配对&#xff0c;才能使的每个女生尽可能幸福…

通过命令行界面使用AWS ElasticMapReduce

在本文中&#xff0c;我将通过针对EMR的CLI使用AWS MapReduce服务&#xff08;称为ElasticMapReduce &#xff09;。 使用EMR的过程可以大致分为三个步骤&#xff1a; 设置并填充S3存储桶 创建并运行EMR作业 从S3存储桶中获取结果 在开始这三个高级步骤之前&#xff0c;还…

sublime 快捷键

Ctrl / 注释代码 <!-- <a href"http://www.baidu.com" target"_blank">百度</a> --> Tab 自动补全 <html tab 补全 html的全部基本标签 <a tab 自动补全为<a href…></a> 还有, 比如你忘记加<…

[UE4]关卡蓝图

转载于:https://www.cnblogs.com/timy/p/9053876.html

android 自定义 theme,Android使用Theme自定义Activity进入退出动画的方法

本文实例讲述了Android使用Theme自定义Activity进入退出动画的方法。分享给大家供大家参考&#xff0c;具体如下&#xff1a;有没有觉得Activity的默认动画太快了或者太难看了。。我原来使用Activity.overridePendingTransition来自定义Activity的进入动画&#xff0c;却发现没…

django 静态资源配置

最近在学习一个项目&#xff0c;django框架&#xff0c;但当 render 模板时&#xff0c;模板里有引入的图片就访问不到&#xff0c; 这是因为 django部署方式比较特别&#xff0c;采用静态文件路径:STATICFILES_DIRS的部署方式&#xff0c;之前你写的相对路径&#xff0c;绝对路…

MOXy的对象图– XML和JSON的输入/输出局部模型

假设您有一个要公开为RESTful服务的域模型。 问题是您只想输入/输出部分数据。 以前&#xff0c;您将创建一个代表子集的单独模型&#xff0c;然后使用代码在模型之间移动数据。 在EclipseLink 2.5.0中&#xff0c;我们有一个称为“对象图”的新功能&#xff0c;使您能够轻松地…