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).
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.
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
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:
Hi,
How does the code actually look like in signed comparison. Doesn't it use the normal cmp . Or is it something else?
Thomas
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
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
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
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?
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. :)