Assembly - Logical Instructions
Assembly - Logical Instructions
2 OR OR operand1, operand2
The first operand in all the cases could be either in register or in memory. The
second operand could be either in register/memory or an immediate (constant)
value. However, memory-to-memory operations are not possible. These instructions
compare or match bits of the operands and set the CF, OF, PF, SF and ZF flags.
Operand1: 0101
Operand2: 0011
----------------------------
After AND -> Operand1: 0001
The AND operation can be used for clearing one or more bits. For example, say the
BL register contains 0011 1010. If you need to clear the high-order bits to zero, you
AND it with 0FH.
https://www.tutorialspoint.com/assembly_programming/assembly_logical_instructions.htm 1/6
6/15/24, 3:53 PM Assembly - Logical Instructions
Let's take up another example. If you want to check whether a given number is odd
or even, a simple test would be to check the least significant bit of the number. If
this is 1, the number is odd, else the number is even.
Example
evnn:
outprog:
https://www.tutorialspoint.com/assembly_programming/assembly_logical_instructions.htm 2/6
6/15/24, 3:53 PM Assembly - Logical Instructions
section .data
even_msg db 'Even Number!' ;message showing even number
len1 equ $ - even_msg
When the above code is compiled and executed, it produces the following result −
Even Number!
Odd Number!
Similarly to clear the entire register you can AND it with 00H.
The OR Instruction
The OR instruction is used for supporting logical expression by performing bitwise OR
operation. The bitwise OR operator returns 1, if the matching bits from either or both
operands are one. It returns 0, if both the bits are zero.
For example,
Operand1: 0101
Operand2: 0011
----------------------------
After OR -> Operand1: 0111
The OR operation can be used for setting one or more bits. For example, let us
assume the AL register contains 0011 1010, you need to set the four low-order bits,
you can OR it with a value 0000 1111, i.e., FH.
https://www.tutorialspoint.com/assembly_programming/assembly_logical_instructions.htm 3/6
6/15/24, 3:53 PM Assembly - Logical Instructions
Example
The following example demonstrates the OR instruction. Let us store the value 5 and
3 in the AL and the BL registers, respectively, then the instruction,
OR AL, BL
mov [result], al
mov eax, 4
mov ebx, 1
mov ecx, result
mov edx, 1
int 0x80
outprog:
mov eax,1 ;system call number (sys_exit)
int 0x80 ;call kernel
section .bss
result resb 1
When the above code is compiled and executed, it produces the following result −
https://www.tutorialspoint.com/assembly_programming/assembly_logical_instructions.htm 4/6
6/15/24, 3:53 PM Assembly - Logical Instructions
For example,
Operand1: 0101
Operand2: 0011
----------------------------
After XOR -> Operand1: 0110
XORing an operand with itself changes the operand to 0. This is used to clear a
register.
For example,
https://www.tutorialspoint.com/assembly_programming/assembly_logical_instructions.htm 5/6
6/15/24, 3:53 PM Assembly - Logical Instructions
https://www.tutorialspoint.com/assembly_programming/assembly_logical_instructions.htm 6/6