Evaluating for Less Than Zero
JDOhio
Posts: 72
I have found many methods to evaluate if a variable is less than zero but I hadn't found any topics here. Consider
I would prefer that the code inside the IF execute however I have not found it to be the case. The same is true if I substitute
myValue2=myValue2-1
for
DEC myValue2
My questions:
Can I use a combination of the Carry bit or Digital Carry bit to indicate that a value is less than zero?
Does this depend on CARRYX?
Is there an assembly instruction I could use to evalute the result (S/X B uses CJAE, I believe)?
Joe
myValue1 VAR Byte myValue2 VAR Byte myValue1=0 myValue2=0 DEC myValue2 IF myValue2 < myValue1 THEN 'some code ENDIF
I would prefer that the code inside the IF execute however I have not found it to be the case. The same is true if I substitute
myValue2=myValue2-1
for
DEC myValue2
My questions:
Can I use a combination of the Carry bit or Digital Carry bit to indicate that a value is less than zero?
Does this depend on CARRYX?
Is there an assembly instruction I could use to evalute the result (S/X B uses CJAE, I believe)?
Joe
Comments
A value is only negative (less than zero) if you choose to look at it that way.
So with byte variables -1 is equal to 255. It just depends on what YOU want it to be.
Normal signed values use the MSB to indicate if the value is negative.
If the MSB is high, then the value is negative.
It gets tricky to do the comparision because either one, none, or both of the value could be negative. And you must trap all conditions.
Bean.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Cheap 4-digit LED display with driver IC·www.hc4led.com
Low power SD Data Logger www.sddatalogger.com
"Remember, you are unique, just like everyone else." Unknown.
·
You're right but I hadn't thought of it that way! (Too spoiled with client/server tools I bet)
Thanks,
Joe
You could use the following code:
myValue1·VAR·Byte
myValue2·VAR·Byte
diff·VAR Byte
myValue1=0
myValue2=0
DEC·myValue2
diff·= myValue2 - myValue1
IF·diff >= 128·THEN
··'some·code
ENDIF
The MSB of diff will be set if myValue2 is less than myValue1.
Dave
Thanks!
Joe