-2

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.

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
user548010
  • 21
  • 1
  • 1
  • 1
  • 1
    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. – lurker Dec 04 '18 at 02:13
  • 2
    Does this answer your question? [Understanding the bitwise AND Operator](https://stackoverflow.com/questions/3427585/understanding-the-bitwise-and-operator) – phuclv Mar 06 '21 at 06:11

4 Answers4

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.

Serkratos121
  • 41
  • 1
  • 4
1

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).

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
1

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.

Taimoor Zaeem
  • 190
  • 1
  • 12
  • If you're counting from zero, you should say "bit #5" to describe the `~(1<<5)` position. [Ordinal numbers](https://en.wikipedia.org/wiki/Ordinal_numeral) like first, second, ..., fifth don't enumerate from zeroth). i.e. bit #5 is the 6th bit. – Peter Cordes May 05 '22 at 08:21
1

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
Markian
  • 322
  • 1
  • 12
  • `sub dx, 32` isn't slower than `and` on any x86 CPUs I'm aware of (https://agner.org/optimize/ / https://uops.info/, and historically https://www2.math.uni-wuppertal.de/~fpf/Uebungen/GdR-SS02/opcode_i.html), or non-x86. The main reason to use `and` or `or` on chars is that if it was already an upper-case letter, `and` doesn't change it, unlike `sub`. So isalpha for ASCII can be `and` / `sub dx, 'A'` / `cmp dx, 25` / `ja non_alphabetic` as in [What is the idea behind ^= 32, that converts lowercase letters to upper and vice versa?](//stackoverflow.com/a/54585515) – Peter Cordes Jan 12 '23 at 04:07
  • Also, `32b` isn't a valid integer constant in assembly; you mean `32` (decimal) or `100000b`. – Peter Cordes Jan 12 '23 at 04:10