News:

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

Macro translation problem from C to MASM

Started by jdoe, August 25, 2007, 07:42:52 PM

Previous topic - Next topic

jdoe

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


drizz

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
The truth cannot be learned ... it can only be recognized.

jdoe

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


zooba

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

drizz

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.
The truth cannot be learned ... it can only be recognized.

jdoe

Many thanks to both of you guys.

Your explanations and examples are top-notch.

:U