About me
Home
Resumé
CV
Pictures

Course Work
CSE300-FA98

Teaching
Portfolio
CSE240-SP99
CSE207-FA03

Misc
eHarmony
Documents

Tools

Logic Instructions

I decided to put the logic instruction separate from the basic instructions since I believe that they require a greater explaination than the basic instructions. These instructions are often used in creative ways, usually to make a process simpler, or faster. Unfortunately, in the process, these tricks tend to make your code less readable until you are accustom to seeing them.


and destination, source
Bitwise AND the source with the destination and store in the destination. This is equivalent to using the & operator in C or C++. The source and destination must be of the same width (byte, word, etc). This is useful for certain mask operations. For example, if we have a bit pattern (01001000) in BX and we want to know if the only bit set is bit-3 we can call and bx, 11110111. If the zero flag is set after this operation, then the only bit set would have been in the fourth position. Do you see why this would be so? The result of the and instruction would leave bx=01000000 which is not equal to zero. If bit-6 was not equal to one, then the result would have been zero.

or destination, source
Bitwise OR the source with the destination and store in the destination. This is equivalent to using the | operator in C or C++. The source and destination must be of the same width (byte, word, etc). Like AND this can be used for certain mask operations. One example would be to convert from upper case letters to lower case letters. Upper case 'A' is equal to 0100 0001 and lower case 'a' is equal to 0110 0001. If you OR a character (upper or lower case) with 0010 0000 you will get the lower case version of it.

not destination, source
Bitwise NOT the source with the destination and store in the destination. This is equivalent to using the ! operator in C or C++. The source and destination must be of the same width (byte, word, etc). I'll let you be creative with this operator

xor destination, source
Bitwise XOR the source with the destination and store in the destination. This is equivalent to using the ^ operator in C or C++. The source and destination must be of the same width (byte, word, etc). This is one of the most commonly used logical operations as it only takes three clock cycles. On the early machines, mov bx, 0 took four clock cycles, so it was faster to xor bx, bx to clear the register. There are many other clever uses of XOR, but I will leave it up to you to decide how you want to use it.

shl destination
Bitwise shift left one position and store in the destination. This is equivalent to using the << operator in C or C++. Most people use this as a quick multiply by 2 (again, only takes two clock cycles) which is logically equivalent to shifting the bits one position to the left. In the 386+ modes, you can specify a number of bits to shift. You may want to look at rotate left (p.715) in addition to this function. Using only shift left and add, can you think of a way of doing a quick multiply by 10(decimal)? It should take you 4 instructions and only need two registers (or one register and one variable). The big advantage will be that you can do a multiply by 10 in eight clock cycles opposed to the 70 to 133 clock cycles nessary to call the mul operation. Some of this advantage goes away as faster hardware is thrown at solving the multiply problem on newer processors, but clever operations are always possible - you can be creative. Of course, the more creative you get, the more important it is that you comment your code well!

shr destination
Bitwise shift right one position and store in the destination. This is equivalent to using the >> operator in C or C++. Most people use this as a quick divide by 2 (again, only takes two clock cycles) which is logically equivalent to shifting the bits one position to the right. In the 386+ modes, you can specify a number of bits to shift. You may want to look at rotate right (p.716) in addition to this function.

One last function that I'm not going to cover, but you can look up in your book, is test. When you get some time, skim through your book (Appendix D p. 631-734) as it covers the all of the instructions.

Last Modified: January 26, 1999 - Barry E. Mapen