The Principle of Displacement in Assembly Language Jump Instructions

The Mechanism of Displacement in Jump Instructions

In 8086 assembly language, various jump instructions use relative displacement instead of absolute addresses to achieve jumps, providing great flexibility for the program’s memory layout.

Core Principle

The machine code of jump instructions contains the offset of the target address from the current instruction position, rather than the absolute value of the target address. This design allows the program to execute correctly from any position in memory without modifying the instruction code.

Displacement Calculation and Machine Code Analysis

Example Code Analysis

Consider the following assembly code:

mov cx, 6
mov ax, 10h
s: add ax, ax
loop s

The corresponding machine code is:

B9 06 00
B8 10 00
01 C0
E2 FC

Key Analysis

  1. Machine code of the loop instruction: <span>E2 FC</span>

  • <span>E2</span>: opcode of the loop instruction
  • <span>FC</span>: displacement (two’s complement representation of -4)
  • Displacement Calculation:

    • The loop instruction is assumed to be at address 0100h
    • The target label s is at address 00FCh
    • Displacement = target address – (current instruction address + instruction length)
    • Displacement = 00FCh – (0100h + 2) = -6 → two’s complement representation is FAh

    However, in this specific example:

    • The add ax, ax instruction (01 C0) occupies 2 bytes
    • The loop s instruction occupies 2 bytes
    • The displacement from the loop instruction to the next instruction is -4 (FCh)

    Detailed Mechanism of Displacement Calculation

    1. Short Jump

    ; Example 1: Short Jump
    org 100h
    
    start:
        jmp short target
        nop
        nop
    target:
        mov ax, 4C00h
        int 21h
    

    Machine Code Analysis:

    • jmp short instruction: EB xx (xx is the displacement)
    • Displacement calculation: target address – (jmp instruction address + 2)

    2. Near Jump

    ; Example 2: Near Jump
    org 100h
    
    start:
        jmp near ptr target
        ; There may be multiple instructions in between
        db 100 dup(90h) ; 100 NOP instructions
    target:
        mov ax, 4C00h
        int 21h
    

    Machine Code Analysis:

    • jmp near ptr instruction: E9 xxxx (xxxx is the 16-bit displacement)
    • Displacement calculation: target address – (jmp instruction address + 3)

    Example of Actual Memory Layout

    Example 1: Position-Independent Code

    ; Example 1: Position-independent loop code
    section .text
    org 0h ; No fixed starting address
    
    start:
        mov cx, 5
        mov ax, 0
    s:
        add ax, cx
        loop s ; Here using relative displacement
    
        ; Exit program
        mov ax, 4C00h
        int 21h
    

    Key Features:

    • No matter where this code is loaded in memory (0100h, 0200h, 0300h…)
    • The displacement value of the loop instruction remains unchanged (FCh)
    • The program can execute correctly

    Example 2: Multiple Jump Instructions

    ; Example 2: Multiple jump instructions using displacement
    org 100h
    
    main:
        mov cx, 3
    outer_loop:
        mov bx, 4
    inner_loop:
        ; Some processing code
        dec bx
        jnz inner_loop ; Conditional short jump
    
        dec cx
        jnz outer_loop ; Conditional short jump
    
        jmp finish ; Short jump
    
    finish:
        mov ax, 4C00h
        int 21h
    

    Mathematical Principles of Displacement Calculation

    Displacement Formula

    For most jump instructions:

    Displacement = target address - (current instruction address + instruction length)
    

    Two’s Complement Representation

    • Positive displacement: directly use binary representation
    • Negative displacement: use two’s complement representation
      • -1 → FFh (8 bits) or FFFFh (16 bits)
      • -4 → FCh (8 bits) or FFFCh (16 bits)

    Advanced Application: Self-Modifying Code

    Since jump instructions use relative displacement, we can create self-modifying code:

    ; Example: Self-modifying code
    org 100h
    
    start:
        mov byte ptr [jump_instr+1], 5 ; Modify jump displacement
    jump_instr:
        jmp short $+0 ; Initially jump to itself
    
        ; Normal code continues...
        mov ax, 4C00h
        int 21h
    

    Debugging Tips: Identifying Displacement Jump Instructions

    1. Identifying Short Jumps

    • Opcode: EB (jmp short), E0-E3 (loop/jcxz, etc.)
    • 1-byte displacement

    2. Identifying Near Jumps

    • Opcode: E9 (jmp near)
    • 2-byte displacement

    3. Manually Calculating Displacement

    When debugging, you can manually verify displacement calculations:

    Target address = current instruction address + instruction length + displacement value
    

    Comparison with Absolute Address Jumps

    Advantages of Relative Displacement Jumps:

    1. Position Independence: Code can run at any memory location
    2. Compact Code: Usually shorter than absolute address jumps
    3. Simple Relocation: No need to modify instructions during loading

    Disadvantages of Absolute Address Jumps:

    1. Position Dependency: Can only run at specific memory addresses
    2. Complex Modification: Requires relocation of instructions during loading
    3. Longer Code: Requires more bytes to store addresses

    Practical Programming Considerations

    1. Displacement Range Limitations:

      ; Error Example: Displacement exceeds range
      jmp short too_far
      ; Insert a lot of code in between
      times 200 db 90h ; 200 NOPs
      too_far:
          ; Here the displacement exceeds 127 bytes, assembly error
      
    2. Solution:

      ; Use near jump instead of short jump
      jmp near ptr too_far
      times 200 db 90h
      too_far:
          ; Correct: near jump has a larger range
      

    Conclusion

    The relative displacement jump mechanism in 8086 assembly language (jmp short/near, jcxz, loop, etc.) achieves:

    1. Memory Layout Flexibility: Programs can run at any memory location
    2. Code Relocation: No need to modify instructions to load at different addresses
    3. Efficiency Optimization: Relative displacement is usually more compact than absolute addresses

    This design is fundamental for writing efficient and flexible system software in assembly language and is a key concept for understanding low-level program execution principles. By mastering the principles of displacement calculation and two’s complement representation, developers can better understand program behavior in memory and write more robust assembly code.

    Leave a Comment