ALT by Bilal Ahmed Shaik - HTML preview

PLEASE NOTE: This is an HTML preview only and some elements such as links or page numbers may be incorrect.
Download the book in PDF, ePub, Kindle for a complete version.

img102.png

 

  Logical Instructions

 

  The processor instruction set provides the instructions AND, OR, XOR, TEST and NOT Boolean logic, which tests, sets and clears the bits according to the need of the program.

 

  The format for these instructions:

img103.png 

  The first operand in all the cases could be either in register or in memory. The second operand could be either in register/memory or an immediate (constant) value. However, memory to memory operations are not possible.

  These instructions compare or match bits of the operands and set the CF, OF, PF, SF and ZF flags.

 

  The AND Instruction

 

  The AND instruction is used for supporting logical expressions by performing bitwise AND operation. The bitwise AND operation returns 1, if the matching bits from both the operands are 1, otherwise it returns 0. For example: img104.png

  The AND operation can be used for clearing one or more bits. For example, say, the BL register contains 0011 1010. If you need to clear the high order bits to zero, you AND it with 0FH.

 

img105.png 

  Let's take up another example. If you want to check whether a given number is odd or even, a simple test would be to check the least significant bit of the number. If this is 1, the number is odd, else the number is even.

 

  Assuming the number is in AL register, we can write:

img106.png

  The following program illustrates this:

 

  Example:

img107.png 

  When the above code is compiled and executed, it produces fol owing result:

 

img108.png 

  Change the value in the ax register with an odd digit, like:

img109.png

  The program would display:

 

img110.png 

  Similarly to clear the entire register you can AND it with 00H.

 

  The OR Instruction

 

  The OR instruction is used for supporting logical expression by performing bitwise OR operation. The bitwise OR operator returns 1, if the matching bits from either or both operands are one. It returns 0, if both the bits are zero.

 

  For example,

 

img111.png

img112.png

 

  The OR operation can be used for setting one or more bits. For example, let us assume the AL register contains 0011 1010, you need to set the four low order bits, you can OR it with a value 0000 1111, i.e., FH.

 

img113.png 

  Example:

 

  The follow ing example demonstrates the OR instruction. Let us stor e the value 5 and 3 in the AL and the BL register respectively. Then the instruction,

 

img114.png 

  should store 7 in the AL register:

 

 img115.png

 

When the above code is compiled and executed, it produces following result:

 

img116.png

  The XOR Instruction

 

  The XOR instruction implements the bitwise XOR operation. The XOR operation sets the resultant bit to 1, i f and only if the bits from the operands are different. If the bits from the operands are same (both 0 or both 1), the resultant bit is cleared to 0.

 

  For example,

img117.png 

  The TEST Instruction

 

  The TEST instruction works same as the AND operation, but unlike AND instruction, it does not change the first operand. So, if we need to check whether a number in a register is even or odd, we can also do this using the TEST instruction without changing the original number.

 

img118.png 

  The NOT Instruction

 

  The NOT instruction implements the bitwise NOT operation. NOT operation reverses the bits in an operand. The operand could be either in a register or in the memory.

 

  For example,

img119.png