Home » Blog » instrumentation »

**Here’s an example program in 8051 microcontroller to sort an array from highest to lowest using the Bubble Sort algorithm:**

```
; Initialize variables
MOV DPTR, #0E00H ; point to the start of the array
MOV R0, #0 ; set loop counter to 0
Loop:
MOV R1, #0 ; set inner loop counter to 0
MOV A, R0
ADD A, #1 ; set the limit for the inner loop to N-1
MOV R2, A
InnerLoop:
MOVX @DPTR, A ; Load A with the current element
INC DPTR ; point to the next element
MOVX @DPTR, R2 ; Load R2 with the next element
DJNZ R1, InnerLoop ; repeat for all remaining elements
DJNZ R2, Swap ; repeat until all elements are sorted
DJNZ R0, Loop ; repeat for all elements
Exit:
RET ; end of program
Swap:
MOV A, R2 ; swap elements
MOV R2, @DPTR-1
MOVX @DPTR-1, A
DEC DPTR
RET
```

Explanation:

- Initialize the program by setting DPTR to point to the start of the array, and setting the loop counter R0 to 0.
- In the outer loop, set the inner loop counter R1 to 0, and set A to the current element at index R0. Add 1 to A to set the limit for the inner loop to N-1. Set R2 to A.
- In the inner loop, load A with the current element and load R2 with the next element. If A is greater than R2, swap the elements using the Swap subroutine.
- Repeat the inner loop until all remaining elements have been compared and sorted.
- Decrement R2 and repeat the outer loop until all elements have been sorted.
- Return from the program.

Swap subroutine:

- Move A to R2.
- Load R2 with the current element and store A in the current element.
- Decrement DPTR to point to the previous element.
- Return from the subroutine.

**Here’s an alternate way to implement the same program in 8051 microcontroller using the Selection Sort algorithm:**

```
; Initialize variables
MOV DPTR, #0E00H ; point to the start of the array
MOV R0, #0 ; set loop counter to 0
Loop:
MOV R1, R0 ; set the minimum index to the current index
MOV A, R0
ADD A, #1 ; set the limit for the inner loop to N-1
MOV R2, A
InnerLoop:
MOVX @DPTR, A ; Load A with the current element
INC DPTR ; point to the next element
MOVX @DPTR, R2 ; Load R2 with the next element
DJNZ R2, Compare ; repeat until all elements are compared
MOV A, R1 ; swap the current element with the minimum element
MOV R3, @DPTR-1
MOVX @DPTR-1, A
MOVX @DPTR, R3
DJNZ R0, Loop ; repeat for all remaining elements
Exit:
RET ; end of program
Compare:
MOVX A, @DPTR-1 ; compare the current element with the minimum element
CJMP ACC.1, Swap ; if current element is less than minimum element, update minimum index
DJNZ R1, ExitCompare ; repeat until all elements are compared
SJMP ExitCompare
Swap:
MOV A, R2 ; update minimum index
MOV R1, R2
ExitCompare:
DEC DPTR ; move DPTR back to the current element
RET
```

Explanation:

- Initialize the program by setting DPTR to point to the start of the array, and setting the loop counter R0 to 0.
- In the outer loop, set the minimum index R1 to the current index R0. Set A to the current index and add 1 to A to set the limit for the inner loop to N-1. Set R2 to A.
- In the inner loop, load A with the current element and load R2 with the next element. If R2 is not 0, compare the current element with the minimum element using the Compare subroutine. Otherwise, the minimum element has been found and swap the current element with the minimum element.
- Decrement R2 and repeat the inner loop until all elements are compared.
- Decrement R0 and repeat the outer loop until all elements have been sorted.
- Return from the program.

Compare subroutine:

- Load A with the current element.
- Compare the current element with the minimum element at index R1. If the current element is less than the minimum element, update the minimum index R1.
- Decrement DPTR to move back to the current element and return from the subroutine.
- Swap subroutine:
- Move A to R2 to update the minimum index R1.
- Load A with the current element and load R3 with the minimum element.
- Swap the current element with the minimum element using MOVX instructions.
- Decrement DPTR to move back to the current element and return from the subroutine.

**Here’s another way to implement the program to sort an array from highest to lowest using the Insertion Sort algorithm in 8051 microcontroller:**

```
; Initialize variables
MOV DPTR, #0E00H ; point to the start of the array
MOV R0, #1 ; set loop counter to 1
Loop:
MOV A, R0 ; set the current index
MOV R1, R0 ; set the previous index
InnerLoop:
MOV R2, A ; set the current element
DEC A ; move to the previous element
MOVX A, @DPTR ; load the previous element
CJNE R2, A, Compare ; compare the current and previous elements
SJMP ExitCompare
Compare:
MOV R3, A ; move the previous element to R3
MOV A, R2 ; move the current element to A
CJNE A, R3, Shift ; if the previous element is greater than the current element, shift the previous element to the current index
MOVX @DPTR, R3 ; otherwise, move the previous element to the current index
SJMP ExitCompare
Shift:
MOVX @DPTR, R3 ; move the previous element to the current index
DEC DPTR ; move the pointer back to the previous element
MOVX @DPTR, A ; move the current element to the previous index
DJNZ A, InnerLoop ; repeat for all previous elements
ExitCompare:
INC DPTR ; move the pointer to the next element
DJNZ R0, Loop ; repeat for all remaining elements
Exit:
RET ; end of program
```

Explanation:

- Initialize the program by setting DPTR to point to the start of the array, and setting the loop counter R0 to 1.
- In the outer loop, set A to the current index R0 and set R1 to the previous index R0. In the inner loop, set R2 to the current element at the current index and load the previous element at the previous index.
- Compare the current and previous elements using the Compare subroutine. If the elements are equal, exit the loop. Otherwise, if the previous element is greater than the current element, shift the previous element to the current index using the Shift subroutine. Otherwise, move the previous element to the current index.
- Increment DPTR and repeat the outer loop until all elements have been sorted.
- Return from the program.

Compare subroutine:

- Move the previous element to R3.
- Move the current element to A.
- If the previous element is greater than the current element, jump to the Shift subroutine.
- Otherwise, move the previous element to the current index and exit the loop.
- Shift subroutine:
- Move the previous element to the current index.
- Decrement DPTR to move back to the previous element.
- Move the current element to the previous index.
- Decrement A to move back to the previous element and repeat the inner loop.