×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
• Talk With Other Members
• Be Notified Of Responses
• Keyword Search
Favorite Forums
• Automated Signatures
• Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

#### Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

# HELP!!! even/odd parity and binary count

## HELP!!! even/odd parity and binary count

(OP)
Hi,

I am trying to write some a program to count the number of a binary value in the main program and a proc to figure out the parity of a binary number.  I'm totally new to assembly and have no idea how to get started!

Thank you.

### RE: HELP!!! even/odd parity and binary count

you can do like that

test YourValue, 01 ;less significant bit 01 means ODD number
jz EvenNumber

;continue here for odd number

what do you mean "count the number of a binary value"?

### RE: HELP!!! even/odd parity and binary count

He's trying to do parity.

For instance, the number 85 in binary is:
01010101

Count the number of binary 1's: there are 4 binary ones, so it is EVEN parity.

On the other hand, the number 97:
01100001

has 3 binary ones, so it is ODD parity.

This is ridiculously hard to do in software, BTW.  Hardware is easy: just put 8 XOR gates to get the parity (0=even, 1=odd).

BTW there is a bit in the eflags that is called 'parity bit' but I am unsure whether this will work or if it has some low-level (system) use.

"Information has a tendency to be free.  Which means someone will always tell you something you don't want to know."

### RE: HELP!!! even/odd parity and binary count

thanks AmkG, got the point now

i think smthng like

MOV CX(ECX),16 (32)
MOV COUNTER,0

BEGIN:
SHR AX (EAX),01
TEST AL,01
JZ ZEROBIT
INC COUNTER

ZEROBIT:

LOOP BEGIN
TEST COUNTER,01
...

should work. u r right, i takes some code..

### RE: HELP!!! even/odd parity and binary count

Although I think the 386's BSR/BSF instructions might be useful... CISC instructions though, so they might not even be faster than a discrete instruction version.

Anyway it's probably easier this way:
MOV CX(ECX),16 (32)
MOV COUNTER,0

BEGIN:
SHR AX (EAX),01
;this is what I changed:
jnc zerobit
INC COUNTER

ZEROBIT:

LOOP BEGIN
TEST COUNTER,01

Why?  Because shr/shl will automatically load the discarded bit into the CARRY flag.  So what you now do is, you slowly load each bit into the CARRY flag using SHR.  Then each time through you check if the CARRY flag is set or reset, if it's set increment the counter.

"Information has a tendency to be free.  Which means someone will always tell you something you don't want to know."

### RE: HELP!!! even/odd parity and binary count

(OP)
Thanks for all your help.  It works great!

#### Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

#### Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Close Box

# Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

• Talk To Other Members
• Notification Of Responses To Questions
• Favorite Forums One Click Access
• Keyword Search Of All Posts, And More...

Register now while it's still free!