Puzzling response to logical OR instruction

Started by carlottagp, June 10, 2006, 11:39:53 AM

Previous topic - Next topic

carlottagp

I'm working on a program to find prime numbers up to 65,536 (equivalent to
one hex word.)

I can get to only half of this value, because there is a hangup when I reach
8000h.  At one point the program does a logical OR of the number under test.
Simplifying a little, I have, for instance:

     mov  DX, 8000h
     or     DX,DX
     jl       label

The conditional jump occurs at this point.  I would have expected this to
happen at FFFF, or maybe FF00, but not at 8000, where the highest bit
is zero.  It is as though the program is treating 8000h as a signed integer.

Can anyone shed a little light on this?

Michael







Ossa

Not too sure what you're trying to do, but remember that jl (jump if less) is a signed conditional jump - the unsigned equivalent is jb (jump if below).

[edit] just had another look because this is really confusing me... why are you using or? It won't work properly (for any result that I can think of) with jl or jb:

QuoteIntel IA-32 Manual Vol. 2B: OR - Logical Inclusive OR

Description

Performs a bitwise inclusive OR operation between the destination (first) and source (second) operands and stores the result in the destination operand location. The source operand can be an immediate, a register, or a memory location; the destination operand can be a register or a memory location. (However, two memory operands cannot be used in one instruction.) Each bit of the result of the OR instruction is set to 0 if both corresponding bits of the first and second operands are 0; otherwise, each bit is set to 1.

This instruction can be used with a LOCK prefix to allow the instruction to be executed atomically.

In 64-bit mode, the instruction's default operation size is 32 bits. Using an REX prefix in the form of REX.R permits access to additional registers (R8-R15). Using an REX prefix in the form of REX.W promotes operation to 64 bits. See the summary chart at the beginning of this section for encoding data and limits.

Flags Affected

The OF and CF flags are cleared; the SF, ZF, and PF flags are set according to the result. The state of the AF flag is undefined.

The jl instruction: "Jump short if less (SF != OF)." Basically only check the sign bit the way you have done it.

The jb instructio: "Jump short if below (CF=1)." Will not be using the result at all.

I think you mean to use the CMP instruction instead of the OR... but I could be wrong - there isn't enough code to tell.

Ossa
Website (very old): ossa.the-wot.co.uk

carlottagp

I must have been wool-gathering when I posted the question; obviously, the
high bit is a one, not a zero.  The reason for the OR was determining when the
prime I'm using for the test - squared - is greater than the number I'm evaluating;
no reason to try primes any higher than the square root of the number being
tested.  Originally I did a subtraction, then compared the result to zero.  Like the
logical OR instruction, this worked fine until I got to 8000h.  So I shall have to
modify the program somehow.  I will certainly try the jb instruction, having been
unaware of the significant difference between jb and jl.  Thanks.

Michael

Casper

Casper says...

Always use jb and ja for unsigned numbers.