Logic Instructions
Manipulating series of ones and zeroes

Definitions of the Logic Instructions
AND A,B = Performs AND operation on each individual bit of A and B. A must be a register or memory location. B can be a memory location, register, or number. Result is stored to A.
OR A,B = Performs OR operation on each individual bit of A and B. A must be a register or memory location. B can be a memory location, register, or number. Result is stored to A.
XOR A,B = Performs XOR operation on each individual bit of A and B. A must be a register or memory location. B can be a memory location, register, or number. Result is stored to A.
NOT A = Performs NOT operation on each individual bit of A. A must be a register or memory location. Result is stored to A.

So far, we've only seen what happens when we perform an AND, OR, XOR, or NOT operation on a single bit (a single binary value). But of course, assembly performs these operations in large groups of bits. Those bits are held and stored in registers, of course, as plain-old hexadecimal numbers.

Now, I will be representing the logic operations (operations that involve AND, OR, XOR, NOT) using "columnar mathematics".

Stop panicking. You are not going to have to memorize what "columnar mathematics" are, because you already know how to do it (at least, I hope you already know how to do it).

For example, this is how you would add 123 to 456 using columnar mathematics:
   123
+  456
   579
See? The numbers are added in columns. Oh wow! It's just elementary school math! Now let's apply that to assembly, except not with addition:
     00010010
AND  01100110
     00000010  ;an AND operation is performed on each column of bits.
10010 (bin) is 18 (dec). 1100110 (bin) is 102 (dec). 10 (bin) = 2 (dec).
That means, you could write the above math statement as "18 AND 102 = 2"

Setting Bits
To forcibly set binary bits, use the OR statement:
     00101001
OR   01111010
     01111011  ;an OR operation is performed on each column of bits.
Use a 1 to force that bit to become 1. If you want to leave a particular bit unchanged, use a 0.

Resetting Bits
To forcibly reset binary bits, use the AND statement:
     00111011
AND  01010110
     00010010  ;an AND operation is performed on each column of bits.
Use a 0 to force that bit to become 0. If you want to leave a particular bit unchanged, use a 1.

Flipping Bits
To flip particular binary bits, use the XOR statement:
     00111011
XOR  01110110
     01001101
Use a 1 to flip the other corresponding bit. If you don't want to flip a particular bit, use 0.

Flip All Bits
To flip all the bits of a particular number, use the NOT statement:
NOT  01110110
     10001001
NOT will simply change the value of each bit into the opposite value.

Example Using Real Code
Instruction      Comment
MOV EAX,5765     ;store 5765 (hex) to EAX
AND EAX,34C      ;perform AND operation on each individual bit between EAX and 34C (hex)
So, what actually happens during the above sequence of two instructions? First, let's convert 5765 (hex) and 34C (hex) to binary:

5765 (hex) = 101011101100101
34C (hex) = 1101001100

Well, those are two massive-looking numbers. After that, perform a bitwise AND:
     101011101100101   ;EAX holds this number, which is equal to 5765 (hex)
AND  000001101001100   ;this is equal to 34C (hex)
     000001101000100
The answer, 1101000100 (bin), is stored into EAX. Of course, it's stored in hex format. So EAX now holds 344 (hex), which is equal to 1101000100 (bin).

Navigation
Previous Lesson: Logic
Next Lesson: ASM Flags
Table of Contents