News:

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

what's the use of return 0 ?

Started by supercoollee, May 10, 2010, 07:46:00 AM

Previous topic - Next topic

supercoollee

i know return 0 means making eax = 0 before return. i see a lot of moduls have return 0 at the end. what is it for ?
currently i don't have any of that in my program but it seems to be working without problem.

hutch--

The specification for many Windows messages when used is to return 0. You can do it manually with xor eax, eax, mov eax, 0 or use the macro that picks out if its returning zero or not.
Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php

supercoollee

sorry, what i meant is not "how to use return 0" . but "why should i use return 0"  :lol

can i cancel the "xor eax,eax" before ret ?  - i mean, in the modules written by myself.
i certainly will not modify the modules written by other people.

ecube

supercoollee the purpose is a easy way to signify if the function worked correctly, for example 1 = everythings fine, 0 = it failed. It can be any number just 1 and 0 are common.

supercoollee

which means, if i don't need to check whether the function fails, i don't need to rewrite eax , right?
that's good for me.

hutch--

No, actually read what has been said, some Windows mesages REQUIRE that 0 is returned in EAX. Some functions that you write can return anything you like in EAX including ZERO. Learn how a system works and you will understand WHY you can return 0.
Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php

jj2007

Most common use is in subclassing procedures; Windows wants to know, for example, if it should erase a button's background, or if you have done it already yourself.

WM_ERASEBKGND Message

QuoteThe WM_ERASEBKGND message is sent when the window background must be erased (for example, when a window is resized). The message is sent to prepare an invalidated portion of a window for painting.
...
Return Value

An application should return nonzero if it erases the background; otherwise, it should return zero.

dedndave

after learning how the API functions work, i tend to write routines much the same way
nearly all API functions return a value in EAX

for some, a zero return value indicates failure - a non-zero value is the result of the operation
one example of this might be a function that returns a handle in EAX - if the call fails, EAX will be zero, otherwise it is the handle

for some other API calls, a zero value indicates success
in these cases, a non-zero value may indicate the error code - or you may have to call GetLastError to get the error code

i try to emulate these behaviors when i write my own routines
notice, in ASM, you can also return values in ECX and EDX - i might use ECX for a string length and EDX for the pointer, for example
not sure how those registers are used in different compilers, though

Slugsnack

http://users.aber.ac.uk/auj/voidmain.shtml

the system uses the return value after your program exits. different return values signify different things. on windows, you generally return 0 for success and anything else for non-success.

another relevant link :
http://stackoverflow.com/questions/1188335/why-default-return-value-of-main-is-0-and-not-exit-success

supercoollee


jj2007

Quote from: Slugsnack on May 10, 2010, 02:19:20 PM
on windows, you generally return 0 for success and anything else for non-success.

Careful with that statement. For most API calls, it is actually the other way round.

Then, there are some silly outliers, such as the FileOpen/CreateFile/FindFirstFile family that uses HFILE_ERROR and INVALID_HANDLE_VALUE, both of which are -1. Which implies that a return value of 0 would be a valid handle ::)

GetKeyState is also a nice example: A key is pressed .if sword ptr ax<0, i.e. according to the documentation, eax might be positive but ax must be negative.

The best advice is: Read the documentation individually. Don't rely on your intuition.

GregL

For the procedures I write, I almost always return zero for success and non-zero for an error.

You should set eax to zero for success at the end of the program too, for ERRORLEVEL and it's just good practice to do so.

jj2007

Quote from: Greg Lyon on May 10, 2010, 04:23:29 PM
For the procedures I write, I almost always return zero for success and non-zero for an error.

It could be declared a matter of taste. However, in many cases you want to return something useful in eax, such as a handle or a pointer. That is easier done if you declare eax!=0 a success.

dedndave

just like the API functions - it is neither "set in stone" nor "a matter of taste"
it simply depends on the requirement of the function

GregL

jj,

Yes, I said "almost always". :lol

Returning zero for success is a C convention, but it is not "set in stone" either.