The MASM Forum Archive 2004 to 2012

General Forums => The Workshop => Topic started by: msmith on March 19, 2006, 07:17:20 AM

Title: Testing Memory Location for Zero/Minus
Post by: msmith on March 19, 2006, 07:17:20 AM
I cannot find an X386 instruction to test a memory location for Zero/Minus.

Is there a single instruction solution to do this?
Title: Re: Testing Memory Location for Zero/Minus
Post by: zooba on March 19, 2006, 08:03:25 AM
No. You'll need to compare with zero and then jump/set/cmov depending on the result.
Title: Re: Testing Memory Location for Zero/Minus
Post by: msmith on March 19, 2006, 08:29:53 AM
Thanks zooba,

The rules for a FOR loop require that I know which branch (greater or less) depending on the step arg. In this case the step arg is a constant, but it could just as well be a var.

It turns out that I could just load it into ebx as follows:


; LN:12 for i=10 to 1 step -2
mov eax,10
mov [i],eax
mov eax,1
mov [_LopVec1],eax
mov eax,0
mov edx,2
sub eax,edx
mov [_LopVec1+4],eax
_Lbl6:
mov eax,[i]
mov ebx,[_LopVec1+4]
or ebx,ebx
js _Lbl9
cmp eax,[_LopVec1]
jg _Lbl8
jmp _Lbl10
_Lbl9:
cmp eax,[_LopVec1]
jl _Lbl8
_Lbl10:


Would it be better to replace:

mov ebx,[_LopVec1+4]
or ebx,ebx


With...


cmp [_LopVec1+4],0


This also seems to work, but since what i really need to know is the sign of the step arg, the or ebx,ebx seems more straightforward.
Title: Re: Testing Memory Location for Zero/Minus
Post by: zooba on March 19, 2006, 09:13:02 AM
If it comes down to speed, you'd just have to benchmark it. If this is part of your compiler, it's probably not worth it. Personally, I'd use cmp mem, 0, simply to save the register.