Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can eax store return value of which size is bigger than 4 bytes?

EAX is used to store return value of function in 32bit platform, I just wonder if the size of return value of a function is bigger than 4 bytes, how does eax handle it? In this case OS can save the return value on the stack and store the address of the stack in EAX, but then how can OS tell whether the value stored in EAX is an address to the return value or is actually the return value itself?

like image 589
wangshuaijie Avatar asked Mar 20 '12 10:03

wangshuaijie


People also ask

How many bytes are in EAX?

The idiv instruction divides the contents of the 64 bit integer EDX:EAX (constructed by viewing EDX as the most significant four bytes and EAX as the least significant four bytes) by the specified operand value.

Does EAX return value?

EAX: The accumulator. This register typically stores return values from functions. EBX: This register is typically the pointer to the base of an array.

What is the value of EAX register?

eax is the 32-bit, "int" size register. It was added in 1985 during the transition to 32-bit processors with the 80386 CPU. I'm in the habit of using this register size, since they also work in 32 bit mode, although I'm trying to use the longer rax registers for everything. ax is the 16-bit, "short" size register.

Is EAX the return register?

eax (or rax) is the return value register. edi (or rdi) is the first function argument. esi (or rsi) is the second function argument.


1 Answers

The caller and callee have to agree on what the registers and stack contain. This is called the calling convention, which is part of a larger concept called the application binary interface (ABI). The callee defines how it wants to be called (ie. whether arguments need to be on the stack, in registers, etc.) and the compiler ensures that the code it generates complies with the calling convention.

As for your specific question, it depends the ABI. Sometimes if the return value is larger than 4 bytes but not larger than 8 bytes, it can be split into EAX and EDX. But most of the time the calling function will just allocate some memory (usually on the stack) and pass a pointer to this area to the called function.

Note also that the role of the OS is not as important as you appear to think. Binaries with different calling conventions may coexist on the same system, and binaries can even use different calling conventions internally. The ABI of the OS is only important when the binary calls its system libraries.

like image 148
sam hocevar Avatar answered Oct 26 '22 09:10

sam hocevar