Unconditional Jump Instruction: JMP 

The JMP instruction is the only unconditional flow control instruction
It unconditionally transfers control to another point in the program
The location to be transferred to is known as the target address
The jump can be direct or indirect
If the jump is indirect, the target address can be either a memory operand or a general purpose register

The JMP statement can take any one of the following forms:

Format of Unconditional JMP Instruction
JMP label	;Direct jump
JMP reg		;Register Indirect
JMP mem		;Memory Indirect

In its simplest form, the JMP statement is similar to the GOTO statement in high level languages.

	GOTO statement 	=	JMP statement

It has the following syntax:

	
	JMP [operator] destination

where operator can be:

SHORT: the target address is encoded with one byte and it is in the same segment
NEAR PTR: the target address is encoded with two bytes and it is in the same segment
FAR PTR: the target address is encoded with four bytes and it is in a different segment

Relative Address

A jump is called a forward jump if the target address is larger than the address of the jump instruction. However, it is called a backward jump if the target address is less than or equal the address of the jump instruction. The next example illustrates both types of jumps.

Example: Forward and backward jump instructions.
		. . .
		MOV CX, 10
		JMP initdone 	;Forward jump
  initCX20:	MOV CX,20
  initdone:	MOV AX,CX
  repeat1:	DEC CX
		. . .
		JMP repeat1	;Backward jump
		. . .

In 16-bit addressing, 16-bits are required to store the offset address i.e. 2 bytes. However, analyzing most of the jumps occurring in a program, it can be observed that they are within a close distance to the jump instruction. Thus, to reduce the code size of the jump instruction we can encode the target address with one byte instead of two bytes. This can be achieved by storing the difference target - IP in the instruction instead of storing the actual target address. The difference can fit in a byte if it is within the range -128 to +127. When the CPU executes the jump instruction, it will get the difference target-IP from the instruction and will add to it IP, i.e., target-IP+IP=target, and will obtain the required target address. Jump instructions with the difference target-IP fitting in one byte are called short jumps.

A jump is called a near jump if the target address is in the same code segment at any location ranging from -32,768 to +32,767 bytes from the IP.
When the jump address is within the same segment, the jump is called intra-‎segment jump. If the jump address is outside the current segment, the jump is referred to ‎as inter-segment jump.

The jump is a far jump if the target address is in a different code segment. In this case, the assembler stores the code segment and the offset of the target address within that segment in the instruction.

The table below summarizes all the addressing modes used with the unconditional jump instruction.

Label ‎Pointer Range Addressing ‎Mode Size Encoded as Directive
Short ‎+127/-128 bytes‎ Immediate Word Relative SHORT
Near Intra-segment Immediate Word Relative Near Ptr
Near Intra-segment Register Word Absolute address Near Ptr
Near Intra-segment Memory Word Absolute address Near Ptr
Far Inter-segment Immediate Double Word Absolute address Far Ptr
Far Inter-segment Memory Double Word Absolute address Far Ptr

The assembler can automatically select the SHORT jump and allocate one byte, if appropriate. So, we do not need to explicitly specify SHORT or NEAR PTR for addresses in the same segment as the assembler will select the appropriate type.

The next example demonstrates the encodings of short, near, and far jumps.

Example: Encodings of short, near, and far jumps.
 0005  33 C0			         XOR AX, AX
 0007  40			Back:    INC AX
 0008  EB 10			         JMP Forward
 000A  B9 000A			         MOV CX, 10
 000D  E9 000A			         JMP Near PTR Forward
 0010  B9 0014			         MOV cx, 20
 0013  EA ---- 001A R		         JMP Far PTR Forward
 0018  8B C1			         MOV AX, CX
 001A  03 C0			Forward: ADD AX, AX
 001C  EB E9			         JMP Back        

As can be seen from the example, for both forward and backward jumps that can be encoded as short will be allocated one byte by the assembler. For example, the address of the label Forward is 001A. The IP address after the instruction JMP Forward has the value 000A. Thus, the difference 001A-000A=0010. Since the difference can fit in a byte, 10 is what is stored in the instruction.
However, for the instruction JMP Near PTR Forward, the IP has a value 0010. So, the difference is 001A-0010=000A. Although the difference can fit in one byte, two bytes are allocated since we explicitly specified that the target is Near.
For the instruction JMP FAR PTR Forward, four bytes are allocated, two for the segment and two for the offset. Here, the absolute address is stored not the relative address. So, the offset 001A is stored. The two bytes for the segment will be assigned once the program is loaded and the segment is determined.
For the instruction, JMP Back, the address of the label Back is 0007 and the value of IP is 001E. So, the difference is 0007-001E=0007+FFE2=FFE9. Since the difference can fit in a byte, one byte is allocated and the value E9 is stored.