Page 1 of 1

Returning long long value in i386 assembly?

Posted: Tue Mar 09, 2021 11:17 pm
by clementttttttttt
Currently making a memory size detecting function in i386 assembly(C wont work because of compiler optimization), need to use long long values.

Re: Returning long long value in i386 assembly?

Posted: Tue Mar 09, 2021 11:31 pm
by kzinti
I believe the usual convention is to return the value in edx:eax.

edx holds the 32 most significant bits and eax the 32 least significant ones.

Re: Returning long long value in i386 assembly?

Posted: Tue Mar 09, 2021 11:39 pm
by SKC
kzinti wrote:I believe the convention is to return the value in edx:eax.

edx holds the 32 most significant bits and eax the 32 least significant ones.
Yes. As the wiki says: "The return value is stored in the eax register, or if it is a 64-bit value, then the higher 32-bits go in edx".

Btw, if you want to disable optimizations in GCC, you can use the 'optimize' attribute:

Code: Select all

__attribute__((optimize("-O0"))) void func();
You can read this to learn more about optimizations in GCC.

Re: Returning long long value in i386 assembly?

Posted: Wed Mar 10, 2021 3:00 am
by iansjack
clementttttttttt wrote:Currently making a memory size detecting function in i386 assembly.
Be very careful about reading/writing random memory addresses. This might have undesired side effects. UEFI and GRUB both provide memory maps; it may be better to use them.

Re: Returning long long value in i386 assembly?

Posted: Wed Mar 10, 2021 5:24 am
by Octocontrabass
clementttttttttt wrote:Currently making a memory size detecting function in i386 assembly
It's not possible to detect installed memory. You must use the memory map provided by the firmware.
clementttttttttt wrote:(C wont work because of compiler optimization)
If your code behaves differently depending on compiler optimization, the problem is your code, not the compiler optimization.
clementttttttttt wrote:need to use long long values.
This is documented in the i386 psABI.