Bitwise Instruction in Assembly

Bitwise instructions in assembly language allow manipulation of individual bits within registers or memory. These instructions operate at the level of binary representation of data and are fundamental for various low-level programming tasks such as data encoding, bit masking, and bitwise arithmetic.

Here are some common bitwise instructions in x86 assembly language:

1 AND (Logical AND):

  • Performs a bitwise AND operation between two operands.
  • Syntax: AND dest, src
  • Example: AND AX, BX performs a bitwise AND operation between the contents of AX and BX, storing the result in AX.
Input AInput BOutput (A & B)
000
010
100
111
AND AX, BX   ; AX = AX & BX

This instruction performs a bitwise AND operation between the contents of AX and BX registers, storing the result in AX.

2 OR (Logical OR):

  • Performs a bitwise OR operation between two operands.
  • Syntax: OR dest, src
  • Example: OR AX, BX performs a bitwise OR operation between the contents of AX and BX, storing the result in AX.
Input AInput BOutput (A | B)
000
011
101
111
OR AX, BX    ; AX = AX | BX

This instruction performs a bitwise OR operation between the contents of AX and BX registers, storing the result in AX.

3 XOR (Logical XOR):

  • Performs a bitwise exclusive OR (XOR) operation between two operands.
  • Syntax: XOR dest, src
  • Example: XOR AX, BX performs a bitwise XOR operation between the contents of AX and BX, storing the result in AX.
Input AInput BOutput (A ^ B)
000
011
101
110
XOR AX, BX   ; AX = AX ^ BX

This instruction performs a bitwise XOR (exclusive OR) operation between the contents of AX and BX registers, storing the result in AX.

4 NOT (Logical NOT):

  • Performs a bitwise NOT operation on a single operand, inverting all its bits.
  • Syntax: NOT dest
  • Example: NOT AX performs a bitwise NOT operation on the contents of AX, inverting all its bits.
Input AOutput (~A)
01
10
NOT AX       ; AX = ~AX

This instruction performs a bitwise NOT (complement) operation on the contents of the AX register, inverting all its bits.

5 TEST (Logical AND and update flags):

  • Performs a bitwise AND operation between two operands and updates the flags without storing the result.
  • Syntax: TEST dest, src
  • Example: TEST AX, BX performs a bitwise AND operation between the contents of AX and BX, updating the flags without storing the result.
TEST AX, BX  ; Update flags based on AX & BX

This instruction performs a bitwise AND operation between the contents of AX and BX registers, updating the flags without storing the result.

6 SHL/SHR (Shift Left/Right):

  • Shifts the bits of an operand left (SHL) or right (SHR) by a specified number of positions, with zeros shifted in from the other side.
  • Syntax: SHL dest, count or SHR dest, count
  • Example: SHL AX, 1 shifts the bits of AX one position to the left.

shl Syntax:

shl destination, count
  • destination: The register or memory location whose bits are to be shifted left.
  • count: The number of positions to shift the bits.

Effect:

  • Each bit in the destination operand is shifted left by the specified count.
  • Zeros are shifted into the rightmost positions.
  • The leftmost bit shifted out is moved into the carry flag (CF).
SHL AX, 1    ; Shift AX left by 1 bit

This instruction shifts the bits of AX register one position to the left, with zeros shifted in from the right.

  • The leftmost bit (bit 31) is shifted into the carry flag, and a zero is shifted into the rightmost bit (bit 0).

Detailed Explanation:

shl ah, 1

  • Before shl ah, 1:
    • AH: b7 b6 b5 b4 b3 b2 b1 b0 (8 bits)
    • CF: The current state of the carry flag (could be 0 or 1).
  • After shl ah, 1:
    • AH: b6 b5 b4 b3 b2 b1 b0 0
    • CF: The value of bit b7 before the shift.

Example:

Let's use an example to illustrate:

Initial State

  • AH = 0010110 (binary) = 0x2D (hex)
  • CF = 0 (initially, it doesn't matter for this operation)

After shl ah, 1:

  • Original AH: 00101101
  • After Shift: 01011010
  • New CF: 0 (Original bit 7 of AH)

Impact on the Carry Flag

The carry flag (CF) is updated to the value of the bit was shifted out from the the position.

shr syntax:

shr destination, count
  • destination: The register or memory location whose bits are to be shifted right.
  • count: The number of positions to shift the bits.

Effect:

  • Each bit in the destination operand is shifted right by the specified count.
  • Zeros are shifted into the leftmost positions.
  • The rightmost bit shifted out is moved into the carry flag (CF).
SHR AX, 1    ; Shift AX right by 1 bit

This instruction shifts the bits of AX register one position to the right, with zeros shifted in from the left.

  • The rightmost bit (bit 0) is shifted into the carry flag, and a zero is shifted into the leftmost bit (bit 31).

Detailed Flag Impact

Carry Flag (CF):

  • shl: Set to the bit that is shifted out from the leftmost bit.
  • shr: Set to the bit that is shifted out from the rightmost bit.

Zero Flag (ZF):

  • Set if the result of the operation is zero; otherwise, it is cleared.

Sign Flag (SF):

  • Set or cleared based on the most significant bit (MSB) of the result. For shl, this depends on the new MSB after the shift. For shr, this only matters if the operand is signed.

Overflow Flag (OF):

  • For shl and shr, when the count is 1:
    • shl: Set if the MSB changes as a result of the shift.
    • shr: Set if the MSB was initially 1 (indicating a change in sign for signed numbers).

7 SAL/SAR (Arithmetic Shift Left/Right):

  • Similar to SHL/SHR but preserves the sign bit during shifts, making them suitable for signed integers.
  • Syntax: SAL dest, count or SAR dest, count.
SAL AX, 1    ; Arithmetic shift AX left by 1 bit

Similar to SHL, this instruction shifts the bits of AX register one position to the left, but preserves the sign bit during shifts.

SAR AX, 1    ; Arithmetic shift AX right by 1 bit

Similar to SHR, this instruction shifts the bits of AX register one position to the right, but preserves the sign bit during shifts.

8 ROL/ROR (Rotate Left/Right):

  • Circularly shifts the bits of an operand left (ROL) or right (ROR) by a specified number of positions, with bits shifted out on one side re-entering from the other side.
  • Syntax: ROL dest, count or ROR dest, count
ROL AX, 1    ; Rotate AX left by 1 bit

This instruction circularly shifts the bits of AX register one position to the left, with bits shifted out on the left re-entering from the right.

ROR AX, 1    ; Rotate AX right by 1 bit

This instruction circularly shifts the bits of AX register one position to the right, with bits shifted out on the right re-entering from the left.

rol (Rotate Left)

Syntax:

rol destination, count
  • destination: The register or memory location whose bits are to be rotated left.
  • count: The number of positions to rotate the bits.

Effect:

  • Each bit in the destination operand is rotated left by the specified count.
  • The leftmost bits that are shifted out are rotated back into the rightmost positions.

Flags Affected:

  • Carry Flag (CF): Set to the value of the last bit rotated out of the leftmost position.
  • Overflow Flag (OF): Undefined if count is not 1. If count is 1, it is set to the XOR of the two most significant bits of the result.

Example:

mov al, 0b00101101  ; AL = 00101101 (0x2D)
rol al, 1           ; AL = 01011010 (0x5A), CF = 0
  • Before rol al, 1:AL = 00101101, CF = undefined
  • After rol al, 1:AL = 01011010, CF = 0 (bit 7 was 0)

ror (Rotate Right)

Syntax:

ror destination, count
  • destination: The register or memory location whose bits are to be rotated right.
  • count: The number of positions to rotate the bits.

Effect:

  • Each bit in the destination operand is rotated right by the specified count.
  • The rightmost bits that are shifted out are rotated back into the leftmost positions.

Flags Affected:

  • Carry Flag (CF): Set to the value of the last bit rotated out of the rightmost position.
  • Overflow Flag (OF): Undefined if count is not 1. If count is 1, it is set to the XOR of the two most significant bits of the result.

Example:

mov al, 0b00101101  ; AL = 00101101 (0x2D)
ror al, 1           ; AL = 10010110 (0x96), CF = 1
  • Before ror al, 1:AL = 00101101, CF = undefined
  • After ror al, 1:AL = 10010110, CF = 1 (bit 0 was 1)

Detailed Flag Impact

Carry Flag (CF):

  • rol: Set to the bit that is rotated out from the leftmost bit.
  • ror: Set to the bit that is rotated out from the rightmost bit.

Overflow Flag (OF):

  • For rol and ror, when the count is 1:
    • rol: Set to the XOR of the two most significant bits of the result.
    • ror: Set to the XOR of the two most significant bits of the result.

Other Flags:

  • The zero flag (ZF), sign flag (SF), auxiliary carry flag (AF), and parity flag (PF) are not affected by rol and ror.