Sorting an Array in Descending Order using 8086 Assembly Language Programming (ALP)

8086 Assembly Language Programming (ALP) to solve a common problem: Sorting an Array using 8086 ALP of 10 bytes in descending order. Buckle up, and let’s explore the intricacies of this assembly language program.

Problem Statement

Imagine you have an array of ten bytes, and you want to arrange them in descending order. How do you do it using 8086 assembly language? Well, fear not! We’ve got the solution for you.

The Assembly Language Program

Let’s break down the assembly language program step by step.

section .data
    array   db  9, 3, 7, 1, 5, 2, 8, 4, 6, 0  ; The array to be sorted
    n       equ 10  ; Number of elements in the array
section .text
    global _start
_start:
    ; Display the original array
    mov cx, n        ; Initialize loop counter with the number of elements
    mov si, 0        ; Initialize source index to 0
display_array:
    mov dl, [array + si]  ; Load the element at array[si] into DL register
    add dl, '0'           ; Convert the numeric value to ASCII
    mov ah, 02h           ; Print character function
    int 21h               ; Invoke the interrupt to print the character
    ; Print a comma and space for clarity
    mov dl, ','
    int 21h
    inc si              ; Move to the next element in the array
    loop display_array ; Repeat until all elements are displayed
    ; Move to the next line
    mov ah, 02h   ; Print newline character function
    mov dl, 0Ah
    int 21h
    ; Sort the array in descending order
    mov cx, n  ; Initialize outer loop counter with the number of elements
outer_loop:
    mov si, 0  ; Initialize inner loop source index to 0
inner_loop:
    mov al, [array + si]      ; Load the current element into AL register
    cmp al, [array + si + 1]  ; Compare with the next element
    jge no_swap                ; Jump if greater than or equal, no swap needed
    ; Swap elements if needed
    mov ah, [array + si]       ; Load the next element into AH register
    mov [array + si], [array + si + 1]  ; Swap the elements
    mov [array + si + 1], ah
no_swap:
    inc si            ; Move to the next element in the array
    loop inner_loop   ; Repeat inner loop until all elements are compared
    dec cx            ; Decrement the outer loop counter
    jnz outer_loop    ; Repeat outer loop until all elements are sorted
    ; Display the sorted array
    mov cx, n
    mov si, 0
display_sorted_array:
    mov dl, [array + si]
    add dl, '0'   ; Convert to ASCII
    mov ah, 02h   ; Print character function
    int 21h
    ; Print a comma and space for clarity
    mov dl, ','
    int 21h
    inc si
    loop display_sorted_array
    ; Exit the program
    mov ah, 4Ch
    int 21h

Explanation:

  1. Initialization:
    • array: The array to be sorted.
    • n: Number of elements in the array.
  2. Display Original Array:
    • Loop through the array, converting each element to ASCII and printing it.
  3. Sorting in Descending Order:
    • Implementing a simple Bubble Sort algorithm:
      • Outer loop (outer_loop) iterates through each element.
      • Inner loop (inner_loop) compares adjacent elements and swaps them if needed.
  4. Display Sorted Array:
    • Similar to the display of the original array, this section prints the sorted elements.
  5. Exit the Program:
    • int 21h with function 4Ch is used to terminate the program.

Conclusion

And there you have it! A simple 8086 assembly language program to sort an array of bytes in descending order. Understanding the low-level operations involved in sorting algorithms can deepen your appreciation for the inner workings of computers.

Add a Comment

Your email address will not be published. Required fields are marked *