Aligned esp-based stack frames – Intel ARCHITECTURE IA-32 User Manual

Page 540

Advertising
background image

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.

Advertising