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.

2

3 Answers

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.

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

2

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.

1

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like