Tuesday, 4 March 2025

Logical instructions

Logical instructions:

Logical instructions perform bitwise operations on operands. These instructions operate on individual bits of the binary representation of data, typically in registers or memory locations. They are crucial for bit manipulation, masking, setting, clearing, and testing specific bits.

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

1. AND Instruction

The AND instruction performs a bitwise AND between two operands. The result is stored in the destination operand. Each bit in the result is 1 if the corresponding bits in both operands are 1, otherwise it is 0.

Syntax:

AND destination, source

Example:

MOV AL, 0Fh   ; AL = 0000 1111 (15 in decimal)

AND AL, 3Ch   ; 3C = 0011 1100 (60 in decimal)

               ; AL = AL AND 3C

               ; AL = 0000 1100 (12 in decimal)

Use Case:
Masking specific bits:

AND AX, 0FFh  ; Clears the upper 8 bits of AX (useful for isolating lower byte)

The AND operation follows these rules:

Bit 1

Bit 2

Result

0

0

0

0

1

0

1

0

0

1

1

1


Common Use Cases

1. Bit Masking

Masking specific bits by using the AND instruction with a mask value.


MOV AL, 0xAB ; AL = 1010 1011 AND AL, 0x0F ; Mask upper nibble -> AL = 0000 1011

2. Clearing Specific Bits

Clear particular bits while preserving others.


MOV AL, 0xFF ; AL = 1111 1111 AND AL, 0xF0 ; AL = AL AND 1111 0000 -> AL = 1111 0000

3. Checking Specific Bits

The AND instruction is useful for checking if specific bits are set.

MOV AL, 0x08 ; AL = 0000 1000 AND AL, 0x08 ; Check if bit 3 is set JZ BitNotSet ; Jump if Zero Flag is set (bit not set)

4. Isolating Specific Bits

Extract certain bits from a byte or word.

MOV AX, 0xF5A3 ; AX = 1111 0101 1010 0011 AND AX, 0x00FF ; Mask the upper byte ; AX = 0000 0000 1010 0011 (0x00A3)

Effect on Flags

  • Zero Flag (ZF): Set if the result is 0.
  • Sign Flag (SF): Set according to the sign of the result (MSB).
  • Parity Flag (PF): Set if the number of set bits in the result is even.
  • Overflow Flag (OF): Cleared to 0.
  • Carry Flag (CF): Cleared to 0.

2. OR Instruction

The OR instruction performs a bitwise OR between two operands. The result is 1 if at least one of the corresponding bits in either operand is 1.

Syntax:

OR destination, source

Example:

MOV AL, 0Fh   ; AL = 0000 1111

OR AL, 3Ch    ; AL = AL OR 0011 1100

               ; AL = 0011 1111 (63 in decimal)

Use Case:
Setting specific bits:

OR AX, 0x0080 ; Set the 7th bit of AX to 1

Common Use Cases

1. Setting Specific Bits

The OR instruction is widely used to set particular bits to 1 without altering other bits.


MOV AL, 0x0A ; AL = 0000 1010 OR AL, 0xF0 ; Set the upper nibble to 1s ; AL = 1111 1010 (0xFA)

2. Enabling Flags or Bits

For example, setting specific control bits in a hardware register:


MOV AX, 0x0040 ; AX = 0000 0000 0100 0000 OR AX, 0x0008 ; Set bit 3 ; AX = 0000 0000 0100 1000 (0x0048)

3. Combining Bit Patterns

MOV AL, 0x55 ; AL = 0101 0101 OR AL, 0x0A ; AL = AL OR 0000 1010 ; AL = 0101 1111 (0x5F)

4. Initializing Registers

A quick method to set a register to a specific pattern:


OR AX, AX ; Doesn't change AX but can set flags (e.g., to test if AX is zero)

Effect on Flags

  • Zero Flag (ZF): Set if the result is 0, cleared otherwise.
  • Sign Flag (SF): Set according to the sign of the result (MSB).
  • Parity Flag (PF): Set if the number of set bits is even.
  • Overflow Flag (OF): Always cleared (0).
  • Carry Flag (CF): Always cleared (0).

3. XOR Instruction

The XOR instruction performs a bitwise XOR (exclusive OR) operation. The result is 1 if the corresponding bits in the operands are different, otherwise 0.

Syntax:

XOR destination, source

Example:

MOV AL, 0Fh   ; AL = 0000 1111

XOR AL, 3Ch   ; AL = AL XOR 0011 1100

               ; AL = 0011 0011 (51 in decimal)

Special Use Case:
Clearing a register (efficiently sets a register to 0):

XOR AX, AX    ; AX = 0 (because x XOR x = 0)

Common Use Cases of XOR Instruction

1. Clearing a Register

The most common use of XOR is to clear a register efficiently. When an operand is XORed with itself, the result is always 0.

XOR AX, AX ; AX = 0 XOR EAX, EAX ; EAX = 0 (for 32-bit register)
  • Why Use XOR AX, AX Instead of MOV AX, 0?
    The XOR operation is shorter in terms of encoding size and may execute faster on certain processors.

2. Toggling Bits

The XOR instruction can be used to toggle specific bits. Toggling means changing 1 to 0 and 0 to 1.


MOV AL, 0x55 ; AL = 0101 0101 XOR AL, 0x0F ; AL = AL XOR 0000 1111 ; AL = 0101 1010 (0x5A)

3. Bitwise Inversion

The XOR instruction can invert specific bits using a mask.


MOV AL, 0x0C ; AL = 0000 1100 XOR AL, 0xFF ; AL = AL XOR 1111 1111 ; AL = 1111 0011 (0xF3)

4. Swapping Two Registers Without a Temporary Register

Using the XOR operation, you can swap the values of two registers without using a third register.


MOV AX, 0x1234 ; AX = 0x1234 MOV BX, 0x5678 ; BX = 0x5678 XOR AX, BX ; AX = AX XOR BX XOR BX, AX ; BX = BX XOR AX (BX = 0x1234) XOR AX, BX ; AX = AX XOR BX (AX = 0x5678)
  • Explanation:
    After these operations, the values in AX and BX are swapped.

5. Checking If Two Values Are Equal

The XOR instruction is useful for comparing two values. When XOR is used and the result is 0, the operands are equal.


MOV AX, 0x1234 MOV BX, 0x1234 XOR AX, BX ; AX = AX XOR BX (result is 0 if AX == BX) JZ EqualValues ; Jump if Zero Flag is set

Effect on Flags

  • Zero Flag (ZF): Set if the result is 0, cleared otherwise.
  • Sign Flag (SF): Set if the result is negative (only in signed operations).
  • Parity Flag (PF): Set if the number of set bits in the result is even.
  • Overflow Flag (OF): Always cleared (0).
  • Carry Flag (CF): Always cleared (0).

4. NOT Instruction

The NOT instruction performs a bitwise NOT operation (one's complement) on the destination operand. Each bit is inverted (0 becomes 1 and 1 becomes 0).

Syntax:

NOT destination

Example:

MOV AL, 0Fh   ; AL = 0000 1111

NOT AL        ; AL = 1111 0000 (240 in decimal)

Use Case:
Flipping all bits, often used for creating bit masks:

MOV AX, 0x0F0F

NOT AX        ; AX = 0xF0F0


5. TEST Instruction

The TEST instruction performs a bitwise AND between two operands, but it does not store the result. Instead, it updates the CPU flags based on the outcome. The ZF (Zero Flag) is set if the result is 0.

Syntax:

TEST destination, source

Example:

MOV AL, 0Fh   ; AL = 0000 1111

TEST AL, 08h  ; AL AND 0000 1000

               ; Result is 0000 1000 (non-zero)

JNZ BitIsSet  ; Jump if Zero Flag is not set (bit is set)

Use Case:
Checking specific bits without altering the operand:

TEST AX, 0x8000 ; Check if the most significant bit is set

JNZ HighBitSet


6. SHL/SHR (Logical Shift Left/Right)

These instructions shift the bits of the operand left (SHL) or right (SHR) by a specified number of positions. Zeros are shifted into the empty positions.

Syntax:

SHL destination, count

SHR destination, count

Example:

MOV AL, 1     ; AL = 0000 0001

SHL AL, 2     ; AL = 0000 0100 (Shift left by 2)

Use Case:
Multiplying and dividing by powers of 2:

SHL AX, 1     ; Multiply AX by 2

SHR AX, 1     ; Divide AX by 2


7. ROL/ROR (Rotate Left/Right)

The ROL and ROR instructions rotate the bits of the operand to the left or right. Bits shifted out from one end are reintroduced at the opposite end.

Syntax:

ROL destination, count

ROR destination, count

Example:

MOV AL, 0x81 ; AL = 1000 0001

ROL AL, 1    ; AL = 0000 0011 (Rotate left by 1 bit)

8. RCR Rotate through Carry Right 

The RCR (Rotate through Carry Right) instruction is an x86 assembly language instruction used to rotate the bits of a destination operand to the right through the carry flag. This means that the rightmost bit is shifted into the carry flag, and the carry flag's previous value is shifted into the leftmost bit of the operand.

Syntax:

RCR destination, count

  • destination: Can be a register or a memory location.
  • count: Specifies the number of bit positions to rotate (can be an immediate value, 1, or the CL register).

Operation:

  1. The operand is rotated to the right by the specified count.
  2. During each rotation, the rightmost bit is moved into the carry flag.
  3. The previous carry flag is moved into the leftmost bit of the operand.

Example:

mov al, 0b10010110 ; Load AL with 0x96 (binary: 10010110) stc ; Set the carry flag (CF = 1) rcr al, 1 ; Rotate AL through carry to the right by 1 bit

Before Rotation:

  • AL = 10010110
  • CF = 1

After Rotation:

  • AL = 11001011 (Previous CF shifted in from the left)
  • CF = 0 (Rightmost bit of AL moved to CF)

Use Cases:

  • Multi-bit shifts with carry flag management.
  • Bit-level manipulations, such as cryptography or checksum calculations.
9. RCL Rotate through Carry Left

The RCL (Rotate through Carry Left) instruction is an x86 assembly instruction used to rotate the bits of a destination operand to the left through the carry flag. In this operation, the leftmost bit is shifted into the carry flag, and the carry flag's previous value is shifted into the rightmost bit of the operand.

Syntax:

RCL destination, count
  • destination: Can be a register or a memory location.
  • count: Specifies the number of bit positions to rotate (can be an immediate value, 1, or the CL register).

Operation:

  1. The operand is rotated to the left by the specified count.
  2. During each rotation, the leftmost bit is moved into the carry flag.
  3. The previous carry flag is moved into the rightmost bit of the operand.

Example:

MOV AL, 0b10010110 ; Load AL with 0x96 (binary: 10010110) STC ; Set the carry flag (CF = 1) RXL AL, 1 ; Rotate AL through carry to the left by 1 bit

Before Rotation:

  • AL = 10010110
  • CF = 1

After Rotation:

  • AL = 00101101 (Previous CF shifted into the rightmost bit)
  • CF = 1 (The leftmost bit of AL moved to CF)

Use Cases:

  • Bitwise operations where the carry flag's state needs to be preserved.
  • Implementing circular shifts with an additional carry bit.
  • Useful in cryptography, arithmetic operations, or manipulating packed data.
Summary Table

Instruction

Operation

Common Use Cases

AND

Bitwise AND

Masking, clearing bits

OR

Bitwise OR

Setting specific bits

XOR

Bitwise XOR

Toggling bits, clearing registers (XOR AX, AX)

NOT

Bitwise NOT (invert)

Flipping all bits, bit manipulation

TEST

Bitwise AND (no store)

Testing specific bits (affects flags only)

SHL/SHR

Logical shift left/right

Multiplication/division by 2

ROL/ROR

Rotate bits left/right

Bit rotation (e.g., in cryptography)


Practical Example: Bit Masking

Objective: Extract the lower nibble (4 bits) of a byte.

MOV AL, 0xAB  ; AL = 1010 1011

AND AL, 0x0F  ; Mask upper nibble -> AL = 0000 1011


Conclusion

Logical instructions are the backbone of low-level bit manipulation in assembly language. They enable fine-grained control over data, which is critical for tasks like hardware control, embedded systems, and performance-critical applications.

 


No comments:

Post a Comment

Desktop Virtualisation

Desktop Virtualization ( DV ) Desktop Virtualization ( DV ) is a technique that creates an illusion of a desktop provided to the user. It d...