Table of Contents
ToggleProblem
Write an assembly language program for microcontroller 8051 to arrange numbers in ascending 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.
Algorithm
- To arrange the numbers in ascending 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 we get arranged data in ascending 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 less than or equal to next number, then go to step 8.
Step 7: Replace number with next number which is largest.
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
Program
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: JNC 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
MOVX @DPTR, A
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
Note:
The only difference between assembly language program to arrange numbers ascending/descending order for 8051 microcontroller is
- For ascending order NEXT: JNC SKIP instruction is used.
- For descending order NEXT: JC SKIP (11th line of program) instruction is used.
Recent posts
Related posts:
- Assembler Directives in 8051 Microcontroller
- Features of 8051 Microcontroller
- Memory Organization of 8051 Microcontroller
- Addressing Modes in 8051 Microcontroller
- Instruction Set in 8051 Microcontroller
- Architecture of 8051 Microcontroller
- PSW Register in 8051 Microcontroller | Program Status Word
- Pin Configuration of 8051 Microcontroller
- The Stack and Stack Pointer in 8051 Microcontroller
- Stack Pointer Data Pointer and Program Counter in 8051 Microcontroller
- Functions of Timing and Control Unit of 8051
- Functions of Ports in 8051 Microcontroller
- Port Structure of 8051 Microcontroller
- Reset Circuit of 8051 Microcontroller
- Registers in 8051 Microcontroller
- Boolean Processor in 8051 Microcontroller
- Power Saving Mode in 8051 | 8051 Power Down and Idle Mode
- 8051 Microcontroller Family
- Program Development Steps in ALP
- Addition of Two 8-Bit Numbers in 8051 Microcontroller
- Addition of Two 8 Bit Numbers with Carry in 8051 Microcontroller
- 8051 Program for Addition of Two 16 Bit Numbers
- 8051 Program to Add an Array of Numbers
- 8051 Program for Subtraction of Two 8-Bit Numbers
- 8051 Program for Subtraction of Two 16 bit Numbers
- 8051 Program to Multiply two 8 Bit numbers
- 8051 Program to Divide two 8 Bit numbers
- 8051 Program to Find the Largest Number in An Array
- 8051 Program to Find the Smallest Number in An Array Stored in Internal Memory
- Interrupts of 8051 Microcontroller