Logo
Articles Compilers Libraries Books MiniBooklets Assembly C++ Linux Others Videos
Advertisement

Article by Ayman Alheraki on January 11 2026 10:38 AM

#19 Mastering GAS A Complete Guide to the GNU Assembler

#19 Mastering GAS: A Complete Guide to the GNU Assembler.

 

Series for explaining and teaching GNU GAS Assembler using AT&T syntax – all codes are reviewed and tested daily on

Fedora Linux 42

GNU Assembler version 2.44-6

Logical Operations (and, or, xor, not)

Logical operations are an essential part of low-level programming, allowing you to manipulate bits and perform bitwise operations directly on data stored in registers or memory. These operations are commonly used in tasks such as masking, flags manipulation, boolean logic, and setting or clearing specific bits in a value.

In GAS (GNU Assembler), logical operations are available for manipulating data at the bit level. These instructions are supported across most processor architectures (e.g., x86, x86-64, ARM, MIPS, RISC-V), though the implementation and behavior might vary slightly depending on the architecture. Below are the most common logical instructions: and, or, xor, and not.

and (Bitwise AND)

The and instruction performs a bitwise AND operation between two operands. The result of this operation is stored in the destination operand. A bitwise AND takes two binary numbers and compares their bits at each position; the result bit is 1 if both bits are 1, otherwise it is 0. This instruction is typically used to clear specific bits in a register or memory location by applying a mask.

Syntax:

  • source: The operand to be ANDed with the destination operand (could be an immediate value, register, or memory).

  • destination: The register or memory location where the result of the AND operation is stored.

Example (x86):

In the above example:

  • The first instruction applies a bitwise AND between the contents of registers EAX and EBX, storing the result in EBX.

  • The second instruction performs a bitwise AND on EAX and the immediate value 0xFF, effectively masking the upper bits of EAX.

Key Points:

  • The and operation is often used in masking operations, where you clear or isolate specific bits in a register.

  • The operation does not affect the carry flag or other condition flags by default but can affect flags like zero and sign.

or (Bitwise OR)

The or instruction performs a bitwise OR operation. The result bit is 1 if at least one of the bits being compared is 1. This operation is typically used to set specific bits in a register or memory location, often used in tasks like setting flags or enabling specific options in control registers.

Syntax:

  • source: The operand to be ORed with the destination operand (could be an immediate value, register, or memory).

  • destination: The register or memory location where the result of the OR operation is stored.

Example (x86):

In the example:

  • The first instruction applies a bitwise OR between the contents of registers EAX and EBX, storing the result in EBX.

  • The second instruction performs a bitwise OR on EAX with the immediate value 0x01, setting the least significant bit in EAX.

Key Points:

  • The or operation is typically used to set specific bits to 1 while leaving other bits unchanged.

  • As with and, the or instruction does not affect the carry flag or other flags unless explicitly used with conditional flags.

xor (Bitwise XOR)

The xor instruction performs a bitwise XOR (exclusive OR) operation. The result bit is 1 if the bits being compared are different (i.e., one is 0 and the other is 1). If both bits are the same (either both 0 or both 1), the result bit is 0. This operation is particularly useful for toggling specific bits in a register, as performing the same xor operation twice will return the original value.

Syntax:

  • source: The operand to be XORed with the destination operand (could be an immediate value, register, or memory).

  • destination: The register or memory location where the result of the XOR operation is stored.

Example (x86):

In this example:

  • The first instruction applies a bitwise XOR between the values in registers EAX and EBX, storing the result in EBX.

  • The second instruction XORs EAX with the immediate value 0xFF, effectively toggling the least significant byte in EAX.

Key Points:

  • The xor operation is often used in toggle operations or clearing a register (XORing a register with itself sets all bits to 0).

  • XOR is frequently used in checksum calculations and other algorithms requiring bitwise manipulations.

  • Similar to and and or, xor does not affect the carry flag but can affect other flags like zero, sign, and overflow.

not (Bitwise NOT)

The not instruction performs a bitwise NOT operation, also known as the ones' complement. It inverts all the bits of the operand, turning 1 bits into 0 and 0 bits into 1. This operation is used to negate a value, and it is often used in implementing certain logical manipulations, negation of conditions, or clearing values.

Syntax:

  • destination: The register or memory location where the result of the NOT operation is stored.

Example (x86):

In this example:

  • The not instruction inverts all the bits in the register EAX, turning 1 bits into 0 and 0 bits into 1.

Key Points:

  • The not operation is a simple and efficient way to invert the bits in a register.

  • It is often used in conjunction with other bitwise operations to manipulate flags or to create the two's complement of a number (by applying not followed by an add 1 operation).

Logical Operations Summary:

  • and: Performs a bitwise AND between two operands. The result bit is 1 if both bits are 1, otherwise it is 0. Commonly used to clear specific bits in a register or memory.

  • or: Performs a bitwise OR between two operands. The result bit is 1 if at least one of the bits is 1. Used to set specific bits in a register or memory.

  • xor: Performs a bitwise XOR between two operands. The result bit is 1 if the bits are different (i.e., one is 0 and the other is 1). It is often used to toggle specific bits or clear registers.

  • not: Performs a bitwise NOT (ones' complement) operation, inverting all bits in the operand. Commonly used for negation and clearing operations.

These logical instructions are fundamental for manipulating data at the bit level in low-level programming. They provide the means to create complex conditional logic, manage flags, and manipulate bits in registers or memory, all of which are vital to building efficient and effective low-level programs. Each of these instructions can be used in various contexts, such as bitmasking, toggling, flag setting, and clearing, or implementing more advanced algorithms.

Advertisements

Responsive Counter
General Counter
1000724
Daily Counter
2344