views:

51

answers:

2
int main(void){

printf("Hello World");

return 0;

}

How is 0 passed as return value in assembly level? Is there a dedicated CPU register for this job?

UPDATE

Here's the 2 tables on passing/return data in the pdf, but doesn't seems to have the exact info on how the calling convention of an API is determined and which register is used for storing return address:

alt text

alt text

+2  A: 

That depends on the architecture. Just for a couple of examples, on x86 the EAX register is normally used for return values. On x86-64, it's RAX. On SPARC, it'll normally show up in %o0.

Jerry Coffin
So there's always a specified **dedicated** register for this,right?
Alan
What exactly do you mean by dedicated? No, EAX (for example) isn't reserved exclusively for that purpose. Yes, when you need to return a value, most compilers will expect it to be in EAX. In hand-written assembly, you're more likely to see other registers used more often (e.g., ESI or EDI for strings). Back in the 16-bit days, 16-bit returns were typically in AX, and 32-bit returns in DX:AX.
Jerry Coffin
If the register is not fixed, how does different APIs communicate?
Alan
@Alan: they can only communicate if they agree on an ABI. The fact that it's not universal is (part of) why you can't always code from different compilers, vendors, etc.
Jerry Coffin
Can you provide an example of `ABI` so that I can get to know how it is like on the earth?
Alan
@Alan: Agner Fog has put together substantial documentation on ABIs, name mangling schemes, etc., available at: http://www.agner.org/optimize/calling_conventions.pdf
Jerry Coffin
So basically all components of the same application should be compiled using the same compiler,is that so?
Alan
I looked at the 2 tables in the pdf for **Passing and returning objects** carefully,but don't find an example how compiler knows the ABI of a specific API exactly...
Alan
@Alan: *You* have to tell the compiler what ABI to target (to the extent you can, of course). There's no "meta-ABI" for it to find the ABI in a particular case. Using the same compiler throughout is one way, but not the only one -- e.g., on Windows, you can use COM as a compiler-neutral ABI.
Jerry Coffin
+2  A: 

That's part of the application binary interface.

These days the return value and function parameters usually are passed in registers, because that is the most efficient option. Only if that gets too big for the available registers the stack is used.

starblue
Can you provide an example of `ABI` so that I can get to know how it is like on the earth?
Alan