Assembly program demonstrating Bubble Sort on a 32-bit x86 integer array using NASM syntax, ideal for learning sorting and array manipulation.
section .data
array dd 34, 7, 23, 32, 5, 62, 78, 0, 12, 45 ; Array of 10 integers to sort
array_len equ 10 ; Length of the array
section .bss
; No uninitialized variables needed
section .text
global _start
_start:
mov ecx, array_len ; Number of elements in the array
dec ecx ; Number of passes = n-1
bubble_sort_outer_loop:
mov esi, 0 ; Index to traverse the array
mov ebx, 0 ; Swap flag (0 = no swap)
mov edx, ecx ; Number of comparisons to make in this pass
bubble_sort_inner_loop:
mov eax, [array + esi*4] ; Load current element
mov edi, [array + esi*4 + 4]; Load next element
cmp eax, edi ; Compare adjacent elements
jle no_swap ; If order correct, skip swap
; Swap elements
mov [array + esi*4], edi
mov [array + esi*4 + 4], eax
mov ebx, 1 ; Mark that a swap was made
no_swap:
inc esi ; Move to next element
dec edx ; Decrement comparison counter
jnz bubble_sort_inner_loop ; Continue inner loop if comparisons remain
cmp ebx, 0 ; Check if a swap was made
je end_sort ; If no swap, array is sorted
dec ecx ; Decrement passes
jnz bubble_sort_outer_loop ; Continue outer loop if passes remain
end_sort:
; Program termination (Linux system call)
mov eax, 1 ; sys_exit
xor ebx, ebx ; exit code 0
int 0x80Microcontroller/CPU: 32-bit x86 architecture (compatible with NASM and Linux)
Memory: 32-bit integer array allocated in data section
Arduino Pinout: Not applicable (code for x86 PC)
Software: NASM (Netwide Assembler) to assemble the code
Software: LD linker to create the executable
Configuration: Linux environment or emulator with x86 32-bit support
Dependencies: No external libraries, uses only Linux system calls to terminate the program