Search code examples
assemblyx86calling-convention

Return Value %eax Convention


Naturally in i386, the %eax holds the return value of functions, yes? However apparently this is only convention. Is it possible to change this convention, and make the return value be %ebx or something? And if you were to do this, would this also make %ebx return values to the system?


Solution

  • It is convention only, yes. Since the ret instruction doesn't change any registers except esp, you can use any other to return values. However both the caller and the callee have to agree on where the return value should be, so if you want to interoperate with other code, the calling conventions used must be compatible. Between your own functions, you can use whatever you want. Compilers also invent ad-hoc conventions for private functions for efficiency reasons.