## 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.

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

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

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

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