I guess I just don't understand signed numbers in asm like I thought I did. Why doesn't this if block work?




dwSomeNumber DWORD ?

mov dwSomeNumber, -10
.IF dwSomeNumber<0
invoke MessageBox, ...
.ENDIF



In my code, the MessageBox never gets executed.
Posted on 2003-12-12 08:12:55 by rdaneel
Try this, a signed DWORD is an SDWORD

dwSomeNumber    SDWORD    ?


mov dwSomeNumber, -10
.IF dwSomeNumber<0
invoke MessageBox, ...
.ENDIF


You can also cast it from a regular DWORD like this

dwSomeNumber    DWORD    ?


mov dwSomeNumber, -10
.IF SDWORD PTR dwSomeNumber<0
invoke MessageBox, ...
.ENDIF
Posted on 2003-12-12 08:49:34 by donkey
So this would make the SDWORD range -2147483648 to 2147483648 right?
Posted on 2003-12-12 08:58:18 by rdaneel
Hi RDaneel,

Something like that:

-(2^31) to + (2^31)-1

-2147483648 to 2147483647
Posted on 2003-12-12 09:15:22 by donkey
If you're using MASM32, the use of the "<" and ">" in an IF statement unfortunately only gets replaced by a jc or a jnc instruction. That negates comparing negative numbers which would need the "jl" or "jg" instructions.

(I don't know how other assemblers treat such IF statements.)

Raymond
Posted on 2003-12-12 21:42:57 by Raymond
rdaneel,

The default interpretation for .IF statements in MASM for memory contents is unsigned. Unsigned numbers can never be interpreted by the CPU as negative, only positive. That means the assembler will use conditional jumps like JA, JAE, JB, JBE, etc. If you want the number to be interpreted as signed, then it needs to use instructions like JG, JGE, JL, JLE, etc. Donkey showed you how to direct MASM to interpret the number as signed. Unsigned numbers have a positive range twice as large as signed numbers because they have one more bit to use for their magnitude. Ratch
Posted on 2003-12-15 20:25:54 by Ratch
Is it the high-order bit that is used as the sign bit?
Posted on 2003-12-16 07:57:01 by rdaneel
For intel, it's the 31th bit.
Posted on 2003-12-16 08:26:01 by roticv
rdaneel ,
Is it the high-order bit that is used as the sign bit?

Yes, that is true IF the number is interpreted as a signed number. If it is interpreted as a unsigned number, then all the bits of the word are used to represent a positive or zero number. There is no negative representation for unsigned numbers. If is important that you know for sure what the method of comparision between two numbers is going to be. Otherwise your results are not going to happen according to your expectations. Ratch
Posted on 2003-12-16 16:07:01 by Ratch