News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

eax

Started by G`HOST, November 21, 2005, 06:04:08 AM

Previous topic - Next topic

G`HOST

Hi all
       After the call to an API function the returned value is stored in eax.So what happens to it if we call an another function after the first call.Something like this:
1.invoke SetTimer,....
2.invoke LoadBitmap,........

What happens to the returned value after 1.call, is it lost?How processor deal with it ?And do we have to take care of it if we dont want to use it.
An another question,Since i dont know any other programming language other then assembly.I was wondering how other languages like c++ deals with the returned value? do they use registers too?

sluggy

Quote from: G`HOST on November 21, 2005, 06:04:08 AMAfter the call to an API function the returned value is stored in eax.So what happens to it if we call an another function after the first call.Something like this:
1.invoke SetTimer,....
2.invoke LoadBitmap,........

What happens to the returned value after 1.call, is it lost?
Yes. eax is never the same after calling an API function, it can be trashed at any time.

QuoteAnd do we have to take care of it if we dont want to use it.
You mean the value that is returned? You only have to take care of it if it is a resource handle, in which case you must "free" it, otherwise Windows doesn't know you have finished with it, and you end up with a memory leak (lots of resources & memory allocated but not being used by anything).

QuoteI was wondering how other languages like c++ deals with the returned value? do they use registers too?
Yes, but in a language like C++ you are not dealing with the registers directly, instead you assign the returned value to a variable, you can then use this at any time within the scope of the class or function it resides in.

These are just brief answers, i hope they help  :U

MichaelW

If you need the return value, or you will need it later, you must process the value, or save it, before you do anything that might change the value. Anything, here, includes any instruction that directly or indirectly changes the value in EAX, and, because the convention under Windows is to not preserve EAX, ECX or  EDX, any call to an API function (or any call to a MASM32 library procedure).

AFAIK all API functions that return a value return a 32-bit value in EAX. There are some documented functions, Uint32x32to64 for example, that return a 64-bit value, probably in EDX:EAX, but the function is actually implemented as a macro in winnt.h, so I had no way of testing it.

To add to sluggy's answer regarding not dealing with registers directly, if you call a CRT function that returns a value, _atoi64 for example, from ASM code then you must deal with registers directly, in this case receiving the 64-bit return value in EDX:EAX.

You can find some information on return values for Microsoft C and C++ here

Agner Fog has a 39-page PDF document: "Calling conventions for different C++ compilers and operating systems", available here:

http://www.agner.org/assem/calling_conventions.pdf


eschew obfuscation

Retsim_X

hey góst.

invoke someapi,params
push eax
invoke someotherapi,params
pop ebx

now ebx has the original return value
and eax has the final value

also is it not true guys that you can return in any standard register
eax ebx ecx edx esi edi ???
i do it often in complicated returns from INTERNAL functions??

yours Resim_X

raymond

Quotealso is it not true guys that you can return in any standard register
eax ebx ecx edx esi edi ???
i do it often in complicated returns from INTERNAL functions??

Yes, in assembly you can do ALMOST anything you want within your own program. I myself often use all the registers (even EBP) without preserving any of them inside small programs to compute something specific.

However, because the Windows APIs were designed to be used under a variety of conditions by myriads of people, they had to be written under a given set of rules, such as preserving some registers and returning values in specific ones.

Raymond
When you assume something, you risk being wrong half the time
http://www.ray.masmcode.com

hutch--

Its usually the case that once you have the system defined registers preserved you can do anything you like with them after that but you cannot safely interact with the OS until after you have restored them. Its common to use EBP when you write a proc without a stack frame and if you are desperate, you can also use ESP but it ceases to be general purpose code when you do things like this.

The triuck is to learn the OS defined register preservations first then once you have that done properly you can write whatever you like.
Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php