为什么这个函数序言使用数种指令来计算ESP减少? [英] Why does this function prologue use several instructions to calculate the esp reduction?

查看:175
本文介绍了为什么这个函数序言使用数种指令来计算ESP减少?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经看过汇编code几转储和有这部分(发现这里和<一个href=\"http://stackoverflow.com/questions/21080335/stack-contents-during-a-function-call/21096254#21096254\">here)在主要功能:

I have looked at a few dumps of assembler code and there is this section (found here and here) in the main function:

<main+0>:    push   %ebp
<main+1>:    mov    %esp, %ebp
<main+3>:    sub    $0x8, %esp
<main+6>:    and    $0xfffffff0, %esp
<main+9>:    mov    $0x0, %eax
<main+14>:   add    $0xf, %eax
<main+17>:   add    $0xf, %eax
<main+20>:   shr    $0x4, %eax
<main+23>:   shl    $0x4, %eax
<main+26>:   sub    %eax, %esp

你能解释一下我是什么(主+ 9)(主+ 26)用的呢?
这是为什么这样做低效?

Can you explain me what (main+9) to (main+26) is used for? Why is this done so 'inefficient'?

推荐答案

所以,你要一个完整的步行通过没有做自己任何研究吗?听起来合法的。

So you want a full walk-through without doing any research yourself? Sounds legit.

主+ 9:MOV $为0x0,%eax中

main+9: mov $0x0, %eax

加载寄存器 EAX 六角0 (= 0十二月)。

Loads the register eax with hex 0 (=dec 0).

主+ 14:加$ 0xF的,EAX%

main+14: add $0xf, %eax

六角˚F(= 12月15日)到零 EAX

Adds hex F (= dec 15) to the zero in eax.

主+ 17:加$ 0xF的,EAX%

main+17: add $0xf, %eax

六角˚F(= 12月15日),以 EAX 一次。这三个指令的可能的也得到了

Adds hex F (= dec 15) to eax again. These three instructions could have also been done by

movl $0x1e, %eax

但谁的票的指示...无论如何,在这一点上 EAX 包含的十六进制1E 这是12月30日。

but who's counting instructions... Anyway, at this point eax contains hex 1E which is dec 30.

主要+ 20:SHR $为0x4,%eax中

main+20: shr $0x4, %eax

由四位档 EAX 到右侧的内容。

Shifts the contents of eax to the right by four bits.

主要+ 23:SHL $为0x4,%eax中

main+23: shl $0x4, %eax

EAX 右后卫。为什么?因为这将清除最低4位。现在EAX中包含的六角10 (= 12月16日)

Shifts eax right back. Why? Because this clears the lowest four bits. Now eax contains hex 10 (= dec 16)

主+ 26:子%EAX,ESP%

main+26: sub %eax, %esp

基体上 EAX 尤其(堆栈指针)。由于

Substracts eax from esp (the stack pointer). Since

主+ 6:$和0xfffffff0,%ESP

main+6: and $0xfffffff0, %esp

清除走低尤其 $ P $四位pviously,新的尤其将是十六字节对齐,根据ABI。为什么就不能在 ESP使用主+ 6 ?因为在x86,堆栈增长的向下的从内存顶部。简单地屏蔽掉尤其的低位风险重挫局部变量。因此,减法成长堆栈的的以十六字节边界。

cleared the lower four bits in esp previously, the new esp will be sixteen byte aligned, as per ABI. Why not simply use esp after main+6? Because on x86, the stack grows downwards from the top of memory. Simply masking off the lower bits of esp risks clobbering local variables. Hence the subtraction to grow the stack down to the sixteen byte boundary.

这篇关于为什么这个函数序言使用数种指令来计算ESP减少?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆