Part 7: Logical Instructions used in Assembly Language

Assembly Language

Assembly Logical Instructions

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

The format for these instructions:

Instruction

Format

AND AND operand1, operand2
OR OR operand1, operand2
XOR XOR operand1, operand2
TEST TEST operand1, operand2

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 or constant value. Although, 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 logical expressions bitwise AND operation. The operation returns 1, if the matching bits from both the operands are 1, otherwise it returns 0. For example:

Operand1: 0101
Operand2: 0011
——————————————–
After AND Operation -> Operand1:0001

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.

AND BL, 0FH ; This sets BL to 0000 1010

Example

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

OUTPUT

Even Number!
Change the value in the ax register with an odd digit, such as:

mov ax, 9h ; getting 9 in the ax
The program would display:

Odd Number!

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

The OR Instruction

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

For example,

Operand1: 0101
Operand2: 0011
——————————————–
After OR -> Operand1: 0111

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.

OR BL, 0FH ; This sets BL to 0011 1111

Example
The following example demonstrates the OR instruction. Let us store the value 5 and 3 in the AL and the BL registers, respectively, then the instruction,

OR AL, BL
should store 7 in the AL register:

section .text
global _start ;must be declared for using gcc

_start: ;tell linker entry point
mov al, 5 ;getting 5 in the al
mov bl, 3 ;getting 3 in the bl
or al, bl ;or al and bl registers, result should be 7
add al, byte '0' ;converting decimal to ascii

mov [result], al
mov eax, 4
mov ebx, 1
mov ecx, result
mov edx, 1
int 0x80

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

section .bss
result resb 1

OUTPUT

7

The XOR Instruction

The XOR instruction implements from the bitwise XOR operation. The XOR operation sets the resultant bit to 1, if 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,

Operand1: 0101
Operand2: 0011
—————————————
After XOR -> Operand1: 0110

XORing an operand with itself changes the operand to 0. This is used to clear a register.

XOR EAX, EAX

The TEST Instruction

The TEST instruction works similar with 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.

TEST AL, 01H
JZ EVEN_NUMBER
The NOT Instruction

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

For example,

Operand1: 0101 0011
After NOT -> Operand1: 1010 1100

0 Comments

You may find interest following article

Chapter 4 Relational Algebra

Relational Algebra The part of mathematics in which letters and other general symbols are used to represent numbers and quantities in formula and equations. Ex: (x + y) · z = (x · z) + (y · z). The main application of relational algebra is providing a theoretical foundation for relational databases, particularly query languages for such databases. Relational algebra...

Chapter 3 Components of the Database System Environment

Components of the Database System Environment There are five major components in the database system environment and their interrelationships are. Hardware Software Data Users Procedures Hardware:  The hardware is the actual computer system used for keeping and accessing the database. Conventional DBMS hardware consists of secondary storage devices, usually...

Chapter 2: Database Languages and their information

Database Languages A DBMS must provide appropriate languages and interfaces for each category of users to express database queries and updates. Database Languages are used to create and maintain database on computer. There are large numbers of database languages like Oracle, MySQL, MS Access, dBase, FoxPro etc. Database Languages: Refers to the languages used to...

Database basic overview

What is DBMS? A Database Management System (DBMS) is a collection of interrelated data and a set of programs to access those data. Database management systems (DBMS) are computer software applications that interact with the user, other applications, and the database itself to capture and analyze data. Purpose of Database Systems The collection of data, usually...

Laravel – Scopes (3 Easy Steps)

Scoping is one of the superpowers that eloquent grants to developers when querying a model. Scopes allow developers to add constraints to queries for a given model. In simple terms laravel scope is just a query, a query to make the code shorter and faster. We can create custom query with relation or anything with scopes. In any admin project we need to get data...