Implementing Bubble Sort in ARM Assembly Language
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
LDRBto read a byte from the source and increment the source pointer - Use
STRBto 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 dataBLS: Branch if less than or sameSTRLSB: Store least significant byte after comparisonBNE: Branch if not equalMOV: Move data between registersLDR: 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 counterr7: Current element addressr8: Next element addressr9: First element valuer10: Inner loop counterr11: 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.