Overview of the JMP Instruction
The JMP instruction is an unconditional transfer instruction that can modify the IP (Instruction Pointer) or simultaneously modify both the CS (Code Segment Register) and IP, thereby changing the execution flow of the program. The JMP instruction requires two key pieces of information:
- The destination address for the transfer
- The distance of the transfer (intra-segment short transfer, intra-segment near transfer, inter-segment transfer)
Different Formats of the JMP Instruction
1. Intra-segment Short Transfer
; Example 1: Intra-segment Short Transfer
org 100h
start:
mov ax, 10
jmp short target ; Short transfer, jump to target label
; Intermediate code (not exceeding 127 bytes)
mov bx, 20
add ax, bx
target:
mov cx, ax
; Continue execution...
Note:
- Use
<span>jmp short</span>format - Transfer range: -128 to 127 bytes
- Instruction length: 2 bytes (opcode + 1 byte offset)
2. Intra-segment Near Transfer
; Example 2: Intra-segment Near Transfer
org 100h
start:
mov ax, 10
jmp near ptr target ; Near transfer, jump to target label
; Intermediate code (up to 64KB)
; There can be a lot of code...
; Simulate a lot of code
times 1000 db 90h ; 1000 NOP instructions
target:
mov cx, ax
; Continue execution...
Note:
- Use
<span>jmp near ptr</span>format - Transfer range: -32768 to 32767 bytes
- Instruction length: 3 bytes (opcode + 2 bytes offset)
3. Inter-segment Transfer
; Example 3: Inter-segment Transfer
assume cs:code1, cs:code2
code1 segment
start:
mov ax, 10
jmp far ptr target ; Inter-segment transfer, jump to another segment
; Code here will not be executed
mov bx, 20
code1 ends
code2 segment
assume cs:code2
target:
mov cx, ax
; Continue execution in code2 segment
mov ax, 4C00h
int 21h
code2 ends
end start
Note:
- Use
<span>jmp far ptr</span>format - Simultaneously modifies CS and IP
- Instruction length: 5 bytes (opcode + 2 bytes segment address + 2 bytes offset)
Different Addressing Modes of the JMP Instruction
1. Direct Addressing
; Example 4: Direct Addressing
org 100h
start:
mov ax, 10
jmp target ; Directly jump to label address
; Intermediate code...
target:
mov bx, ax
; Continue execution...
2. Register Indirect Addressing
; Example 5: Register Indirect Addressing
org 100h
start:
mov ax, offset target ; Store the target address offset in ax
jmp ax ; Jump to the address specified by ax
; Intermediate code...
target:
mov bx, ax
; Continue execution...
3. Memory Indirect Addressing
; Example 6: Memory Indirect Addressing
org 100h
start:
mov word ptr [jump_addr], offset target ; Store the target address in memory
jmp word ptr [jump_addr] ; Read the address from memory and jump
; Intermediate code...
target:
mov bx, ax
; Continue execution...
jump_addr dw ? ; Used to store the jump address
Using the JMP Instruction with Conditional Transfer Instructions
; Example 7: Using JMP with Conditional Transfers
org 100h
start:
mov ax, 10
mov bx, 20
cmp ax, bx
jg greater ; If ax > bx, jump to greater
jl less ; If ax < bx, jump to less
; If equal, continue execution
mov cx, 0
jmp done ; Skip other branches
greater:
mov cx, 1
jmp done
less:
mov cx, 2
; No need for jmp, continue directly to done
done:
; All branches converge here
; Continue execution...
Practical Application Example: Loop Control
; Example 8: Using JMP to Implement a Loop
org 100h
start:
mov cx, 5 ; Number of loops
mov ax, 0 ; Accumulator
loop_start:
add ax, cx ; Accumulate
dec cx ; Decrement counter
jnz loop_start ; If cx ≠ 0, continue looping
; Loop ends, ax = 5+4+3+2+1 = 15
mov ax, 4C00h
int 21h
Machine Code Representation of the JMP Instruction
Understanding the machine code of the JMP instruction helps to gain deeper insights into its operation:
- Short Transfer: EB xx (xx is an 8-bit signed offset)
- Near Transfer: E9 xxxx (xxxx is a 16-bit signed offset)
- Far Transfer: EA xxxx xxxx (first the offset, then the segment address)
Considerations
- When using short transfer, ensure the target address is within -128 to 127 bytes
- Inter-segment transfers will change the CS register, be mindful of segment boundaries and permissions
- For indirect jumps, ensure that the register or memory contains a valid address
- In protected mode, jumps may also involve permission checks and protection mechanisms
Conclusion
The JMP instruction is a fundamental instruction in assembly language for controlling program flow, providing various transfer methods:
- Intra-segment short transfer: Suitable for short-distance jumps, compact code
- Intra-segment near transfer: Suitable for jumps to any position within the same segment
- Inter-segment transfer: Allows jumps to different code segments
By flexibly using the different formats and addressing modes of the JMP instruction, complex program control structures such as conditional branches, loops, and subroutine calls can be implemented. Understanding the workings of the JMP instruction is crucial for mastering assembly language programming.