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.
2. Clearing Specific Bits
Clear particular bits while preserving others.
3. Checking Specific Bits
The AND instruction is useful for checking if specific bits are set.
4. Isolating Specific Bits
Extract certain bits from a byte or word.
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.
2. Enabling Flags or Bits
For example, setting specific control bits in a hardware register:
3. Combining Bit Patterns
4. Initializing Registers
A quick method to set a register to a specific pattern:
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.
- Why Use
XOR AX, AXInstead ofMOV AX, 0?
TheXORoperation 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.
3. Bitwise Inversion
The XOR instruction can invert specific bits using a mask.
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.
- Explanation:
After these operations, the values inAXandBXare 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.
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 theCLregister).
Operation:
- The operand is rotated to the right by the specified
count. - During each rotation, the rightmost bit is moved into the carry flag.
- The previous carry flag is moved into the leftmost bit of the operand.
Example:
Before Rotation:
AL = 10010110CF = 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.
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:
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 theCLregister).
Operation:
- The operand is rotated to the left by the specified
count. - During each rotation, the leftmost bit is moved into the carry flag.
- The previous carry flag is moved into the rightmost bit of the operand.
Example:
Before Rotation:
AL = 10010110CF = 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.
|
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