Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What registers must be preserved by an x86 function?

I'm writing a function in x86 assembly that should be callable from c code, and I'm wondering which registers i have to restore before i return to the caller.

Currently I'm only restoring esp and ebp, while the return value is in eax.

Are there any other registers I should be concerned about, or could I leave whatever pleases me in them?

like image 428
bobbaluba Avatar asked Mar 07 '12 14:03

bobbaluba


People also ask

Which are the preserved registers?

r12 , r13 , r14 , r15 , rbx , rsp , rbp are the callee-saved registers - they have a "Yes" in the "Preserved across function calls" column. What about flags? like DF ?

What are x86 registers?

The main tools to write programs in x86 assembly are the processor registers. The registers are like variables built in the processor. Using registers instead of memory to store values makes the process faster and cleaner.

How many registers does x86 have?

The x86 architecture has 8 General-Purpose Registers (GPR), 6 Segment Registers, 1 Flags Register and an Instruction Pointer. 64-bit x86 has additional registers.

Which register is responsible for keeping the return values of functions in x86 architecture?

Registers EAX, ECX, and EDX are designated for use within the function. Return values are stored in the EAX register.


2 Answers

Using Microsoft's 32 bit ABI (cdecl or stdcall or other calling conventions), EAX, EDX and ECX are scratch registers (call clobbered). The other general-purpose integer registers are call-preserved.

The condition codes in EFLAGS are call-clobbered. DF=0 is required on call/return so you can use rep movsb without a cld first. The x87 stack must be empty on call, or on return from a function that doesn't return an FP value. (FP return values go in st0, with the x87 stack empty other than that.) XMM6 and 7 are call-preserved, the rest are call-clobbered scratch registers.

Outside of Windows, most 32-bit calling conventions (including i386 System V on Linux) agree with this choice of EAX, EDX and ECX as call-clobbered, but all the xmm registers are call-clobbered.


For x64 under Windows, you only need to restore RBX, RBP, RDI, RSI, R12, R13, R14, and R15. XMM6..15 are call-preserved. (And you have to reserve 32 bytes of shadow space for use by the callee, whether or not there are any args that don't fit in registers.) xmm6..15 are call-preserved.
See https://en.wikipedia.org/wiki/X86_calling_conventions#Microsoft_x64_calling_convention for more details.

Other OSes use the x86-64 System V ABI (see figure 3.4), where the call-preserved integer registers are RBP, RBX, RSP, R12, R13, R14, and R15. All the XMM/YMM/ZMM registers are call-clobbered.

EFLAGS and the x87 stack are the same as in 32-bit conventions: DF=0, condition flags are clobbered, and x87 stack is empty. (x86-64 conventions return FP values in XMM0, so the x87 stack registers always need to be empty on call/return.)


For links to official calling convention docs, see https://stackoverflow.com/tags/x86/info

like image 81
Necrolis Avatar answered Dec 06 '22 13:12

Necrolis


32-bit: EBX, ESI, EDI, EBP
64-bit Windows: RBX, RSI, RDI, RBP, R12-R15, XMM6-XMM15
64-bit Linux,BSD,Mac: RBX, RBP, R12-R15

For details see "Software optimization resources" by Agner Fog. Calling conventions are described in this pdf.

like image 22
Evgeny Kluev Avatar answered Dec 06 '22 13:12

Evgeny Kluev