News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

Testing an odd number

Started by frktons, August 27, 2010, 09:40:15 AM

Previous topic - Next topic

frktons

I've to test if an integer number is odd or even.
I've thought about a couple of solutions:

1. shift right by 1 position the number MOVed into a register and test the carry flag [I think at least]
2. Testing the 0 bit of the register into which I moved the number to test, and  acting accordingly.

I'd like to ask:

1. What is the fastest method, and if there is a better one than the 2 methods I'm thinking about?
2. What is the syntax of both these methods if I want to print "The number is ODD" or "The number is EVEN"?

Thanks for your help
Mind is like a parachute. You know what to do in order to use it :-)

bomz

and eax, 1
cmp eax, 1   
jne NotEven

frktons

Quote from: bomz on August 27, 2010, 09:53:20 AM
and eax, 1
cmp eax, 1   
jne NotEven

Thanks bomz.

What about this solution:

;-------------------------------------------------------------------------------------------------------------------------
; Test if a number is ODD or EVEN
; Date: 27 august 2010
;-------------------------------------------------------------------------------------------------------------------------


INCLUDE \masm32\include\masm32rt.inc

;------------------------------------------------------------------------------------------------------------------------------

.DATA

number    db    12


;------------------------------------------------------------------------------------------------------------------------------


.CODE


start:

    xor eax,eax

    mov al, number

    shr eax, 1

    jc  isodd

    print "The number is EVEN",13,10

    inkey

    jmp end_test

isodd:

    print "The number is ODD",13,10

    inkey


end_test:


    ret

end start


is it slower? or what else?
Mind is like a parachute. You know what to do in order to use it :-)

bomz


frktons

Quote from: bomz on August 27, 2010, 10:00:28 AM
and eax, 1
jz ODD

This is probably faster than the previous one.  :U

I'm looking at the BT mnemonic to see how it can be used.


Maybe something like:


BT number, 0
jc  ODD

Mind is like a parachute. You know what to do in order to use it :-)

bomz

thats all I know about SHR - I think GOOGLE knows how many each operation processor tact(?) needs

frktons

Thanks bomz. These examples should be enough for now  :U

Nice gif  :P
Mind is like a parachute. You know what to do in order to use it :-)

Neil

Just a thought,

and eax,1

This zero's all bits except bit 0, so if you want to preserve your original number, and is not the answer.

Neil


jj2007

Intel(R) Pentium(R) 4 CPU 3.40GHz (SSE3)
4552    cycles for 1000*test eax, 1, result=500
10634   cycles for 1000*bt eax, 0, result=500

4595    cycles for 1000*test eax, 1, result=500
10640   cycles for 1000*bt eax, 0, result=500


Testbed attached.

dedndave

;value in EAX:
        test    al,1
        jnz     value_is_odd

jj2007

Quote from: dedndave on August 27, 2010, 11:33:59 AM
;value in EAX:
        test    al,1
        jnz     value_is_odd


You won - three bytes shorter than my version  :bg

However, it's a lot slower:
Intel(R) Celeron(R) M CPU        420  @ 1.60GHz (SSE3)
2885    cycles for 1000*test eax, 1, result=500
4023    cycles for 1000*test al, 1, result=500
3001    cycles for 1000*bt eax, 0, result=500

dedndave

hiyas Jochen - good morning (here) - good afternoon (there)

i can't imagine that much difference in speed  :P
must be something wrong with the test   ::)   :lol

lingo

"The thieves are always liars"[/U]
As you saw his test program is proven crap, hence his results too...  :lol

frktons

Morning/Evening/Night Masters. Oh Mr Lingo is here. How are you Sir?

So from the tests Jochen has posted the faster instruction is:

test eax,1


Probably because on 32/64 bit machines dealing with 32/64 bits at a time
is the faster solution for the CPU.
I run the test on my pc and post here afterwards.

Edit: Well on my CPU BT is faster than TEST:
Quote
Intel(R) Core(TM)2 CPU          6600  @ 2.40GHz (SSE4)
2043    cycles for 1000*test eax, 1, result=500
2037    cycles for 1000*bt eax, 0, result=500

2019    cycles for 1000*test eax, 1, result=500
2035    cycles for 1000*bt eax, 0, result=500


--- ok ---


They are quite the same.
Mind is like a parachute. You know what to do in order to use it :-)