Intel Register Save Conventions (32-bit)

The contents of ebx, esi, and edi must be preserved across a subroutine call. If a subroutine uses one of these registers, it must save the original value and restore it before returning to the caller.

Registers eax, ecx, and edx are scratch registers. A subroutine can change the value in a scratch register without saving its previous value.

When the Intel floating-point coprocessor is used, the floating-point stack registers must be empty before entry and upon exit from a subroutine. However, if the subroutine returns a floating-point value, the value is returned on top of the stack in register fp0 and the remainder of the stack is empty.

A called procedure is responsible for establishing a new stack frame for itself and saving and restoring all preserved registers. The procedure establishes the stack frame with the following sequence:

pushl ebp
movl esp, ebp
subl #framesize, esp
pushl ebx         //only if used within function
pushl esi         //only if used within function
pushl edi         //only if used within function

Note that the code sequence may be abbreviated if -opt is specified.

A return must store any function result value, restore any preserved registers it used, pop its stack frame, and return to the caller.

A procedure that returns one of the data types that requires a return temporary must remove the extra pointer(s) from the stack entry; callers to such routines push these hidden arguments. The called routine must load register eax with the pointer to the return area that its caller passed before returning. Such a routine will start with the following code before establishing a frame:

popl edx
xchgl edx, 0 (esp)

Then it saves the return pointer in a local variable in its stack frame.