Assembly - Logical Operators / Instructions

Prahlad Godara ------ From DOOSEEP

Logical operators evaluate Boolean expressions and determine whether the entire expression is true or false based on the operator used. Logical instructions are also used with condition instructions in assembly language. And let's decide. The next code block to run if the expression is true.

Logical Operators in Assembly - AND, OR, XOR, TEST and NOT


Operators - AND, OR, XOR, TEST and NOT

The NASM assembly language processor instruction set provides AND, OR, XOR, TEST and NOT boolean logic, which tests, sets and clears bits as required by the program.

The format for these instructions −

Sr.No. Instruction Format
1 AND AND operand1, operand2
2 OR OR operand1, operand2
3 XOR XOR operand1, operand2
4 TEST TEST operand1, operand2
5 NOT NOT operand1

In all cases the first operand can be either in a register or in memory. The second operand can be either in register/memory or an immediate (constant) value. However, memory-to-memory operations are not possible. These instructions compare or match the bits of the operands and set the CF, OF, PF, SF, and ZF flags.

  Operand1: 	Operand1: register or memory
  Operand2: 	Operand2: register/memory or immediate (constant)
--------------------------------------
After AND ->  Operand: register/memory

AND instruction

AND instruction is used to support logical expressions by performing bitwise AND operation. The bitwise AND operation returns 1 if the matching bits of both operands are 1, otherwise it returns 0.

Syntax
 AND    Register or memory,    register/memory or immediate (constant)

For example

AND  AL,  01H


THE OR operator

OR instruction is used to support logical expression by performing bitwise OR operation. If the matching bits of either or both operands are one. So the bitwise OR operator returns 1, if both bits are zero, it returns 0.

The OR operation can also be used to set one or more bits.

Syntax
 OR    Register or memory,    register/memory or immediate (constant)

For example

OR  AL, BL

XOR instruction

XOR instruction implements the bitwise XOR operation. If the bits from both the operands are different. The XOR operation sets the resulting bit to 1, if the bits of the operands are identical (both 0 or both 1), the resulting bit is set to 0.

XORing an operand with itself turns the operand into 0. It is used to clear a register.

Syntax
 XOR    Register or memory,    register/memory or immediate (constant)

For example

XOR  EAX,  EAX

TEST instructions

The TEST instruction works in the same way as the AND operation, but unlike the AND instruction, it does not change the first operand.

Syntax
 TEST    Register or memory,    register/memory or immediate (constant)

For example

TEST  AL,  01H

NOT Instruction

The NOT instruction implements the bitwise NOT operation. NOT operation reverses the bits in an operand. The operand can be either in the register or in the memory. Returns 0 if the result is 1. Returns 1 if it is 0.

Syntax
  NOT    Register or memory 

NOTE - Logical Instructions/Operators are used to compare more than one condition. Like true and false in other languages, 1 and 0 are used in assembly.

Exampal

Let us take an example. If you want to check whether the given number is odd or even.


    section .text
   global _start            ;must be declared for using gcc
	
_start:                     ;tell linker entry point
   mov   ax,   8h           ;getting 8 in the ax 
   and   ax, 1              ;and ax with 1
   jz    evnn
   mov   eax, 4             ;system call number (sys_write)
   mov   ebx, 1             ;file descriptor (stdout)
   mov   ecx, odd_msg       ;message to write
   mov   edx, len2          ;length of message
   int   0x80               ;call kernel
   jmp   outprog

evnn:   
  
   mov   ah,  09h
   mov   eax, 4             ;system call number (sys_write)
   mov   ebx, 1             ;file descriptor (stdout)
   mov   ecx, even_msg      ;message to write
   mov   edx, len1          ;length of message
   int   0x80               ;call kernel

outprog:

   mov   eax,1              ;system call number (sys_exit)
   int   0x80               ;call kernel

section   .data
even_msg  db  'Even Number!' ;message showing even number
len1  equ  $ - even_msg 
   
odd_msg db  'Odd Number!'    ;message showing odd number
len2  equ  $ - odd_msg

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

 Even Number!


Tags- education, Assembly - Logical Operators , assembly language, type of Assembly Logical Operators , example program in assembly language