Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

HELP!!! even/odd parity and binary count

Status
Not open for further replies.

MaxEd

Technical User
Jul 3, 2002
407
US
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.
 

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"?
 
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."
 
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..
 
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."
 
Thanks for all your help. It works great!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top