13.07.2015 Views

Intel® 64 and IA-32 Architectures Optimization Reference Manual

Intel® 64 and IA-32 Architectures Optimization Reference Manual

Intel® 64 and IA-32 Architectures Optimization Reference Manual

SHOW MORE
SHOW LESS
  • No tags were found...

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

STACK ALIGNMENTD.1.1Aligned ESP-Based Stack FramesThis section discusses data <strong>and</strong> parameter alignment <strong>and</strong> the declspec(align)extended attribute, which can be used to request alignment in C <strong>and</strong> C++ code. Increating ESP-based stack frames, the compiler adds padding between the returnaddress <strong>and</strong> the register save area as shown in Example 4-9. This frame can be usedonly when debug information is not requested, there is no need for exceptionh<strong>and</strong>ling support, inlined assembly is not used, <strong>and</strong> there are no calls to alloca withinthe function.If the above conditions are not met, an aligned EDP-based frame must be used.When using this type of frame, the sum of the sizes of the return address, savedregisters, local variables, register spill slots, <strong>and</strong> parameter space must be a multipleof 16 bytes. This causes the base of the parameter space to be 16-byte aligned. Inaddition, any space reserved for passing parameters for stdcall functions also mustbe a multiple of 16 bytes. This means that the caller needs to clean up some of thestack space when the size of the parameters pushed for a call to a stdcall function isnot a multiple of 16. If the caller does not do this, the stack pointer is not restored toits pre-call value.In Example D-1, we have 12 bytes on the stack after the point of alignment from thecaller: the return pointer, EBX <strong>and</strong> EDX. Thus, we need to add four more to the stackpointer to achieve alignment. Assuming 16 bytes of stack space are needed for localvariables, the compiler adds 16 + 4 = 20 bytes to ESP, making ESP aligned to a 0mod 16 address.Example D-1. Aligned esp-Based Stack Framevoid _cdecl foo (int k){int j;foo:// See Note A belowpush ebxmov ebx, espsub esp, 0x00000008<strong>and</strong> esp, 0xfffffff0add esp, 0x00000008jmp commonfoo.aligned:push ebxmov ebx, espD-3

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!