How to return bool value in masm?
Posted on 2009-06-10 13:32:22 by GermainR27
xor eax, eax
retn xx

xor eax, eax
inc eax
retn xx


xx depends on the calling convention and the number of parameters.

Replace eax with rax in 64-bit.
Posted on 2009-06-10 18:20:19 by ti_mo_n
Assembly is different than the other higher level languages. In assembly you control all the registers (Technically, you could not save any register and return several values without pointing to a structure even.), in C or VB (ect..) this is automated for you through the eax register. In order to make things simple in the higher level languages, they consider everything to be a dword even boolean values.

ti_mo_n was correct, even though you are really returning 00000001 for TRUE and 00000000 for FALSE, it is still a 1 or 0 value. By clearing the rest of the bits, you are free to cmp or test or and EAX instead of just AL for your boolean value.
Posted on 2009-06-10 18:33:52 by jakor
Simply said, 0 is FALSE and any other value, in any part of any register can be treated as TRUE. Doesn't matter if it is AL, AH or high 16-bits of EAX or wherever else. And Timo's code is fine. 1 DOES define TRUE no matter where in the register that bit is set.
Posted on 2009-06-13 04:20:28 by XCHG
Well thanks to all  :)
Posted on 2009-06-13 21:42:23 by GermainR27
There are only two kinds of programmers... those who can count to 10, and those who can't.
Posted on 2009-06-14 04:44:40 by Homer
TRUE is simply anything that is not FALSE, however there are cases where it is more specific such as when dealing with returns for VARIANTs using IDispatch, in that case the value must be 0xFFFF, returning any other value will fail. Because of bad programming practices (of which I am equally guilty) a lot of programs test for TRUE (+1) and will fail without it when they should always test for FALSE instead. The reason to test for FALSE is that a test for TRUE extends the definition of FALSE to anything that is not 1, while a test for FALSE does not.
Posted on 2009-06-14 11:10:03 by donkey
Yeah, a painfully easy mistake to make, and one that will have you scratching your head and step-debugging seemingly sane code.
Posted on 2009-06-16 02:08:29 by Homer
With some languages it doesn't matter.
Eg, in C/C++ you can write if (x), which is equivalent to if (x == true), because by definition, true is anything that is not 0 (false). So in that language, it is solved in the == operator. I'm not sure what MASM does with the == in its .IF macro. If it follows C/C++ behaviour, you'd be safe... However if you manually use instructions like cmp etc, you'll have to be careful not to fall into the trap.

You'd still have to be careful when manipulating VARIANTs though, because those may be passed onto external code, which expects explicit values of VARIANT_TRUE or VARIANT_FALSE.
You can't just use 0 for false and any nonzero value for true.
Posted on 2009-06-16 08:17:15 by Scali
ti_mo_n,

      MOV EAX,ESP
      RET

The above will guarantee a "true" return.  Any value other than zero should be considered TRUE.  ESP is never zero.

Ratch
Posted on 2009-06-25 15:13:18 by Ratch
Ratch, that's extremely cute and obscure code.  ;) If you didn't explain I would have spent some time trying to figure it out.
Posted on 2009-06-26 10:42:47 by roticv
hehe, it's nice indeed.
Posted on 2009-06-26 13:18:08 by ChaperonNoir