The MASM Forum Archive 2004 to 2012

General Forums => The Campus => Topic started by: Jimg on April 05, 2005, 05:00:22 PM

Title: numeric .if
Post by: Jimg on April 05, 2005, 05:00:22 PM
It took me a while to find this problem.....
I have the following misunderstanding of how .if works-

mov eax,-8
mov edx,12
.if eax < edx
PrintText "okokok"
.else
PrintText "error"
.endif

This prints error, i.e. -8 is not less than 12   ????

looking at the listing it does a
cmp eax, edx
jae    @C001C

if it had done a   jge  insted of a  jae  everything would have been ok.

Is there some way to do a numeric comparison .if ??  I know I can just do it without .if but I like the structure (sometimes).
Title: Re: numeric .if
Post by: Tedd on April 05, 2005, 05:09:33 PM
I'm guessing here, but I'd expect it to be along the lines of ASSUME-ing the registers are signed. Which would then cause a signed comparison, rather than the usual unsigned one.
I don't have the docs to hand, so I can't check.
Title: Re: numeric .if
Post by: MichaelW on April 05, 2005, 05:17:04 PM
You need to add an "sdword ptr" to either side of the operation so the operation will be considered as signed.

.IF SDWORD PTR eax < edx


Title: Re: numeric .if
Post by: QvasiModo on April 05, 2005, 07:00:29 PM
By default .if will compare unsigned values. And the signed value -8 has the same binary representation than FFFFFFFFFFFFFFF8h, definitely larger than 12 ;)

Two solutions:

assume eax:SDWORD
assume edx:SDWORD
.if eax < edx
assume eax:DWORD
assume edx:DWORD


Or:

.if (SDWORD ptr eax) < (SDWORD ptr edx)


The use of the "ptr" keyword can be rather confusing, you're not really using pointers in the above code since there are no square brackets. Shame on the MS engineers that designed the syntax for MASM. :naughty:
Title: Re: numeric .if
Post by: thomasantony on April 06, 2005, 11:19:07 AM
Hi,
   How does the code actually look like in signed comparison. Doesn't it use the normal cmp . Or is it something else?

Thomas
Title: Re: numeric .if
Post by: roticv on April 06, 2005, 12:04:20 PM
Quote from: thomasantony on April 06, 2005, 11:19:07 AM
Hi,
   How does the code actually look like in signed comparison. Doesn't it use the normal cmp . Or is it something else?

Thomas

the conditional jump used is for signed, eg jge instead of jnc
Title: Re: numeric .if
Post by: farrier on April 06, 2005, 01:45:10 PM
When comparing Unsigned values, use:

jb, jbe, ja, jae        or equivalents

these test the Carry flag

When comparing Signed values, use:


jl, jle, jg, jge         or equivalents

these test the Sign and Overflow flags

farrier
Title: Re: numeric .if
Post by: thomasantony on April 07, 2005, 06:53:10 AM
Oh,
   Even if I have benn progging in ASM for about 1 and a half years I used to think jae and jge and jle and jbe were the same thing. :dazzled: . Though I alsways wondered how the CPU distinguished b/w signed and unsigned DWORD. I will give an example(this may be slightly off topic). I have seen stack frames that do add esp,0FFFFFFFCh instead of sub esp,4  ::)

Thomas :U
Title: Re: numeric .if
Post by: Jimg on April 07, 2005, 04:42:08 PM
Thank you one and all.

Since I always thought .if used signed compares, I've a lot of programs to look over and fix.

What is the downside of just putting an "assume eax:SDWORD" in the front of a program to be in effect throughout the whole program, that is where would it cause problems (other than the obvious case of using very large unsigned integers)?

Also, wouldn't it be a good thing if all the windows structures defined values that could be negative as sdword rather than just dword?

Later...

I found a more acceptable compromise (at least for me).

sif equ .if sdword ptr

I couldn't find where sif would conflict with any existing keywords, anyone know of any?
Title: Re: numeric .if
Post by: QvasiModo on April 08, 2005, 08:27:08 PM
QuoteWhat is the downside of just putting an "assume eax:SDWORD" in the front of a program to be in effect throughout the whole program, that is where would it cause problems (other than the obvious case of using very large unsigned integers)?
None that I know of. It's just that the source is more readable when the .if statements behave as they should by default...
QuoteAlso, wouldn't it be a good thing if all the windows structures defined values that could be negative as sdword rather than just dword?
Wouldn't make a difference, signed or unsigned is just your choice of opcodes to use but the values remain the same. :)