CHAPTER
11
Logical Instructions
T he processor instruction set provides the instructions AND, OR, XOR, TEST and NOT Boolean logic,
which tests, sets and clears the bits according to the need of the program.
The format for these instructions:
SN 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
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.
The AND Instruction
The AND instruction is used for supporting logical expressions by performing bitwise AND operation. The bitwise
AND operation returns 1, if the matching bits from both the operands are 1, otherwise it returns 0. For example:
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.
AND BL, 0FH ; This sets BL to 0000 1010
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.
Assuming the number is in AL register, we can write:
TUTORIALS POINT
Simply Easy Learning
AND AL, 01H ; ANDing with 0000 0001
JZ EVEN_NUMBER
The following program illustrates this:
Example:
section .text
global main ;must be declared for using gcc
main: ;tell linker entry point
mov ax, 8h ;getting 8 in the ax
and ax, 1 ;and ax with 1
jz evnn
mov eax, 4 ;system call number (sys_write)
mov ebx, 1 ;file descriptor (stdout)
mov ecx, odd_msg ;message to write
mov edx, len2 ;length of message
int 0x80 ;call kernel
jmp outprog
evnn:
mov ah, 09h
mov eax, 4 ;system call number (sys_write)
mov ebx, 1 ;file descriptor (stdout)
mov ecx, even_msg ;message to write
mov edx, len1 ;length of message
int 0x80 ;call kernel
outprog:
mov eax,1 ;system call number (sys_exit)
int 0x80 ;call kernel
section .data
even_msg db 'Even Number!' ;message showing even number
len1 equ $ - even_msg
odd_msg db 'Odd Number!' ;message showing odd number
len2 equ $ - odd_msg
When the above code is compiled and executed, it produces following result:
Even Number!
Change the value in the ax register with an odd digit, like:
mov ax, 9h ; getting 9 in the ax
The program would display:
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
----------------------------
TUTORIALS POINT
Simply Easy Learning
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.
OR BL, 0FH ; This sets BL to 0011 1111
Example:
The following example demonstrates the OR instruction. Let us store the value 5 and 3 in the AL and the BL register
respectively. Then the instruction,
OR AL, BL
should store 7 in the AL register:
section .text
global main ;must be declared for using gcc
main: ;tell linker entry point
mov al, 5 ;getting 5 in the al
mov bl, 3 ;getting 3 in the bl
or al, bl ;or al and bl registers, result should be 7
add al, byte '0' ;converting decimal to ascii
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 following result:
The XOR Instruction
The XOR instruction implements the bitwise XOR operation. The XOR operation sets the resultant bit to 1, if and
only if the bits from the operands are different. If the bits from the operands are same (both 0 or both 1), the
resultant bit is cleared to 0.
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.
XOR EAX, EAX
TUTORIALS POINT
Simply Easy Learning
The TEST Instruction
The TEST instruction works same as the AND operation, but unlike AND instruction, it does not change the first
operand. So, if we need to check whether a number in a register is even or odd, we can also do this using the
TEST instruction without changing the original number.
TEST AL, 01H
JZ EVEN_NUMBER
The NOT Instruction
The NOT instruction implements the bitwise NOT operation. NOT operation reverses the bits in an operand. The
operand could be either in a register or in the memory.
For example,
Operand1: 0101 0011
After NOT -> Operand1: 1010 1100
TUTORIALS POINT
Simply Easy Learning
CHAPTER
12
Assembly Conditions
C onditional execution in assembly language is accomplished by several looping and branching
instructions. These instructions can change the flow of control in a program. Conditional execution is observed in
two scenarios:
SN Conditional Instructions
Unconditional jump
This is performed by the JMP instruction. Conditional execution often involves a transfer of control to the
1
address of an instruction that does not follow the currently executing instruction. Transfer of control may be
forward to execute a new set of instructions, or backward to re-execute the same steps.
Conditional jump
This is performed by a set of jump instructions j<condition> depending upon the condition. The conditional
2
instructions transfer the control by breaking the sequential flow and they do it by changing the offset value in
IP.
Let us discuss the CMP instruction before discussing the conditional instructions.
The CMP Instruction
The CMP instruction compares two operands. It is generally used in conditional execution. This instruction
basically subtracts one operand from the other for comparing whether the operands are equal or not. It does not
disturb the destination or source operands. It is used along with the conditional jump instruction for decision
making.
SYNTAX
CMP destination, source
CMP compares two numeric data fields. The destination operand could be either in register or in memory. The
source operand could be a constant (immediate) data, register or memory.
EXAMPLE:
CMP DX, 00 ; Compare the DX value with zero
JE L7 ; If yes, then jump to label L7
.
.
TUTORIALS POINT
Simply Easy Learning
L7: ...
CMP is often used for comparing whether a counter value has reached the number of time a loop needs to be
run. Consider the following typical condition:
INC EDX
CMP EDX, 10 ; Compares whether the counter has reached 10
JLE LP1 ; If it is less than or equal to 10, then jump to LP1
Unconditional Jump
As mentioned earlier this is performed by the JMP instruction. Conditional execution often involves a transfer of
control to the address of an instruction that does not follow the currently executing instruction. Transfer of control
may be forward to execute a new set of instructions, or backward to re-execute the same steps.
SYNTAX:
The JMP instruction provides a label name where the flow of control is transferred immediately. The syntax of the
JMP instruction is:
JMP label
EXAMPLE:
The following code snippet illustrates the JMP instruction:
MOV AX, 00 ; Initializing AX to 0
MOV BX, 00 ; Initializing BX to 0
MOV CX, 01 ; Initializing CX to 1
L20:
ADD AX, 01 ; Increment AX
ADD BX, AX ; Add AX to BX
SHL CX, 1 ; shift left CX, this in turn doubles the CX value
JMP L20 ; repeats the statements
Conditional Jump
If some specified condition is satisfied in conditional jump, the control flow is transferred to a target instruction.
There are numerous conditional jump instructions, depending upon the condition and data.
Following are the conditional jump instructions used on signed data used for arithmetic operations:
Instruction Description Flags tested
JE/JZ Jump Equal or Jump Zero ZF
JNE/JNZ Jump not Equal or Jump Not Zero ZF
JG/JNLE Jump Greater or Jump Not Less/Equal OF, SF, ZF
JGE/JNL Jump Greater or Jump Not Less OF, SF
JL/JNGE Jump Less or Jump Not Greater/Equal OF, SF
JLE/JNG Jump Less/Equal or Jump Not Greater OF, SF, ZF
Following are the conditional jump instructions used on unsigned data used for logical operations:
Instruction Description Flags tested
TUTORIALS POINT
Simply Easy Learning
JE/JZ Jump Equal or Jump Zero ZF
JNE/JNZ Jump not Equal or Jump Not Zero ZF
JA/JNBE Jump Above or Jump Not Below/Equal CF, ZF
JAE/JNB Jump Above/Equal or Jump Not Below CF
JB/JNAE Jump Below or Jump Not Above/Equal CF
JBE/JNA Jump Below/Equal or Jump Not Above AF, CF
The following conditional jump instructions have special uses and check the value of flags:
Instruction Description Flags tested
JXCZ Jump if CX is Zero none
JC Jump If Carry CF
JNC Jump If No Carry CF
JO Jump If Overflow OF
JNO Jump If No Overflow OF
JP/JPE Jump Parity or Jump Parity Even PF
JNP/JPO Jump No Parity or Jump Parity Odd PF
JS Jump Sign (negative value) SF
JNS Jump No Sign (positive value) SF
The syntax for the J<condition> set of instructions:
Example,
CMP AL, BL
JE EQUAL
CMP AL, BH
JE EQUAL
CMP AL, CL
JE EQUAL
NON_EQUAL: ...
EQUAL: ...
Example:
The following program displays the largest of three variables. The variables are double-digit variables. The three
variables num1, num2 and num3 have values 47, 72 and 31 respectively:
section .text
global main ;must be declared for using gcc
main: ;tell linker entry point
mov ecx, [num1]
cmp ecx, [num2]
jg check_third_num
mov ecx, [num3]
check_third_num:
cmp ecx, [num3]
TUTORIALS POINT
Simply Easy Learning
jg _exit
mov ecx, [num3]
_exit:
mov [largest], word ecx
mov ecx,msg
mov edx, len
mov ebx,1 ;file descriptor (stdout)
mov eax,4 ;system call number (sys_write)
int 0x80 ;call kernel
nwln
mov ecx,largest
mov edx, 2
mov ebx,1 ;file descriptor (stdout)
mov eax,4 ;system call number (sys_write)
int 0x80 ;call kernel
mov eax, 1
int 80h
section .data
msg db "The largest digit is: ", 0xA,0xD
len equ $- msg
num1 dd '47'
num2 dd '22'
num3 dd '31'
segment .bss
largest resb 2
When the above code is compiled and executed, it produces following result:
The largest digit is:
47
TUTORIALS POINT
Simply Easy Learning