News:

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

safe way to use .if

Started by ecube, May 21, 2008, 01:20:50 AM

Previous topic - Next topic

ecube

.if sdword ptr myvalue > 5


is that a safe way? I just want positive numbers compared

Jimg

It's perfectly safe to do, but what do you mean you "just want positive numbers compared"?

This form of if is for comparing signed numbers.  If you want numbers compared as if they were unsigned, just use the .if without the sdword ptr.

If you don't have any numbers greater than 2,147,483,647 (which is 800000000h - 1)  then it doesn't make any difference which one you use.

ecube

well yeah so .if -1>0 would give unexpected results correct? been awhile since I took math in college, signed is negative and positive numbers, and unsigned is positive only yes?

Jimg

yes,
mov eax,-1
.if eax > 0
   would be true because eax looks like 2,147,483,648 if you don't look at the sign.

One of my pet peeves with masm.  There should have been a .sif instruction for signed if's without having to use sdword ptr.]

You can also use a normal .if if the variable is declared as sdword-

local myvalue:sdword

mov myvalue,-1
.if myvalue > 5

   will work for negative numbers.  Confusing enough?

ecube

ok thankyou, i'll go through my 40k line project and see if I used if incorrectly, I been having weird bugs during testing so this maybe the cause :) have a great night!

hutch--

Cube,

If you don't want to have to remember the difference, you can use either form, DWORD PTR or SDWORD PTR to make sure you get what you are after.
Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php

ecube

ok i'm extremely confused now and I just changed about 800 .if's to .if SDWORD PTR...


IsVista proc
Local osvi:OSVERSIONINFOEXAX
mov osvi.dwOSVersionInfoSize, SIZEOF OSVERSIONINFOEX
invoke GetVersionEx, ADDR osvi
.if osvi.dwPlatformId == VER_PLATFORM_WIN32_NT
.if osvi.dwMajorVersion == 6 && osvi.dwMinorVersion == 0
.if osvi.wProductType==VER_NT_WORKSTATION
mov eax,1
ret
.endif
.endif
.endif
xor eax,eax
ret
IsVista endp   


that code example above is I change all the if's to .if SDWORD PTR it makes it not work, why? otherwise it works fine

hutch--

Cube,

It is probably because the values you are tested are UNSIGNED 32 bit values above 2 gig and they need to be evaluated as UNSIGNED, not SIGNED.

Disassemble the code with the .IF block and look at the conditional jumps for both and it will make sense.

To identify the code put about 4 nops before and after the code so you can easily find the .IF block when you disassemble it.
Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php

japheth

Quote from: E^cube on May 31, 2008, 06:52:18 AM
that code example above is I change all the if's to .if SDWORD PTR it makes it not work, why? otherwise it works fine

i.e. wProductType is just a BYTE. If it is prefixed with a typecast of SDWORD PTR Masm will create bad code.

jj2007

I like the sif idea ;-)

.nolist
include \masm32\include\masm32rt.inc

sif      equ .if sdword ptr
else     equ .else
endsif    equ .endif



.data
IsLower   db "Operand is < 0", 0
IsHigher   db "Operand is > 0", 0

.code

;read-only data before start
AppName     db "Test app:", 0
MyVar   dd 0

start:   call TestWin
   invoke ExitProcess, 0

TestWin proc
  mov eax, -123
  sif eax<0
   invoke MessageBox, NULL, addr IsLower, addr AppName, MB_OK
  .else
   invoke MessageBox, NULL, addr IsHigher, addr AppName, MB_OK
  endsif
  mov eax, 123
  sif eax<0
   invoke MessageBox, NULL, addr IsLower, addr AppName, MB_OK
  else
   invoke MessageBox, NULL, addr IsHigher, addr AppName, MB_OK
  endsif
  ret
TestWin endp
end start

gfalen

Typing dword ptr, byte ptr etc. is a real pain so I use these equates.


option dotname
.b  equ byte ptr
.d  equ dword ptr
.w  equ word ptr

.sb equ sbyte ptr
.sd equ sdword ptr
.sw equ sword ptr

ecube

ok the byte was the issue as he mentioned so thanks for that,but I fail to see why .if doesn't compared signed by default considering a lot of windows api returns can be a negative value? Because right now I changed all my .if's in the understanding it'll compared negative values correctly against positive, and positive against positive correctly?

PBrennick

ii2007,
Code does not assemble...


Microsoft (R) Macro Assembler Version 6.15.8803
Copyright (C) Microsoft Corp 1981-2000.  All rights reserved.

Assembling: D:\NewStuff\sif.asm
D:\NewStuff\sif.asm(5) : fatal error A1010: unmatched block nesting


Paul
The GeneSys Project is available from:
The Repository or My crappy website

jj2007

Paul,
First of all, welcome back to the forum!
The sif part worked fine with me, and while writing my post, I tried to polish it by using the same logic for .else and .endif.
It turns out that logic and Masm are not necessarily friends, but in any case apologies for not testing my code :red
Attachment works fine now.


[attachment deleted by admin]

PBrennick

ii2007,
The fact that the sif part works so well makes this a very useful thing. The rest was like icing on the cake but was not really central to the idea, anyhow.

Is it okay to add this to the GeneSys macro set?
Paul
The GeneSys Project is available from:
The Repository or My crappy website