The MASM Forum Archive 2004 to 2012

General Forums => The Campus => Topic started by: jdoe on August 25, 2007, 07:42:52 PM

Title: Macro translation problem from C to MASM
Post by: jdoe on August 25, 2007, 07:42:52 PM
Hi,

I can't figure how to translate this macro to MASM. I don't know what to do with " (x) <= 0 ? ".
Maybe I'm stuck because there is no way to do it, I don't know.

From WinError.h (Windows SDK)


#define HRESULT_FROM_WIN32(x) ((HRESULT)(x) <= 0 ? ((HRESULT)(x)) : ((HRESULT) (((x) & 0x0000FFFF) | (FACILITY_WIN32 << 16) | 0x80000000)))




This is what I guess...


HRESULT_FROM_WIN32 MACRO x
    if x le 0
        exitm <x>
    else
        exitm <((x and 0000FFFFh) or (FACILITY_WIN32 shl 16) or 80000000h))>
    endif
ENDM




If you have an idea, I'll be thankful for an hints.

Thanks

Title: Re: Macro translation problem from C to MASM
Post by: drizz on August 26, 2007, 02:20:26 AM
this is how i would write it:
HRESULT_FROM_WIN32 MACRO x:req
    if ((x) eq 0) or ((x) shr 31)
        exitm <(x)>
    else
        exitm <(((x) and 0000FFFFh) or (FACILITY_WIN32 shl 16) or 80000000h)>
    endif
ENDM
Title: Re: Macro translation problem from C to MASM
Post by: jdoe on August 26, 2007, 09:10:44 AM
Thanks drizz.

But two more questions...

What is different between :  if ((x) eq 0) or ((x) shr 31)   and   if x le 0

What is different between :  (x)   and   x

Title: Re: Macro translation problem from C to MASM
Post by: zooba on August 26, 2007, 10:20:09 AM
Quote from: jdoe on August 26, 2007, 09:10:44 AM
What is different between :  if ((x) eq 0) or ((x) shr 31)   and   if x le 0

Testing for equal to zero or testing if the sign bit is set ((x) shr 31 is the same as (x) and 80000000h) will guarantee that the comparison is signed. I can't remember whether compile-time conditionals are signed comparisons by default or not. If they are, then your code will also work fine.

Quote from: jdoe on August 26, 2007, 09:10:44 AM
What is different between :  (x)   and   x

MASM replaces macro parameters within a macro literally, the same as C. Putting brackets around the parameter x will prevent the same problems.

Cheers,

Zooba :U
Title: Re: Macro translation problem from C to MASM
Post by: drizz on August 26, 2007, 11:27:19 AM
zooba nicely explained, i'll just add a few words.
Quote from: zooba on August 26, 2007, 10:20:09 AM
Quote from: jdoe on August 26, 2007, 09:10:44 AM
What is different between :  if ((x) eq 0) or ((x) shr 31)   and   if x le 0
"x le 0" will only work if the constants passed have a minus sign otherwise they are treated as unsigned even if you cast "sdword ptr (x)". i don't know why or if i am mistaken, but this is stupid since other constructs will generate right code if type cast signed-unsigned is used.
.if eax > sdword ptr 0ffffffffh
.endif
.if eax > dword ptr -7
.endif

Quote from: zooba on August 26, 2007, 10:20:09 AM
Quote from: jdoe on August 26, 2007, 09:10:44 AM
What is different between :  (x)   and   x
example:
-----------
somemacro macro x
exitm %(x shl 5)
endm
take somemacro (1+2) the "1+2" expression will directly be passed to "1+2 shl 5" so by order of precedence shl will be calculated before 1+2 hence the result will be wrong. believe me you don't won't to discover this the hard way.
Title: Re: Macro translation problem from C to MASM
Post by: jdoe on August 26, 2007, 07:36:55 PM
Many thanks to both of you guys.

Your explanations and examples are top-notch.

:U