System V ABI: Difference between revisions

The latest SYSV i386 ABI changed the stack alignment from 4-byte to 16-byte
[unchecked revision][unchecked revision]
No edit summary
(The latest SYSV i386 ABI changed the stack alignment from 4-byte to 16-byte)
Line 16:
=== i386 ===
 
This is a 32-bit platform. The stack grows downwards. Parameters to functions are passed on the stack in reverse order such that the first parameter is the last value pushed to the stack, which will then be the lowest value on the stack. Parameters passed on the stack may be modified by the called function. Functions are called using the <tt>call</tt> instruction that pushes the address of the next instruction to the stack and jumps to the operand. Functions return to the caller using the <tt>ret</tt> instruction that pops a value from the stack and jump to it. The stack is 416-byte aligned just before the call instruction is called.
 
Functions preserve the registers <tt>ebx</tt>, <tt>esi</tt>, <tt>edi</tt>, <tt>ebp</tt>, and <tt>esp</tt>; while <tt>eax</tt>, <tt>ecx</tt>, <tt>edx</tt> are scratch registers. The return value is stored in the <tt>eax</tt> register, or if it is a 64-bit value, then the higher 32-bits go in <tt>edx</tt>. Functions push <tt>ebp</tt> such that the <tt>caller-return-eip</tt> is 4 bytes above it, and set <tt>ebp</tt> to the address of the saved ebp. This allows iterating through the existing stack frames. This can be eliminated by specifying the <tt>-fomit-frame-pointer</tt> GCC option.
 
Additionally, if you compile with [[GCC]], it assumes the stack is 16-byte aligned (not just 4-byte aligned) when the <tt>call</tt> instruction in the caller was executed. This is required to properly store SSE values on the stack, as they require 16-byte alignment, or other values (such as 64-bit values) with higher-than-4-byte alignment. As a special exception, GCC assumes the stack is not properly aligned and realigns it when entering <tt>main</tt> or if the <tt>attribute ((force_align_arg_pointer))</tt> is set on the function.
 
=== x86-64 ===
Anonymous user