Question

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
Was it helpful?

Solution 2

An even parity means that there's an even number of bits set in an octet. According to this brief introduction to Intel 8085 architecture:

Flag is an 8-bit register containing 5 1-bit flags:
- Sign - set if the most significant bit of the result is set.
- Zero - set if the result is zero.
- Auxiliary carry - set if there was a carry out from bit 3 to bit 4 of the result.
- Parity - set if the parity (the number of set bits in the result) is even.
- Carry - set if there was a carry during addition, or borrow during subtraction/comparison.

The parity flag can be most easily tested with conditional branches (link to source)

JPE label;   // Jump if parity is Even 
JPO label;   // or jump if parity is Odd

The flag is set by most arithmetic and logical instructions that deal with a single output registers. Counter-examples are MOV, and CMP, which doesn't store a result. Arithmetic operations (INX, DEX etc.) that modify a register pair in turn do not have an unambiguous result.

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top