What does the 'and' instruction do to the operands in assembly language?

What does the 'and' instruction do in assembly language? I was told that it checks the bit order of the operands and sets the 1s to true and anything else to false, but I don't know what it actually does or what effect it has on the code.

354k 49 49 gold badges 685 685 silver badges 934 934 bronze badges asked Dec 4, 2018 at 0:18 user548010 user548010 31 1 1 gold badge 1 1 silver badge 1 1 bronze badge

This should be described in the documentation for any assembler that has an and instruction. It does a bit-wise Boolean "and" between two operands. In other words, corresponding bits (bit n in each operand) are anded, in the Boolean operation sense, giving bit n of the result. In Boolean logic, 1 and 1 = 1, but 0 and x (anything else) = 0. Thus, 10111010 and 01101011 results in 00101010 . If you're not familiar with Boolean logic, I suggest starting there and look it up since it's the rudimentary basis of how most electronic computers work.

Commented Dec 4, 2018 at 2:13 Does this answer your question? Understanding the bitwise AND Operator Commented Mar 6, 2021 at 6:11

4 Answers 4

AND instruction compares the bits in the 2 operands in the following manner:

Bit position 1234 5678 Operand A -- 1101 1001 Operand B -- 1001 0111 _________ Result ----- 1001 0001 

The bits at position 1, 4 and 8 are true in both bytes so the position 1,4 and 8 of the resulting byte will be true. The result will be stored in the first operand.

answered Mar 4, 2021 at 13:12 Serkratos121 Serkratos121 61 2 2 silver badges 4 4 bronze badges

For 32-bit registers, it does 32 separate/independent boolean and operations, one for each bit position. (true if both inputs are true, otherwise false.)

Like output[4] = a[4] & b[4] , where this pseudocode syntax is describing the inputs/output as arrays of bits.

It's exactly the same operation as C's bitwise & or &= operator.

(Not C's && logical-and operator, which checks for !=0 ).

answered Dec 4, 2018 at 0:58 Peter Cordes Peter Cordes 354k 49 49 gold badges 685 685 silver badges 934 934 bronze badges So it checks if both operands are the same? Commented Dec 4, 2018 at 1:59

@user548010: no, it intersects two bitmaps. Bit 3 of the result does not depend on bit 4 of either input.

Commented Dec 4, 2018 at 2:01

The instruction and performs bit-wise AND operation on its operands. For example the instruction and al, bl should compute the AND operation on the register al and bl (as illustrated by @Serkratos121) and store the result in al register.

It can be used to clear bit(s) off a register. A popular example for this is to convert a lowercase character to uppercase. To convert the m to M . One can write:

mov al, 'm' ; this moves the ascii code of m i.e. 109 to al register 

Now, to convert this uppercase, subtract 32 from al . But instead of using sub al, 32d , one can write:

and al, 11011111b ; this clears the 5th bit (LSB is 0th bit) 

So, al now contains 77 which is the ascii code for M .

answered Feb 1, 2022 at 12:22 Taimoor Zaeem Taimoor Zaeem 312 2 2 silver badges 14 14 bronze badges

If you're counting from zero, you should say "bit #5" to describe the ~(1<<5) position. Ordinal numbers like first, second, . fifth don't enumerate from zeroth). i.e. bit #5 is the 6th bit.

Commented May 5, 2022 at 8:21

The and instruction performs a bit-wise AND for every bit in a number.

Bit-Wise AND

Bit-Wise AND, not to be confused with C’s logical AND, checks each bit position. If it finds that both operando have a 1 bit in the same position, the output bit in the position will be a 1. If it find that both bits are 0 or that 1 bit is 0 and the other is 1 the output bit in that position with be 0.

Bit Position — 1234 5678 Operand 1 ———— 0111 0101 Operand 2 ———— 1101 1111 ————————- Output ——————- 0101 0101 

Functional Example

The and instruction has multiple uses. A common one is the convert lowercase ASCII characters to uppercase ones. This can be done by subtracting 32 but that will also effect uppercase letters.

mov dx, ‘a’ and dx, 11011111b 

The dx register will now contain A . It works like this:

Bit Position - 1234 5678 Operand 1 ———- 0110 0001 Operand 2 ———- 1101 1111 ————————- Output ——————- 0100 0001