SoFunction
Updated on 2025-03-04

Assembly language AND instruction implements logic (bit-wise) and operations on two operands

The AND instruction performs (bit-wise) logical and (AND) operations between the corresponding bits of two operands and stores the results in the target operand:

AND destination,source

The following are allowed operand combinations, but the immediate operand cannot exceed 32 bits:

AND reg, reg
AND reg, mem
AND reg, imm
AND mem, reg
AND mem, imm

Operands can be 8-bit, 16-bit, 32-bit, and 64-bit, but both operands must be of the same size. Each pair of corresponding bits of the two operands follows the following operating principle: if both bits are 1, the result bit is equal to 1; otherwise the result bit is equal to 0.

The following table shows two input bits X and Y, and the third column is the value of the expression X^Y:

X Y X^Y
0 0 0
0 1 0
1 0 0
1 1 1

The AND instruction can clear 1 or more bits in an operand without affecting other bits. This technique is called bit shielding, just like when painting a house, you use masking tape to cover areas that don’t need to be painted (such as windows).

For example, suppose you want to copy a control byte from the AL register to the hardware device. And when bits 0 and bit 3 of the control byte are equal to 0, the device is reset. Then, if you want to reset the device without modifying other bits of AL, you can use the following instructions:

and AL, 11110110b                                                                                                                        �

For example, let AL initialize to binary number 1010 1110, and after AND operation with 1111 0110, AL equals 1010 0110:

mov al,10101110b
and al, 11110110b    ;Result in AL = 1010 0110

Logo position

The AND 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. For example, the result of the following instruction is stored in the EAX register, assuming its value is 0. In this case, the zero flag will be set to 1:

and eax,1Fh

Convert characters to uppercase

The AND directive provides an easy way to convert characters from lowercase to uppercase. If you compare the ASCII codes in uppercase A and lowercase a, you will find that only bit 5 is different:

0  1  1  0  0  0  0  1 = 61h ('a')
0  1  0  0  0  0  0  1 = 41h ('A')

Other alphabetical characters have the same relationship. AND any character with the binary number 1101 1111, then all bits except bit 5 remain unchanged, and bit 5 clears 0. In the following example, all characters in the array are converted to uppercase:

  .data  array BYTE 50 DUP(?)  .code      mov ecx,LENGTHOF array      mov esi,OFFSET

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.