Returning long long value in i386 assembly?

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
clementttttttttt
Member
Member
Posts: 70
Joined: Tue Jul 14, 2020 4:01 am
Libera.chat IRC: clementttttttttt

Returning long long value in i386 assembly?

Post 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.
kzinti
Member
Member
Posts: 898
Joined: Mon Feb 02, 2015 7:11 pm

Re: Returning long long value in i386 assembly?

Post 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.
SKC
Member
Member
Posts: 28
Joined: Thu Feb 18, 2021 3:07 am

Re: Returning long long value in i386 assembly?

Post 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.
User avatar
iansjack
Member
Member
Posts: 4703
Joined: Sat Mar 31, 2012 3:07 am
Location: Chichester, UK

Re: Returning long long value in i386 assembly?

Post 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.
Octocontrabass
Member
Member
Posts: 5568
Joined: Mon Mar 25, 2013 7:01 pm

Re: Returning long long value in i386 assembly?

Post 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.
Post Reply