3 ways for Sorting Arrays in 8051 Microcontroller

Home » Blog » instrumentation » 3 ways for Sorting Arrays in 8051 Microcontroller

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:

  1. Initialize the program by setting DPTR to point to the start of the array, and setting the loop counter R0 to 0.
  2. 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.
  3. 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.
  4. Repeat the inner loop until all remaining elements have been compared and sorted.
  5. Decrement R2 and repeat the outer loop until all elements have been sorted.
  6. Return from the program.

Swap subroutine:

  1. Move A to R2.
  2. Load R2 with the current element and store A in the current element.
  3. Decrement DPTR to point to the previous element.
  4. 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:

  1. Initialize the program by setting DPTR to point to the start of the array, and setting the loop counter R0 to 0.
  2. 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.
  3. 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.
  4. Decrement R2 and repeat the inner loop until all elements are compared.
  5. Decrement R0 and repeat the outer loop until all elements have been sorted.
  6. Return from the program.

Compare subroutine:

  1. Load A with the current element.
  2. 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.
  3. Decrement DPTR to move back to the current element and return from the subroutine.
  4. Swap subroutine:
  5. Move A to R2 to update the minimum index R1.
  6. Load A with the current element and load R3 with the minimum element.
  7. Swap the current element with the minimum element using MOVX instructions.
  8. 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:

  1. Initialize the program by setting DPTR to point to the start of the array, and setting the loop counter R0 to 1.
  2. 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.
  3. 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.
  4. Increment DPTR and repeat the outer loop until all elements have been sorted.
  5. Return from the program.

Compare subroutine:

  1. Move the previous element to R3.
  2. Move the current element to A.
  3. If the previous element is greater than the current element, jump to the Shift subroutine.
  4. Otherwise, move the previous element to the current index and exit the loop.
  5. Shift subroutine:
  6. Move the previous element to the current index.
  7. Decrement DPTR to move back to the previous element.
  8. Move the current element to the previous index.
  9. Decrement A to move back to the previous element and repeat the inner loop.

Leave a Comment

Your email address will not be published. Required fields are marked *