how do i test a register content for odd or even without modifying same?

i thought about using something like this:


mov edx,$20000000 ;value to test least significant bit in eax for odd/even test
bt eax,edx ;test for odd/even

is there a better way?
Posted on 2003-09-01 23:10:04 by jack
test eax, 1

je EVEN
bt eax,0

jnc EVEN
Posted on 2003-09-01 23:21:33 by bitRAKE
I don't know any asm, but here it is in BASIC pseudocode :P

IF number AND 1 THEN odd ELSE even
Posted on 2003-09-01 23:32:20 by Ninkazu

I don't know any asm, but here it is in BASIC pseudocode :P

IF number AND 1 THEN odd ELSE even

That corrupts the number.

I wanted to say

shr eax, 1
jc _odd

but too bad that corrupts the number too.
Posted on 2003-09-01 23:35:13 by roticv
One more non-destructive version!:
push eax

popf ; not for RING0
jc ODD
...and only four byte. :)
Posted on 2003-09-01 23:51:11 by bitRAKE
thanks much :alright:
Posted on 2003-09-02 06:16:11 by jack
BT? what does BT do?
Posted on 2003-09-02 11:58:29 by bj1500
BT (Bit Test) copies the selected bit to the carry flag.
Posted on 2003-09-02 12:00:42 by donkey
roticv, that basic code wouldn't modify the input number, just like the C code "if(num & 1)" doesn't corrupt num.
Posted on 2003-09-02 17:17:50 by f0dder
YEAH! You show him!
Posted on 2003-09-02 21:34:37 by comrade
I would say the 'test' method is best.

AFAIK BT is implemented as a CISC instruction.... could be wrong though.
Posted on 2003-09-03 06:14:24 by AmkG
So whats the worst way? :grin:
Posted on 2003-09-03 11:29:33 by ThoughtCriminal

So whats the worst way? :grin:

mov ecx,2
xor edx,edx
div cl
test edx,edx
jnz odd
; even
odd:
;odd
Posted on 2003-09-03 11:56:55 by donkey