odd number problem
Author Message odd number problem

Hi,
Suppose I have a number in BX and I want to know if it is odd.  Won't the
following work:

SHR BX, 1

even_number_message:
....

odd_number_message:
...

Won't the SHR shift the right-most bit into the carry flag, which I could
then test to determine if the right-most bit was 1 or 0 (and thus, even or
odd)?

Also, how would I do the same thing if I were to use masking?

Thanks,
Krishnan

Sun, 06 Feb 2005 05:59:42 GMT  odd number problem
Yes, but why?  A simple TEST is easier to understand and on the 486 the
processor cycles are much less.

Quote:
> Hi,
> Suppose I have a number in BX and I want to know if it is odd.  Won't the
> following work:

> SHR BX, 1

> even_number_message:
> ....

> odd_number_message:
> ...

> Won't the SHR shift the right-most bit into the carry flag, which I could
> then test to determine if the right-most bit was 1 or 0 (and thus, even or
> odd)?

> Also, how would I do the same thing if I were to use masking?

> Thanks,
> Krishnan

Sun, 06 Feb 2005 06:35:14 GMT  odd number problem
Um, how do I use that instruction for this problem?

Quote:
> Yes, but why?  A simple TEST is easier to understand and on the 486 the
> processor cycles are much less.

> > Hi,
> > Suppose I have a number in BX and I want to know if it is odd.  Won't
the
> > following work:

> > SHR BX, 1
> > JC addr_of_odd_number_message

> > even_number_message:
> > ....

> > odd_number_message:
> > ...

> > Won't the SHR shift the right-most bit into the carry flag, which I
could
> > then test to determine if the right-most bit was 1 or 0 (and thus, even
or
> > odd)?

> > Also, how would I do the same thing if I were to use masking?

> > Thanks,
> > Krishnan

Sun, 06 Feb 2005 07:05:55 GMT  odd number problem

Quote:
> Um, how do I use that instruction for this problem?

test bx,1
jz odd        ; or jnz even

The "test" instruction do the same thing as the "and"
instruction but it doesn't change the value of
the register.

And also like david said, it's faster on the 486 (who
care) but probably also on the p4 (due to it's lack
of a fast shift barrel). But i don't have a p4 so I'm
not sure here.

Anyway, for a simple odd/even test the "test" or "and"
instructions are much more elegant to most
programmers then a right shift.

Sun, 06 Feb 2005 11:33:47 GMT  odd number problem

Quote:
> test bx,1
> jz odd        ; or jnz even

Oooooooooooops, it's the reverse :)

Sun, 06 Feb 2005 11:40:04 GMT  odd number problem

Hi Krishnan!

Quote:
> > > Suppose I have a number in BX and I want to know if it is odd.

> > Yes, but why?  A simple TEST is easier to understand and on the 486 the
> > processor cycles are much less.

> Um, how do I use that instruction for this problem?

The "test" instruction performs an "imaginary and" - that is, the flags
are set as per "and", but the result is not stored - the destination
remains unchanged.

So "test bx, 1" would set the zero-flag if bit 0 of bx was clear (bx is
even).

test bx, 1
jnz is_odd

should do what you want - and won't destroy bx, as the "shr" method
does.

Best,
Frank

Sun, 06 Feb 2005 11:44:51 GMT

 Page 1 of 1 [ 6 post ]

Relevant Pages