SoFunction
Updated on 2025-04-06

Assembly language XOR instruction: perform logical (bit-wise) XOR operation on two operands (recommended)

Assembly Language

Assembly language is a low-level language used in electronic computers, microprocessors, microcontrollers, or other programmable devices, also known as symbolic language. In assembly language, mnemonics are used instead of the opcode of machine instructions, and address symbols or labels are used instead of the address of instructions or operands. In different devices, assembly language corresponds to different machine language instruction sets and is converted into machine instructions through assembly process. Specific assembly language and specific machine language instruction sets are one-to-one, and cannot be directly ported between different platforms.

The XOR instruction performs a (bit-wise) logical XOR (XOR) operation between the corresponding bits of two operands and stores the result in the target operand:

XOR destination, source

The combination and size of the XOR instruction operand is the same as that of the AND instruction and the OR instruction. Each pair of corresponding bits of the two operands applies the following operating principle: If the values ​​of the two bits are the same (both 0 or the same 1), the result bit is equal to 0; otherwise the result bit is equal to 1.

The following table describes the Boolean operation X㊉y:

x y x㊉y
0 0 0
0 1 1
1 0 1
1 1 0

The value of the XOR with 0 remains unchanged, and the XOR with 1 is triggered (replenished). If you perform XOR operations on the same operand twice, the result will be reversed to itself. As shown in the following table, bit x and bit y perform X-OR twice, and the result is reversed to the initial value of x:

x y x㊉y (x㊉y)㊉y
0 0 0 0
0 1 1 0
1 0 1 1
1 1 0 1

The "reversible" property of XOR makes it an ideal tool for simple symmetric encryption.

Logo position

The XOR instruction always clears the overflow and carry flags and modifies the symbol flags, zero flags, and parity flags based on the value of the target operand.

Check the parity flag

Parity check is a function implemented on a binary number, which calculates the number of 1 in the number; if the calculation result is an even number, it is said that the number is even parity; if the result is an odd number, it is odd parity.

In the x86 processor, the parity flag is set to 1 when the lowest byte of the target operand for a bitwise operation or arithmetic operation is even checked. On the contrary, if the operand is odd check, the odd flag bit is cleared by 0. An effective way to check the parity of a number without modifying its value is to XOR the number with 0:

mov al,10110101b                      ;5 1, odd verification
xor al, 0                                                                                                                            �
mov al, 11001100b                                                                                                                                                                �
xor al, 0                                                                                                                            �

Visual Studio uses PE=1 to represent even parity, and PE=0 to represent odd parity.

16-bit parity

For 16-bit integers, the parity of numbers can be detected by exORing their high and low bytes:

mov ax,64Clh    ;0110 0100 1100 0001
xor ah, al             ;Parity flag position 1 (even)

Think of the set bit in each register (bit equal to 1) as a member in an 8-bit set. The XOR instruction clears the members in the intersection of two sets by 0 and forms a union of the remaining bits. The parity of this union is the same as the parity of the entire 16-bit integer.

So what about the 32-digit value? If the bytes of a numeric value are numbered, from B₀ to B₃, then the expression for calculating parity is: B₀ XOR B₁  XOR B₂  XOR B₃.

Summarize

The above is the assembly language XOR instruction introduced to you by the editor: perform logical (bit-wise) XOR operations on two operands. I hope it will be helpful to you. If you have any questions, please leave me a message and the editor will reply to you in time. Thank you very much for your support for my website!
If you think this article is helpful to you, please reprint it. Please indicate the source, thank you!