什么意思对齐堆栈? [英] What does it mean to align the stack?

查看:197
本文介绍了什么意思对齐堆栈?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直是一个高层次的codeR,和架构是pretty新的给我,所以我决定在这里阅读关于大会教程:

I have been a high-level coder, and architectures are pretty new to me, so I decided to read the tutorial on Assembly here:

<一个href=\"http://en.wikibooks.org/wiki/X86_Assembly/Print_Version\">http://en.wikibooks.org/wiki/X86_Assembly/Print_Version

远了教程,如何将世界您好转换说明!程序

Far down the tutorial, instructions on how to convert the Hello World! program

#include <stdio.h>

int main(void) {
    printf("Hello, world!\n");
    return 0;
}

成等效组装code是给出生成以下内容:

into equivalent assembly code was given and the following was generated:

        .text
LC0:
        .ascii "Hello, world!\12\0"
.globl _main
_main:
        pushl   %ebp
        movl    %esp, %ebp
        subl    $8, %esp
        andl    $-16, %esp
        movl    $0, %eax
        movl    %eax, -4(%ebp)
        movl    -4(%ebp), %eax
        call    __alloca
        call    ___main
        movl    $LC0, (%esp)
        call    _printf
        movl    $0, %eax
        leave
        ret

有关线路之一,

andl    $-16, %esp

的解释是:

这code和与0xFFFFFFF0 s ^ ESP,
  与下一个对准堆栈
  最低16字节边界。一个
  称为Mingw的源$ C ​​$ C检查
  揭示了,这可能是对于SIMD
  出现在_main指示
  程序,该程序仅在对齐操作
  地址。由于我们的例程不
  包含SIMD指令,这一行
  是不必要的。

This code "and"s ESP with 0xFFFFFFF0, aligning the stack with the next lowest 16-byte boundary. An examination of Mingw's source code reveals that this may be for SIMD instructions appearing in the "_main" routine, which operate only on aligned addresses. Since our routine doesn't contain SIMD instructions, this line is unnecessary.

我不明白这一点。有人可以给我意味着什么对准下一个16字节边界堆栈,为什么需要一个解释?又是怎样的和L 实现这一目标?

I do not understand this point. Can someone give me an explanation of what it means to align the stack with the next 16-byte boundary and why it is required? And how is the andl achieving this?

推荐答案

假设栈看起来像这样在进入 _main (堆栈指针的地址仅仅是一个示例):

Assume the stack looks like this on entry to _main (the address of the stack pointer is just an example):

|    existing     |
|  stack content  |
+-----------------+  <--- 0xbfff1230

从推的%ebp ,再减去8 %ESP 来保留一些空间,局部变量:

Push %ebp, and subtract 8 from %esp to reserve some space for local variables:

|    existing     |
|  stack content  |
+-----------------+  <--- 0xbfff1230
|      %ebp       |
+-----------------+  <--- 0xbfff122c
:    reserved     :
:     space       :
+-----------------+  <--- 0xbfff1224

现在,在和L 指令归零%ESP 的低4位,其中的可以减小它;在这个特殊的例子,它具有保留额外的4个字节的效果:

Now, the andl instruction zeroes the low 4 bits of %esp, which may decrease it; in this particular example, it has the effect of reserving an additional 4 bytes:

|    existing     |
|  stack content  |
+-----------------+  <--- 0xbfff1230
|      %ebp       |
+-----------------+  <--- 0xbfff122c
:    reserved     :
:     space       :
+ - - - - - - - - +  <--- 0xbfff1224
:   extra space   :
+-----------------+  <--- 0xbfff1220

此的一点是,有一些SIMD(单指令多数据)指令(在x86的土地也被称为上交所为SIMD流指令扩展),它可以在多个单词执行并行操作存储器,但要求这些多个单词是开始是16字节的倍数的地址的块。

The point of this is that there are some "SIMD" (Single Instruction, Multiple Data) instructions (also known in x86-land as "SSE" for "Streaming SIMD Extensions") which can perform parallel operations on multiple words in memory, but require those multiple words to be a block starting at an address which is a multiple of 16 bytes.

在一般情况下,编译器不能假设从%ESP 特定的偏移将导致一个合适的地址(因为的ESP%状态在进入功能取决于调用code)。但是,通过以这种方式有意对准堆栈指针,编译器知道加入16字节的倍数的堆栈指针将导致一个16字节对齐地址时,这是与这些SIMD指令是安全的。

In general, the compiler can't assume that particular offsets from %esp will result in a suitable address (because the state of %esp on entry to the function depends on the calling code). But, by deliberately aligning the stack pointer in this way, the compiler knows that adding any multiple of 16 bytes to the stack pointer will result in a 16-byte aligned address, which is safe for use with these SIMD instructions.

这篇关于什么意思对齐堆栈?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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