Logical operations

General Assembly and Auto Assembler tutorials


Post Reply
User avatar
bbfox
Table Master
Table Master
Journeyman Hacker
Journeyman Hacker
Posts: 178
Joined: Sat Jul 23, 2022 8:59 am
Answers: 0
x 387

Logical operations

Post by bbfox »

This article is for newbies.

Warning: This post features AI-assisted content. While I created its foundational structure, an AI generated the content, which I then curated. If you prefer not to engage with such material, please use your browser's back button.



Prerequest:
CPU Registers: About Intel x64 CPU Registers



Logical operations
Logical operations are fundamental in computer science, allowing for bitwise manipulation of binary digits (bits) within data. These operations include AND, OR, NOT, and XOR, each performing a distinct function based on binary input. Below is an explanation and examples for each:


AND Operation
The AND operation takes two bits and returns 1 if both bits are 1, otherwise, it returns 0.

Truth Table:

0 AND 0 = 0
0 AND 1 = 0
1 AND 0 = 0
1 AND 1 = 1

      0    1
--------------
0  |  0    0
1  |  0    1

Binary Example: 1100 AND 1010 = 1000

 1 1 0 0
 1 0 1 0
---------
 1 0 0 0 

Assembly code for example above:

mov cl, 0xC0  ; 0000 1100 in binary
mov bl, 0xA0  ; 0000 1010 in binary
and cl, bl    ; 0000 1100 AND 0000 1010, store into cl


OR Operation
The OR operation takes two bits and returns 1 if at least one of the bits is 1, otherwise, it returns 0.

Truth Table:

0 OR 0 = 0
0 OR 1 = 1
1 OR 0 = 1
1 OR 1 = 1

      0    1
--------------
0  |  0    1
1  |  1    1

Binary Example: 1100 OR 1010 = 1110

 1 1 0 0
 1 0 1 0
---------
 1 1 1 0 

Assembly code for example above:

mov cl, 0xC0  ; 0000 1100 in binary
or  cl, 0xA0  ; 0000 1100 or 0000 1010, store into cl

NOT Operation
The NOT operation takes one bit and returns the opposite value, flipping 1 to 0 and 0 to 1.

Truth Table:

NOT 0 = 1
NOT 1 = 0

      0    1
--------------
      1    0

Binary Example: NOT 1100 = 0011

 1 1 0 0
---------
 0 0 1 1 

Assembly code for example above:

mov cl, 0xC0  ; 0000 1100 in binary
not cl        ; 1111 0011
not word ptr [MyVar1]  ; perform a NOT operation in address of [MyVar1], width 16-bits


XOR Operation (Exclusive OR)
The XOR operation takes two bits and returns 1 if the bits are different, otherwise, it returns 0.

Truth Table:

0 XOR 0 = 0
0 XOR 1 = 1
1 XOR 0 = 1
1 XOR 1 = 0

      0    1
--------------
0  |  0    1
1  |  1    0

Binary Example: 1100 XOR 1010 = 0110

 1 1 0 0
 1 0 1 0
---------
 0 1 1 0 

Assembly code for example above:

mov cl, 0xC0  ; 0000 1100 in binary
mov bl, 0xA0  ; 0000 1010 in binary
xor cl, bl    ; 0000 1100 xor 0000 1010, store result into cl


Practical Application

Logical operations are extensively used in computer programming and digital electronics to perform bit manipulation, which is crucial for tasks like data encryption, error detection, and controlling logic gates in hardware circuits. For example, XOR is particularly useful in cryptography for its property that applying the same XOR operation twice returns the original value:

If A XOR B = C, then C XOR B = A.

This reversible property makes XOR a popular choice for simple encryption and data obfuscation techniques.



Bitwise Operators

There are a set of bitwise insturctions used for shift bits: SHL, SHR, SAL, SAR, ROL, ROR, RCL, RCR.

Here are some of the instructions:
SHL: Shift Left, SAL: Shift Arithmetic Left
performs a logical left shift on the destination operand, filling the lowest bit with 0.
Shifting left 1 bit multiplies a number by 2; Shifting left n bits multiplies the operand by 2n
Image

SHL example:

Spoiler

Image


SHR: Shift right
performs a logical right shift on the destination operand. filling highest bit with 0.
Shifting right 1 bit divide a number by 2; Shifting right n bits divides the operand by 2n
Image


SAR: Shift Arithmetic Right
Performs a right arithmetic shift on the destination operand. This instruction preserves the number's sign.
Image


I create tables to suit my preferences. Table is free to use, but need to leave the author's name and source URL: https://opencheattables.com.
Table will not be up-to-date. Feel free to modify it, but kindly provide credit to the source.


Post Reply