Jump
Jump
Jump
SN Conditional Instructions
1
Unconditional jump
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.
2
Conditional jump
Let us discuss the CMP instruction before discussing the conditional instructions.
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 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 is often used for comparing whether a counter value has reached the number of times 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
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
Following are the conditional jump instructions used on unsigned data used for logical operations
The following conditional jump instructions have special uses and check the value of flags
JC Jump If Carry CF
JO Jump If Overflow OF
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 _start ;must be declared for using gcc
check_third_num:
_exit:
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
segment .bss
largest resb 2
When the above code is compiled and executed, it produces the following result