8051 Microcontroller Descending Order

In summary, your program for sorting an array in memory using 8051 microcontroller doesn't seem to work.
  • #1
tyogav
14
0
I have written a program for sorting an array in memory using 8051 microcontroller. But when I modify it for descending order(Replace JNC by JC). It doesn't work.

What is the point that I am missing?
 

Attachments

  • 1418912200645.jpg
    1418912200645.jpg
    52.2 KB · Views: 2,769
Technology news on Phys.org
  • #2
I suspect that your "JNC" version didn't really work either. The numbers may have ended up sorted only because they were partially sorted to begin with.
Perhaps you should post the full sort program - not just the first page.
I would also be good if you could post it right-side up.
 
  • #3
Full Program
 

Attachments

  • 1418913299646.jpg
    1418913299646.jpg
    27.4 KB · Views: 2,083
  • 1418913337210.jpg
    1418913337210.jpg
    14.9 KB · Views: 1,446
  • #4
Sorry, I couldn't post it with correct orientation. Somehow I get it posted wrongly.
 
  • #5
@tyogav, all three of the images you uploaded are sideways. It would be much better if you typed your program and entered it as text directly into the input pane here. Many helpers won't bother to comment if they have to click an image, especially one that isn't oriented correctly.

Having your code appear as text rather than as an image has the advantage that helpers can identify a line of code that is incorrect. When the code is posted as an image, helpers need to type the line or lines where a problem occurs.
 
  • #6
MOV R0, #(N-1)
MOV A,R0
MOV R1,A
L3: MOV DPTR, 4150
L1: MOVX A,@DPTR
MOV B,A
INC DPTR
MOVX A,@DPTR
MOV R4,A
CLR C
SUBB A,B
MOV A,R4
JNC L2
XCH A,B
L2: MOVX @DPTR,A
MOV R0,DPL
DEC R0
MOV DPL,R0
MOV A,B
MOVX @DPTR,A
INC DPTR
DJNZ R0,L1
DJNZ R1,L3
HLT: SJMP HLT
 
  • #8
For everyones information: DPL is the low-order byte of DPTR.

You are overwriting R0 with it - which may be you're intention. I can't tell.

I think the problem is that when you loop from DJNZ R1,L3, R0 is not reset.
 
  • Like
Likes tyogav
  • #9
This isn't a bubble sort. DPTR should only be incremented once per loop. After a compare (subtract and restore A), if the elements are not in order, then decrement DPTR, store the smaller (or larger if descending) number, increment DPTR and store the larger (or smaller if descending). If the elements are in order, then skip to the end of the next inner loop. The idea here is to move the largest (or smallest if descending) element to the end of the array on each loop. The "size" of the array can be decremented on each loop since the largest (or smallest) values are already moved to the end.
 
Last edited:
  • #10
rcgldr said:
This isn't a bubble sort. The code increments DPTR twice on each loop. It should only increment DPTR once on each loop.
That's why I put the note about DPL in my last post.
He increments DPTR twice and decrements the low order byte once. As long as his list is less than 160 bytes long, he's okay.
 
  • #11
Why not use this to decrement DPTR, and also avoid overwriting R0?

Code:
        XCH     A,DPL
        JNZ     D1
        DEC     DPH
D1:     DEC     A
        XCH     A,DPL
 
Last edited:
  • #12
.Scott said:
I think the problem is that when you loop from DJNZ R1,L3, R0 is not reset.

rcgldr said:
Why not use this to decrement DPTR, and also avoid overwriting R0?
Thanks. So overwriting R0 has been the problem. I will replace R0 with another register and check.

I wonder how it worked for ascending order.
:oldconfused:
 
  • #13
Could anyone suggest a good 8051 simulator to use in Windows as I don't have access a 8051 microcontroller kit right now? I am using EdSim51 right now. But it doesn't support MOVX command :L.
 

1. What is a 8051 microcontroller?

The 8051 microcontroller is a type of microprocessor that is commonly used in embedded systems. It was first introduced in 1981 by Intel and has since been widely used due to its low cost, simplicity, and versatility.

2. What is the purpose of "descending order" in relation to the 8051 microcontroller?

"Descending order" refers to the way the 8051 microcontroller organizes and executes instructions. In descending order, instructions are executed from highest to lowest memory addresses, meaning that the first instruction in a program will be located at the highest memory address and the last instruction will be at the lowest memory address.

3. How does the "descending order" affect the performance of the 8051 microcontroller?

The descending order of instructions in the 8051 microcontroller allows for efficient use of memory and can result in faster execution times. This is because the microcontroller does not have to search for the next instruction in memory, as it is already located in the next sequential address.

4. What are the advantages of using the 8051 microcontroller in descending order?

Some of the advantages of using the 8051 microcontroller in descending order include faster execution times, efficient use of memory, and simpler programming. The descending order also makes it easier to debug programs, as the instructions are organized in a predictable and sequential manner.

5. Are there any downsides to using the 8051 microcontroller in descending order?

While there are many advantages to using the 8051 microcontroller in descending order, there are some potential downsides as well. For example, using descending order may limit the flexibility of the program and make it more difficult to implement certain functions. Additionally, if the program is not properly organized, it may lead to inefficient use of memory and slower execution times.

Similar threads

  • Programming and Computer Science
Replies
6
Views
3K
  • Programming and Computer Science
Replies
7
Views
3K
  • Electrical Engineering
Replies
10
Views
2K
  • Programming and Computer Science
Replies
8
Views
788
Replies
2
Views
1K
  • Electrical Engineering
Replies
8
Views
950
  • Programming and Computer Science
Replies
6
Views
817
  • Programming and Computer Science
Replies
17
Views
1K
  • Programming and Computer Science
Replies
19
Views
1K
  • Computing and Technology
Replies
5
Views
2K
Back
Top