Sorting in Assembly Language

Our program will demonstrate sorting in Assembly Language. Here’s the implementation:

section .data
    strs db 'c', 'a', 'b', 0
    strs_len equ $ - strs - 1
    ints db 7, 2, 4
    ints_len equ $ - ints
    fmt_str db 'Strings: %s', 10, 0
    fmt_int db 'Ints:    ', 0
    fmt_int_elem db '%d ', 0
    fmt_newline db 10, 0
    fmt_sorted db 'Sorted:  %s', 10, 0

section .text
    global _start
    extern printf

_start:
    ; Sort strings
    mov ecx, strs_len
    dec ecx
    .outer_loop_str:
        mov esi, 0
        .inner_loop_str:
            mov al, [strs + esi]
            mov bl, [strs + esi + 1]
            cmp al, bl
            jle .skip_swap_str
            mov [strs + esi], bl
            mov [strs + esi + 1], al
        .skip_swap_str:
            inc esi
            cmp esi, ecx
            jl .inner_loop_str
        loop .outer_loop_str

    ; Print sorted strings
    push strs
    push fmt_str
    call printf
    add esp, 8

    ; Sort integers
    mov ecx, ints_len
    dec ecx
    .outer_loop_int:
        mov esi, 0
        .inner_loop_int:
            mov al, [ints + esi]
            mov bl, [ints + esi + 1]
            cmp al, bl
            jle .skip_swap_int
            mov [ints + esi], bl
            mov [ints + esi + 1], al
        .skip_swap_int:
            inc esi
            cmp esi, ecx
            jl .inner_loop_int
        loop .outer_loop_int

    ; Print sorted integers
    push fmt_int
    call printf
    add esp, 4

    mov ecx, ints_len
    mov esi, 0
    .print_loop_int:
        xor eax, eax
        mov al, [ints + esi]
        push eax
        push fmt_int_elem
        call printf
        add esp, 8
        inc esi
        loop .print_loop_int

    push fmt_newline
    call printf
    add esp, 4

    ; Check if sorted
    mov ecx, ints_len
    dec ecx
    mov esi, 0
    mov edx, 1  ; Assume sorted
    .check_loop:
        mov al, [ints + esi]
        mov bl, [ints + esi + 1]
        cmp al, bl
        jle .still_sorted
        mov edx, 0  ; Not sorted
        jmp .end_check
    .still_sorted:
        inc esi
        loop .check_loop
    .end_check:

    ; Print sorted status
    push edx
    push fmt_sorted
    call printf
    add esp, 8

    ; Exit
    mov eax, 1
    xor ebx, ebx
    int 0x80

This Assembly Language program demonstrates sorting for both strings and integers. It uses a simple bubble sort algorithm for both types.

First, we sort a string array containing ‘c’, ‘a’, and ‘b’. Then, we sort an integer array containing 7, 2, and 4. After sorting, we print the results.

The program also checks if the integer array is sorted after the sorting operation and prints the result.

To run this program, you would typically need to assemble it into an object file, link it, and then execute the resulting binary. The exact commands may vary depending on your system and assembler, but it might look something like this:

$ nasm -f elf sorting.asm
$ ld -m elf_i386 -o sorting sorting.o -lc
$ ./sorting
Strings: abc
Ints:    2 4 7 
Sorted:  1

Note that Assembly Language doesn’t have built-in sorting functions like higher-level languages. We have to implement the sorting algorithm manually. This example uses a simple bubble sort, which isn’t the most efficient for large datasets but works well for this demonstration.

Also, checking if a slice is sorted is done manually by iterating through the array and comparing adjacent elements. This is equivalent to the IsSorted function in higher-level languages.

Remember that Assembly Language programs are highly dependent on the specific architecture and operating system. This example is written for x86 assembly on a Linux-like system.