assembly lang 8085 .. how to find even/odd parity

user2171979 picture user2171979 · Mar 17, 2013 · Viewed 15.7k times · Source

I have the following code to count the number of 1s and save it to reg B. I need to modify it to find if the parity is even or odd...

LXI H, var1
MVI B, 00H
MVI D, 00H
MVI C, 08H
MOV A, M

LOOP: RAR
JNC SKIP
INR B
SKIP: DCR C
JNZ LOOP

HLT

var1: db 72

Answer

paxdiablo picture paxdiablo · Mar 17, 2013

With B holding the count of set bits in the input: just bitwise AND register B with the value 1. If the result is 1, it's odd parity. If the result is 0, it's even parity. That's because even numbers always have 0 in the least significant bit, odd numbers always have 1.

Accessing very vague memories of 8080-class assembly, but I think it would be something like:

          MOV      A, B
          ANI      01H
          JZ       par_even
par_odd   ; parity is odd here, work your magic
          JMP      par_done

par_even  ; parity is even here, work other magic

par_done  ; carry on processing here

For example, the value 72 that you're using, is 64 + 8 or binary 01001000.

So, assuming your bit counting code works correctly, register B will be set to two, or 00000010. ANDing that with 00000001 gives you 0, hence even parity.

Alternatively, the value 254, or 11111110 would set register B to 7, or 00000111. ANDing that with 00000001 gives 1, hence odd parity.

You can also use something like:

ANA    A
JPE    par_even

where A is the value rather than the count of 1-bits.

The reason you may need the ANA is to ensure the parity flag is set based on the contents of the accumulator. Only certain operations set the flags and ANDing the accumulator with itself will leave it untouched but set the P, S and Z flags based on it.