Bitwise Operations
Bitwise instructions treat a register as a row of independent bits. They are the tools for flags and masks, fast tests, character tricks and much of cryptography and compression.
The four logic instructions
| Instruction | Each result bit is 1 when… | Typical use |
|---|---|---|
and d, s |
both bits are 1 | clear bits, keep a field (mask) |
or d, s |
at least one bit is 1 | set bits |
xor d, s |
the bits differ | toggle bits, zero a register |
not d |
(one operand) the bit was 0 | invert all bits |
1100 1010 1100 1010 1100 1010
and 0000 1111 or 0000 1111 xor 0000 1111
= 0000 1010 = 1100 1111 = 1100 0101and, or and xor set ZF and SF from the result and clear CF and OF; not changes no flags.
Masks: set, clear, toggle, test
A mask is a constant with 1s in the positions you care about. To work with bit number n, the mask is 1 << n (NASM computes it):
READ equ 1 << 0 ; bit 0
WRITE equ 1 << 1 ; bit 1
EXEC equ 1 << 2 ; bit 2
section .text
global _start
_start:
xor ebx, ebx ; no permissions
or ebx, READ | WRITE ; set two bits
mov edi, ebx
call print_uint ; 3 = 0b011
or ebx, EXEC ; set bit 2
mov edi, ebx
call print_uint ; 7 = 0b111
and ebx, ~WRITE ; clear bit 1 (~ inverts the mask)
mov edi, ebx
call print_uint ; 5 = 0b101
xor ebx, READ ; toggle bit 0
mov edi, ebx
call print_uint ; 4 = 0b100
test ebx, EXEC ; is bit 2 set?
setnz al
movzx edi, al
call print_uint ; 1
mov eax, 60
xor edi, edi
syscall
; ---- print_uint: print rdi as an unsigned decimal number and a newline ----
print_uint:
mov rax, rdi ; rax = the number to convert
sub rsp, 32 ; room for up to 20 digits and a newline
lea rsi, [rsp + 31] ; fill the buffer from its end
mov byte [rsi], 10 ; the newline is the last character
mov ecx, 10
.next_digit:
xor edx, edx ; the dividend is rdx:rax, so clear rdx
div rcx ; rax = quotient, rdx = remainder (0..9)
add dl, '0' ; remainder -> ASCII digit
dec rsi
mov [rsi], dl ; store it in front of the previous one
test rax, rax
jnz .next_digit ; repeat until the quotient is 0
lea rdx, [rsp + 32]
sub rdx, rsi ; rdx = number of bytes to print
mov eax, 1 ; write(1, rsi, rdx)
mov edi, 1
syscall
add rsp, 32
ret3
7
5
4
1
| Goal | Instruction |
|---|---|
| set bit n | or reg, 1 << n |
| clear bit n | and reg, ~(1 << n) |
| toggle bit n | xor reg, 1 << n |
| is bit n set? | test reg, 1 << n then jnz/setnz |
| keep only the low 4 bits | and reg, 0xF |
Extracting fields
Several small values are often packed into one number. An RGB colour like 0xFF8800 holds red in bits 16–23, green in bits 8–15 and blue in bits 0–7. Shifting moves the field down, and cuts away the rest:
section .text
global _start
_start:
mov ebx, 0xFF8800 ; orange
mov edi, ebx
shr edi, 16 ; red field to the bottom
and edi, 0xFF
call print_uint
mov edi, ebx
shr edi, 8 ; green field
and edi, 0xFF
call print_uint
mov edi, ebx
and edi, 0xFF ; blue field: already at the bottom
call print_uint
mov eax, 60
xor edi, edi
syscall
; ---- print_uint: print rdi as an unsigned decimal number and a newline ----
print_uint:
mov rax, rdi ; rax = the number to convert
sub rsp, 32 ; room for up to 20 digits and a newline
lea rsi, [rsp + 31] ; fill the buffer from its end
mov byte [rsi], 10 ; the newline is the last character
mov ecx, 10
.next_digit:
xor edx, edx ; the dividend is rdx:rax, so clear rdx
div rcx ; rax = quotient, rdx = remainder (0..9)
add dl, '0' ; remainder -> ASCII digit
dec rsi
mov [rsi], dl ; store it in front of the previous one
test rax, rax
jnz .next_digit ; repeat until the quotient is 0
lea rdx, [rsp + 32]
sub rdx, rsi ; rdx = number of bytes to print
mov eax, 1 ; write(1, rsi, rdx)
mov edi, 1
syscall
add rsp, 32
ret255
136
0
A character trick
In ASCII, upper- and lowercase letters differ in exactly one bit: 'A' is 0x41 and 'a' is 0x61, a difference of 0x20 (bit 5). So or 0x20 makes a letter lowercase, and ~0x20 makes it uppercase and xor 0x20 swaps the case:
section .data
text: db "Hello, Assembly World"
text_len equ $ - text
newline: db 10
section .text
global _start
_start:
xor ecx, ecx
.next:
mov al, [text + rcx]
mov dl, al
or dl, 0x20 ; lowercase copy, only used for the test
cmp dl, 'a'
jb .skip ; not a letter
cmp dl, 'z'
ja .skip
xor al, 0x20 ; letter: swap its case
mov [text + rcx], al
.skip:
inc ecx
cmp ecx, text_len
jb .next
mov eax, 1
mov edi, 1
mov rsi, text
mov edx, text_len + 1 ; the text and the newline after it
syscall
mov eax, 60
xor edi, edi
syscallhELLO, aSSEMBLY wORLD
xor tricks
xor eax, eaxsets a register to zero (and is the preferred way to do so).xorwith the same key twice gives back the original:(x ^ k) ^ k = x. That is the basis of simple ciphers and of the "xor swap".
section .text
global _start
_start:
mov ebx, 2026 ; a secret value
mov r12d, 0x5A5A ; the key
xor ebx, r12d ; "encrypt"
mov edi, ebx
call print_uint
xor ebx, r12d ; "decrypt": back to 2026
mov edi, ebx
call print_uint
mov eax, 60
xor edi, edi
syscall
; ---- print_uint: print rdi as an unsigned decimal number and a newline ----
print_uint:
mov rax, rdi ; rax = the number to convert
sub rsp, 32 ; room for up to 20 digits and a newline
lea rsi, [rsp + 31] ; fill the buffer from its end
mov byte [rsi], 10 ; the newline is the last character
mov ecx, 10
.next_digit:
xor edx, edx ; the dividend is rdx:rax, so clear rdx
div rcx ; rax = quotient, rdx = remainder (0..9)
add dl, '0' ; remainder -> ASCII digit
dec rsi
mov [rsi], dl ; store it in front of the previous one
test rax, rax
jnz .next_digit ; repeat until the quotient is 0
lea rdx, [rsp + 32]
sub rdx, rsi ; rdx = number of bytes to print
mov eax, 1 ; write(1, rsi, rdx)
mov edi, 1
syscall
add rsp, 32
ret23984
2026
Note: A single-key xor is fine for checksums and puzzles, but it is not real encryption: knowing one plain value reveals the key.
Test yourself
Which instruction clears bit 3 of eax and leaves the others unchanged?
or eax, 8and eax, ~8xor eax, 8not eax
What is 0b1100 xor 0b1010?
- 0b1000
- 0b1110
- 0b0110
- 0b0010
How does test al, 1 help?
- It sets al to 1
- It sets ZF=0 if al is odd, without changing al
- It compares al with 1
- It clears bit 1