Assembly Language - Logical Instructions
Logical instructions are used for bit-level operations in assembly programming, and are widely used for masking, bit extraction, flag setting, and efficient multiply/divide operations.
AND - bitwise AND
AND Performs a bitwise "AND" operation on the two operands (1 only when both are 1).
Example
; File path: and_demo.asm
; AND instruction example
section
.text
global
_start
_start
:
; Basic bitwise AND
mov
eax
,
0x0F0F
; 0000 1111 0000 1111
and
eax
,
0x00FF
; 0000 0000 1111 1111
; Result: eax = 0x000F ; 0000 0000 0000 1111
; Common trick 1: mask off the low 4 bits (keep the low 4 bits)
mov
eax
,
0xAB
; 1010 1011
and
eax
,
0x0F
; 0000 1111
; eax = 0x0B ; 0000 1011
; Common trick 2: determine parity (AND with 1)
mov
eax
,
42
; Even
and
eax
,
1
; eax = 0 (even)
; The binary form of 42 ends in 0, 42 & 1 = 0
mov
eax
,
43
; Odd
and
eax
,
1
; eax = 1 (odd)
; The binary form of 43 ends in 1, 43 & 1 = 1
; Common trick 3: clear a register to zero
xor
eax
,
eax
; Equivalent to mov eax, 0, but faster and shorter
; AND sets the flags: CF=0, OF=0, ZF and SF depend on the result
mov
eax
,
1
mov
ebx
,
0
int
0x80
OR - bitwise OR
OR Performs a bitwise "OR" operation on the two operands (1 if either is 1).
Example
; OR instruction example
; Combine flag bits
mov
eax
,
0x0F00
; 0000 1111 0000 0000
or
eax
,
0x00FF
; 0000 0000 1111 1111
; eax = 0x0FFF ; 0000 1111 1111 1111
; Set a bit (set bit 3 to 1)
mov
eax
,
0
; 0000 0000
or
eax
,
0x08
; 0000 1000
; eax = 0x8 ; 0000 1000
; Case conversion: uppercase to lowercase
mov
al
,
'A'
; al = 0x41 (0100 0001)
or
al
,
0x20
; 0x20 = 0010 0000
; al = 0x61 = 'a' ; 0110 0001
NOT - bitwise NOT
NOT Inverts every bit of the operand (0 becomes 1, 1 becomes 0).
Example
; NOT instruction example
mov
eax
,
0x0F0F0F0F
; 0000 1111 0000 1111 ...
not
eax
; 1111 0000 1111 0000 ...
; eax = 0xF0F0F0F0
; NOT does not affect any flags (unlike AND/OR/XOR)
XOR - bitwise XOR
XOR Performs a bitwise "XOR" operation on the two operands (1 if different, 0 if the same).
Example
; File path: xor_demo.asm
; XOR instruction example
section
.text
global
_start
_start
:
; Basic XOR
mov
eax
,
0x0F0F
; 0000 1111 0000 1111
xor
eax
,
0x00FF
; 0000 0000 1111 1111
; Result: eax = 0x0FF0 ; 0000 1111 1111 0000
; Classic trick 1: clear a register to zero (more efficient than mov reg, 0)
xor
eax
,
eax
; eax = 0, taking only 2 bytes
xor
ebx
,
ebx
xor
ecx
,
ecx
; Classic trick 2: swap two registers (without a third temporary register)
mov
eax
,
100
; eax = 100
mov
ebx
,
200
; ebx = 200
xor
eax
,
ebx
; eax = 100 xor 200
xor
ebx
,
eax
; ebx = 200 xor (100 xor 200) = 100
xor
eax
,
ebx
; eax = (100 xor 200) xor 100 = 200
; Now eax = 200, ebx = 100 (swap complete!)
; Classic trick 3: simple encryption/decryption
mov
al
,
'A'
; Original character 'A' = 0x41
xor
al
,
0x55
; Encrypt: al = 'A' xor 0x55
; al now holds some garbled value
xor
al
,
0x55
; Decrypt: xor 0x55 again to restore
; al = 'A' is back!
mov
eax
,
1
mov
ebx
,
0
int
0x80
Although the XOR swap trick is cool, on modern CPUs it is less efficient than using
XCHGan instruction or a temporary register. Just understand it; there is no need to insist on using it.
Shift instructions
Shift instructions move binary bits left or right, and are often used for efficient multiply/divide operations (multiply by 2, divide by 2, etc.).
| Instruction | Function | Example |
|---|---|---|
| SHL | Logical left shift (bits leave on the left, zeros fill on the right) | shl eax, 1(multiply by 2) |
| SHR | Logical right shift (bits leave on the right, zeros fill on the left) | shr eax, 1(unsigned divide by 2) |
| SAL | Arithmetic left shift (same as SHL) | sal eax, 1 |
| SAR | Arithmetic right shift (bits leave on the right, the sign bit is kept on the left) | sar eax, 1(signed divide by 2) |
Example
; File path: shift_demo.asm
; Shift instruction example
section
.text
global
_start
_start
:
; SHL: logical left shift = multiply by a power of 2
mov
eax
,
10
; eax = 10 (1010)
shl
eax
,
1
; eax = 20 (10100), i.e. 10×2
shl
eax
,
2
; eax = 80 (1010000), i.e. 20×4
; SHR: logical right shift = unsigned divide by a power of 2
mov
eax
,
80
; eax = 80
shr
eax
,
3
; eax = 10, i.e. 80÷8
; SAR: arithmetic right shift = signed divide by a power of 2 (preserving the sign bit)
mov
eax
,
16
; eax = -16 (0xFFFFFFF0)
sar
eax
,
2
; eax = -4 (0xFFFFFFFC), i.e. -16÷4
; SAR versus SHR:
; If eax = 0xFFFFFFF0 (-16), SHR would yield 0x3FFFFFFC (a very large positive number)
; while SAR would yield 0xFFFFFFFC (-4), preserving the sign bit
; The last bit shifted out goes into the CF flag
mov
eax
,
5
; 0101
shr
eax
,
1
; eax = 2, CF = 1 (the trailing 1 was shifted out)
jc
carry_was_set
; If CF=1, the original number was odd
carry_was_set
:
mov
eax
,
1
mov
ebx
,
0
int
0x80
Rotate instructions
Rotates feed the shifted-out bits back into the other end, forming a cycle:
| Instruction | Function |
|---|---|
| ROL | Rotate left (bypassing CF) |
| ROR | Rotate right (bypassing CF) |
| RCL | Rotate left through carry (via CF) |
| RCR | Rotate right through carry (via CF) |
Example
; Rotate instruction example
; ROL: rotate left
mov
al
,
0x85
; 1000 0101
rol
al
,
1
; Shift left by 1 bit: 0000 1011
; CF = 1 (the highest bit was shifted out into CF)
; At the same time, CF's original 1 is shifted into the lowest bit, forming a cycle
; Actual effect of ROL: shift left by 1 bit, with the highest bit entering both CF and the lowest bit
; 1000 0101 -> ROL 1 -> 0000 1011
; RCL: rotate left through carry (CF participates in the cycle)
clc
; Clear CF = 0
mov
al
,
0x85
; 1000 0101
rcl
al
,
1
; CF participates: CF bit7...bit0 -> CF
; Result: 0000 1010, CF = 1
; Rotations are commonly used in encryption algorithms and bit operations
Practical uses of logical operations
A comprehensive example -- using logical operations to implement a simple bit-flag system:
Example
; File path: bit_flags.asm
; Implement a bit-flag system using logical operations
section
.data
flags
db
0
; 8 flag bits, all initially 0
; bit0: whether active
; bit1: whether visible
; bit2: whether a save is needed
; bit3: whether modified
FLAG_ACTIVE
equ
1
; 0000 0001
FLAG_VISIBLE
equ
2
; 0000 0010
FLAG_NEED_SAVE
equ
4
; 0000 0100
FLAG_MODIFIED
equ
8
; 0000 1000
section
.text
global
_start
_start
:
; Set a flag bit: OR
mov
al
,
[
flags
]
or
al
,
FLAG_ACTIVE
; Set bit0
or
al
,
FLAG_VISIBLE
; Set bit1
; al = 0000 0011 = 3
mov
[
flags
]
,
al
; Check a flag bit: AND + TEST
test
byte
[
flags
]
,
FLAG_ACTIVE
; Test whether bit0 is 1
jnz
is_active
; ZF=0 means the bit is 1
is_active
:
; Clear a flag bit: AND + NOT
mov
al
,
[
flags
]
and
al
,
~FLAG_VISIBLE
; Clear bit1 (leaving the other bits unchanged)
; al = 0000 0001 = 1
mov
[
flags
]
,
al
; Toggle a flag bit: XOR
mov
al
,
[
flags
]
xor
al
,
FLAG_MODIFIED
; Toggle bit3
; If it was 0 it becomes 1; if it was 1 it becomes 0
mov
eax
,
1
mov
ebx
,
0
int
0x80
Bit-flag operations are extremely common in systems programming. OS kernels, device drivers, and embedded systems widely use this technique to manage state.