Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

Implementing Bubble Sort in ARM Assembly Language

Tech Nov 24 15

Problem Description

Memory address 0x40008000 stores 20 unsigned 8-bit integers. Write an ARM assembly program to sort these data items in ascending order using the bubble sort algorithm.

Note: During verification, store specific test data at the designated memory address, perform the sorting operation, and then examine the sorted results using the memory viewer in the development environment.

Implementation Approach

Step 1: Define Memory Address and Data

Use EQU to assign a label to the target memory adddress. Use DCB to define a sequence of 8-bit data values. (Note: DCW defines 16-bit data, DCD defines 32-bit data)

Step 2: Load Data into Memory

Initialize the data transfer process:

  • Load the destination address into a register
  • Load the source data address into another register
  • Set up a counter register

Execute the data loading loop:

  • Use LDRB to read a byte from the source and increment the source pointer
  • Use STRB to write the byte to the destination and increment the destination pointer
  • Increment the counter
  • Compare counter with 19; if less, continue the loop

Step 3: Bubble Sort Algorithm

Implement nested loops for bubble sort:

  • Outer loop: iterates through all elements
  • Inner loop: compares adjacent elements and swaps if necessary

Key instruction explanations:

  • STRB/LDRB: Store/Load 8-bit data
  • BLS: Branch if less than or same
  • STRLSB: Store least significant byte after comparison
  • BNE: Branch if not equal
  • MOV: Move data between registers
  • LDR: Load data into register

Assembly Code

SRAM_ADDR      EQU     0x40008000

                AREA    SortDemo,CODE,READONLY
                ENTRY
                CODE32

START           MOV     r6,#0
                LDR     r7,=SRAM_ADDR
                LDR     r8,=SOURCE_DATA
                B       COPY_LOOP

SOURCE_DATA     DCB 100,95,90,85,80,75,70,65,60,55,50,45,40,35,30,25,20,15,10,5

COPY_LOOP       LDRB    r9,[r8],#1
                STRB    r9,[r7],#1
                ADD     r6,r6,#1
                CMP     r6,#19
                BLS     COPY_LOOP

                MOV     r6,#0
OUTER_LOOP
                LDR     r7,=SRAM_ADDR
                ADD     r8,r7,#1
                MOV     r10,#0

INNER_LOOP      
                LDRB    r9,[r7]
                LDRB    r11,[r8]
                CMP     r11,r9
                STRLSB  r9,[r8]
                STRLSB  r11,[r7]
                ADD     r7,r7,#1
                ADD     r8,r8,#1
                ADD     r10,r10,#1
                CMP     r10,#19
                BNE     INNER_LOOP

                ADD     r6,r6,#1
                CMP     r6,#19
                BNE     OUTER_LOOP

                MOV     r7,#6
                END

Code Explanation

The program uses the following register allocation:

  • r6: Loop counter
  • r7: Current element address
  • r8: Next element address
  • r9: First element value
  • r10: Inner loop counter
  • r11: Second element value

The bubble sort works by repeatedly stepping through the list, comparing adjacent elements and swapping them if they are in the wrong order. After each pass through the list, the largest unsorted element "bubbles up" to its correct position.

Modification for Descending Order

To sort in descending order instead of ascending order, simply swap the positions of r9 and r11 in the comparison logic. This changes the comparison condition so that elements are swapped when the left element is smaller than the right element, effectively reversing the sort order.

Summary

This experiment demonstrates bubble sort implementation using nested loops in ARM assembly. The algorithm compares adjacent elements throughout the array and swaps them if they are in the wrong order. To change from ascending to descending order, only one modification is needed—changing which elements trigger a swap operation. The key advantage of this approach is its simplicity, though it has O(n²) time complexity which makes it less efficient for large datasets.

Tags: arm

Related Articles

Understanding Strong and Weak References in Java

Strong References Strong reference are the most prevalent type of object referencing in Java. When an object has a strong reference pointing to it, the garbage collector will not reclaim its memory. F...

Comprehensive Guide to SSTI Explained with Payload Bypass Techniques

Introduction Server-Side Template Injection (SSTI) is a vulnerability in web applications where user input is improper handled within the template engine and executed on the server. This exploit can r...

Implement Image Upload Functionality for Django Integrated TinyMCE Editor

Django’s Admin panel is highly user-friendly, and pairing it with TinyMCE, an effective rich text editor, simplifies content management significantly. Combining the two is particular useful for bloggi...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.