Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

assembly

# What are logical instructions in assembly language?

Rukhshan Haroon

In assembly language, programmers can make use of the following five logical instructions:

• AND – Returns 1 only when both bits are 1; otherwise, it returns 0.
• NOT – Reverses each bit.
• OR – Returns 1 if one or one of the two bits or both bits are 1. Returns 0 if both bits are 0.
• XOR – Returns 1 only if one of the two bits is 1; returns 0 in all other cases.
• TEST – Same as the AND operator, but does not change the value of the first operand.

Logical operators are executed bit-wise, which means that each bit in the first operand is compared to the corresponding bit (in accordance with the position) in the second operand. The new bit for that position in the resulting number depends on the semantics of the logical operator.

The NOT operator only takes one operand and reverses each bit one by one. Except for the TEST operator, all logical operators save the resulting number in the first operand.

## Syntax

The syntax for AND, TEST, XOR, and OR instructions is as follows:

operator_name operand_1 operand_2 ;


On the other hand, the NOT instruction only takes in one operand:

not operand_1;

## Example

The program below performs several logical operations on an arbitrary number stored in the eax general purpose register.

Upon successful execution of all the operations, the program checks whether or not the resulting number is even or odd. A display message is printed onto the console before the program ends, as we can see here:

section .text
global _start

_start:                     ;denotes the entry point of the program
mov   eax, 4h            ;stores 0100 in eax
and   eax, 1h            ;performs 0100 AND 0001 and saves 0000 in eax
or    eax, 1h            ;performs 0000 OR 0001 and saves 0001 in eax
and   eax, 2h            ;performs 00001 AND 0010 and saves 0000 in eax
xor   eax, 1h            ;performs 0000 XOR 0001 and saves 0001 in eax
not   eax                ;saves 1110 in eax
and   eax, 1h            ;performs 1110 AND 0001 and saves 0000 in eax, which is zero and even

jz    if_even            ;jz stands for 'jump if zero'. if value in eax is 0, program execution jumps to the if_even section, skipping all the lines in between. If not, it executes the next instruction without altering the program flow.

mov   eax, 4             ;system call number for the write system call saved in eax
mov   ebx, 1             ;file descriptor of the stdout stream saved in ebx
mov   ecx, odd           ;display message for odd number saved in ecx
mov   edx, len_odd       ;length of display message saved in edx
int   0x80               ;calls kernel to call the write system call
jmp   exit_prog          ;jumps to the exit_prog section, skipping all code in the if_even section

if_even:

mov   eax, 4             ;system call number for the write system call saved in eax
mov   ebx, 1             ;file descriptor of the stdout stream saved in ebx
mov   ecx, even          ;display message for odd number saved in ecx
mov   edx, len_even      ;length of display message saved in edx
int   0x80               ;calls kernel to call the write system call

exit_prog:

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

section   .data
even  db  'The final number was an even number!' ;display message for even number
len_even  equ  $-even ;length of the display message 'even' odd db 'The final number was an odd number!' ;display message for odd number len_odd equ$-odd                              ;length of the display message 'odd'

RELATED TAGS

assembly

CONTRIBUTOR

Rukhshan Haroon