Overview of Transfer Instructions in Assembly Language

Overview of Transfer Instructions

In 8086 assembly language, transfer instructions are a key mechanism for controlling the execution flow of a program. They can modify the Instruction Pointer (IP) or simultaneously modify both the Code Segment (CS) register and the Instruction Pointer (IP), thereby changing the location of code execution by the CPU.

Classification of Transfer Instructions

1. Intra-segment Transfer

Only modifies the IP register without changing the CS value.

  • Short Transfer: The modification range for IP is -128 to 127 bytes.
  • Near Transfer: The modification range for IP is -32768 to 32767 bytes.

2. Inter-segment Transfer

Simultaneously modifies the CS and IP registers, allowing jumps to any memory location.

Unconditional Transfer Instruction (JMP)

The JMP instruction is the most basic transfer instruction, which unconditionally alters the program execution flow.

Intra-segment Short Transfer

; Example 1: Intra-segment Short Transfer
org 100h

start:
    mov ax, 5
    jmp short skip_data  ; Short transfer, jump to skip_data label

data_area:
    db 10, 20, 30, 40

skip_data:
    mov bx, ax
    ; More code...

Intra-segment Near Transfer

; Example 2: Intra-segment Near Transfer
org 100h

start:
    mov cx, 100
    jmp near ptr process_loop  ; Near transfer

initialize:
    ; Initialization code...

process_loop:
    dec cx
    jnz process_loop  ; Conditional transfer
    jmp finish

finish:
    mov ax, 4C00h
    int 21h

Inter-segment Transfer

; Example 3: Inter-segment Transfer
assume cs:code, ds:data

data segment
    message db 'Hello, World!$'
data ends

code segment
start:
    mov ax, data
    mov ds, ax

    ; Display string
    mov dx, offset message
    mov ah, 09h
    int 21h

    ; Inter-segment transfer to another code segment
    jmp far ptr other_segment

    mov ax, 4C00h
    int 21h
code ends

other_code segment
    assume cs:other_code

other_segment:
    ; Code in another segment
    mov ax, 4C00h
    int 21h

other_code ends
end start

Conditional Transfer Instructions

Conditional transfer instructions determine whether to transfer based on the status of the flag register.

; Example 4: Conditional Transfer
org 100h

compare_values:
    mov ax, 10
    mov bx, 20
    cmp ax, bx      ; Compare ax and bx

    jg greater      ; Jump if ax > bx
    jl less         ; Jump if ax < bx
    je equal        ; Jump if ax = bx

greater:
    mov cx, 1
    jmp done

less:
    mov cx, 2
    jmp done

equal:
    mov cx, 3

done:
    ; Continue execution...

Loop Instruction (LOOP)

; Example 5: Loop Instruction
org 100h

start:
    mov cx, 5       ; Set loop count
    mov ax, 0       ; Initialize accumulator

sum_loop:
    add ax, cx      ; Accumulate
    loop sum_loop   ; Decrement cx, loop if cx ≠ 0

    ; Now ax = 5+4+3+2+1 = 15

    mov ax, 4C00h
    int 21h

Procedure Call and Return

; Example 6: Procedure Call
org 100h

main:
    mov ax, 5
    mov bx, 3

    call multiply   ; Call multiplication procedure

    ; Now ax = 5 * 3 = 15

    mov ax, 4C00h
    int 21h

; Multiplication procedure
multiply proc
    push bx         ; Save register
    push cx

    mov cx, ax      ; Save multiplicand
    mov ax, 0       ; Clear ax

mult_loop:
    add ax, cx      ; Accumulate
    dec bx          ; Decrement multiplier
    jnz mult_loop   ; Continue if multiplier ≠ 0

    pop cx          ; Restore register
    pop bx
    ret             ; Return to main program
multiply endp

Interrupt Instructions

; Example 7: Interrupt Call
org 100h

start:
    ; Display character 'A'
    mov ah, 02h     ; DOS display character function
    mov dl, 'A'     ; Character to display
    int 21h         ; Call DOS interrupt

    ; Program termination
    mov ax, 4C00h
    int 21h

Summary of Transfer Instruction Principles

  1. IP Modification Mechanism: Transfer instructions change the address of the next instruction to be executed by modifying the IP register.
  2. Relative Offset Calculation: Short and near transfers use relative offsets, making the code position-independent.
  3. Flag Dependency: Conditional transfer instructions depend on the status of the flag register to determine whether to jump.
  4. Stack Operations: CALL and RET instructions use the stack to save and restore return addresses.
  5. Interrupt Handling: The INT instruction finds the handler address through the interrupt vector table.

Notes

  1. The range of short transfers is limited (-128 to 127); exceeding this range will cause assembly errors.
  2. Inter-segment transfers require explicit specification of the target segment address and offset address.
  3. When using CALL and RET instructions, ensure stack operations are balanced.
  4. Conditional transfer instructions typically follow CMP or TEST instructions.

By understanding and mastering these transfer instructions, you can effectively control program flow, implement conditional judgments, loops, and subroutine calls, among other structured programming functionalities.

Leave a Comment