简体   繁体   中英

x86_64 assembly conventions saving argument registers

I'm writing some x86_64 assembly to call a C function. My C function takes in 1 argument, so the assembly places the argument in %rdi . The ABI pdf (linked below) says that the other 6 argument registers (rsi, rdx, rcx, r8, r9) are not preserved across function calls. However, since my C function only takes one long argument, do I have any guarantees about whether or not the C function will clobber the other 5 registers? My assumption was that the argument registers are only clobbered if an argument's value is changed:

void foo(int a, int b) {
    a++; /* %rdi will be changed, but %rsi won't be changed when control returns. */
}

I'm asking because I'd like to preserve the values of the other 5 argument registers across my C function call (without having to explicitly push/pop them from the stack manually).

x86_64 ABI - http://www.x86-64.org/documentation/abi-0.99.pdf

There is no guarantee. You will have to save them on the stack to ensure they are not changed. Whether they are changed will depend on the compiler.

If you want to somehow ensure they are not changed, you could write the function in assembly.

Look at the table on page 21. It has a column "Preserved Yes/No" for all the registers. And it says "No" for all the registers used to pass parameters.

Whether you pass arguments or not, the argument registers are not required to be preserved. You will likely not get your parameters back.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM