Encyclopedia > Parity Assembler Code

  Article Content

Parity/Assembler Code

The following Intel 8086 Assembler code tests the parity of a 16-bit unsigned integer specified in the AX register. If AX is even on entry, the carry flag will be clear. If it is odd on entry, the carry flag will be set. Test the carry flag by using the JC (jump on carry) or JNC (jump on no carry) instructions.

            SHR         AX, 1

This 1-instruction routine, shifts the least significant bit (representing the 1's place) out. Effectively dividing the number by 2. The CPU sets the carry flag if the bit that was shifted out was set, otherwise it clears the carry flag. Another method follows:

            AND         AX, 1

This 1-instruction routine performs a bitwise 'AND' operation with the number stored in AX and the number 1. This routine differs in that it will set AX to 0 of the number was even and set AX to 1 if the number was odd.

Both of these instructions modify the value of AX. If the value of AX is required after the operation, the following method may be desired:

            TEST        AX, 1

NOTE: The "TEST" instruction actually performs a bitwise AND on its operands. It varies from the AND instruction only in that it does not store the result of this operation. With this method if AX is even on entry, the zero flag will be set. If AX is odd on entry, the zero flag will be cleared. Test the zero flag using the JZ (jump on zero) or JNZ (jump on no zero) instructions. This method is the most efficient on the Intel platform.

In all cases, only a single instruction is used. And in all cases the parity check takes place by simply testing the least significant bit of the number (rather then by factoring the number, which would require many more instructions and would be far less efficient).

See: Parity



All Wikipedia text is available under the terms of the GNU Free Documentation License

 
  Search Encyclopedia

Search over one million articles, find something about almost anything!
 
 
  
  Featured Article
Michael Barrymore

... famous for his variety shows. This article is a stub. You can help Wikipedia by fixing it. E ...

 
 
 
This page was created in 46.5 ms