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!
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."
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.
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."
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.