Assembly program that sorts an array of integers using Bubble Sort algorithm

Assembly program demonstrating Bubble Sort on a 32-bit x86 integer array using NASM syntax, ideal for learning sorting and array manipulation.

Brief Introduction

Assembly program that sorts an array of integers using Bubble Sort algorithm (assembly)
DESCRIPTION:
This assembly program sorts an array of integers using the Bubble Sort algorithm. The array is defined in memory and the code swaps adjacent elements if they are out of order, repeating the process until the array is sorted. The code is written for 32-bit x86 architecture, using NASM syntax and can be run on Linux systems.

ADDITIONAL NOTES:
The code is written to be simple and educational, with comments in Italian to facilitate understanding. The Bubble Sort algorithm is not the most efficient for large datasets but is ideal to demonstrate sorting concepts and array manipulation in assembly.

EXPLANATIONS:
- The array is defined in the .data section and contains 10 unordered integer elements.
- The ECX register is used as a counter for the number of passes of the algorithm.
- The ESI register points to the start of the array to iterate through elements.
- The inner loop compares pairs of adjacent elements and swaps them if necessary.
- The outer loop repeats the process until the array is sorted.
- At the end, the program terminates with a Linux system call (exit).

Example Code: assembly

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 0x80
×
×
×
Do you want to regenerate the answer?
×
Export chat
Choose export format
Generazione PDF in corso…
Allegati
×
You are about to close the chat and switch to the image generator. If you are not logged in, you will lose our chat. Do you confirm?
You are viewing a shared chat in temporary mode. It will not be saved.
×
Saved prompts
×
Private note
×
Label
×
Search all chats
×
Your insights
Analyzing…
×
AI Preferences
Assistant name 30
Answer length
AI model
Structured answers
Include examples
Use emojis
Guide me, don't solve Hints and questions instead of a ready answer
More settings →
×
Share this chat
Anyone opening this link can view the chat or add it to their profile as their own chat.
Note: the chat attachments will be shared too. Anyone who adds it will receive a copy of the files in their own folder.
Shared chat
Someone shared a chat with you. Do you want to just view it or add it to your chats?
Note: the chat attachments will be shared too. Anyone who adds it will receive a copy of the files in their own folder.
×

Saved messages

Loading...

×

Chat History

example-programs · CHAT HISTORY

Loading...

AI Preferences

×
  • BasicQuick and essential answers for study
  • MediumHigher quality for study and programming
  • AdvancedComplex reasoning and detailed analysis
Explain Steps

What do you need?

Microcontroller/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

Similar Topics