Stack Alignment
D
D-4
Example D-1 in the following sections illustrate this technique. Note the
entry points
foo
and
foo.aligned
, the latter is the alternate aligned
entry point.
Aligned
esp
-Based Stack Frames
This section discusses data and parameter alignment and the
declspec(align)
extended attribute, which can be used to request
alignment in C and C++ code. In creating
esp
-based stack frames, the
compiler adds padding between the return address and the register save
area as shown in Example 3-11. This frame can be used only when
debug information is not requested, there is no need for exception
handling support, inlined assembly is not used, and there are no calls to
alloca
within the function.
If the above conditions are not met, an aligned
ebp
-based frame must be
used. When using this type of frame, the sum of the sizes of the return
address, saved registers, local variables, register spill slots, and
parameter space must be a multiple of 16 bytes. This causes the base of
the parameter space to be 16-byte aligned. In addition, any space
reserved for passing parameters for
stdcall
functions also must be a
multiple of 16 bytes. This means that the caller needs to clean up some
of the stack space when the size of the parameters pushed for a call to a
stdcall
function is not a multiple of 16. If the caller does not do this,
the stack pointer is not restored to its pre-call value.
In Example D-1, we have 12 bytes on the stack after the point of
alignment from the caller: the return pointer,
ebx
and
edx
. Thus, we
need to add four more to the stack pointer to achieve alignment.
Assuming 16 bytes of stack space are needed for local variables, the
compiler adds 16 + 4 = 20 bytes to
esp
, making
esp
aligned to a 0 mod
16 address.
Содержание ARCHITECTURE IA-32
Страница 1: ...IA 32 Intel Architecture Optimization Reference Manual Order Number 248966 013US April 2006...
Страница 220: ...IA 32 Intel Architecture Optimization 3 40...
Страница 434: ...IA 32 Intel Architecture Optimization 9 20...
Страница 514: ...IA 32 Intel Architecture Optimization B 60...
Страница 536: ...IA 32 Intel Architecture Optimization C 22...
Страница 560: ...IA 32 Intel Architecture Optimization E 14...