8051 Program to Arrange Numbers in Descending Order


Write an assembly language program for microcontroller 8051 to arrange numbers in descending order from an array of 10 numbers.

Assume array of the ten bytes is stored in external memory of 8051 microcontroller from memory location 4000H.


  • To arrange the numbers in descending order, first compare two numbers.
  • If first number < second number, then interchange these numbers.
  • Then compare second number and third number, interchange if needed.
  • Continue same process for remaining numbers in the array.
  • We have to repeat process until wee get arranged data in descending order.
  • For this process, two counters are required.
    • Byte counter, for accessing numbers from array.
    • Pass counter for repeating the comparison process.


Step 1: Initialize a counter for comparison (Pass counter).

Step2: Initialize memory pointer to read number from the array.

Step3: Initialize byte counter.

Step 4: Read numbers from array.

Step 5: Compare two numbers.

Step 6: If number greater than or equal to next number, then go to step 8.

Step 7: Replace number with next number which is smaller.

Step 8: Increment memory pointer to read next number in the array.

Step 9: Decrement byte counter by 1.

Step 10: If byte counter is not equal to zero then go to step 4.

Step 11: Decrement pass counter by 1.

Step 12: If pass counter is not equal to zero then go to step 2.

Step 13: Stop.


Flowchart for 8051 Program to Arrange Numbers in Descending Order


MOV R0, #0AH                        ; Initialize pass counter.

REP1: MOV DPTR, #4000H      ; Initialize memory pointer

MOV R1, #0AH                        ; Initialize byte counter

REPEAT: MOV R2, DPL            ; Save the lower byte address

MOVX A, @DPTR                    ; Read the number from array

MOV 0F0H, A                          ; Store the number in register B

INC DPTR                                 ; Increment memory pointer

MOVX A, @DPTR                    ; Take the next number from array

CJNE A, 0F0H, NEXT                ; Compare number with next number

AJMP SKIP                               ; Jump to SKIP unconditionally

NEXT: JC SKIP                          ; If number<next number then go to SKIP

MOV DPL, R2                          ; Else exchange the number with next number

MOVX @DPTR, A                    ; Copy greater number to memory location

INC DPTR                                 ; Increment memory pointer

MOV A, 0F0H                         


SKIP: DJNZ R1, REPEAT           ; Decrement byte counter by 1, if byte counter≠ 0                                                            then go to REPEAT.

DJNZ R0, REP1                        ; Decrement pass counter if not zero then go to REP1

STOP: AJMP STOP                   ; Stop


The only difference between assembly language program to arrange numbers ascending/descending order for 8051 microcontroller is

  • For descending order NEXT: JC SKIP (11th line of program) instruction is used.
  • For ascending order NEXT: JNC SKIP instruction is used.

Recent posts

1 thought on “8051 Program to Arrange Numbers in Descending Order”

Leave a Comment

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