8051 Microcontroller Descending Order

  • Thread starter Thread starter tyogav
  • Start date Start date
  • Tags Tags
    Microcontroller
Click For Summary

Discussion Overview

The discussion revolves around programming an 8051 microcontroller to sort an array in descending order. Participants explore issues related to the implementation of the sorting algorithm, specifically the modifications needed to switch from ascending to descending order, and the challenges faced in the code execution.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Mathematical reasoning
  • Homework-related

Main Points Raised

  • One participant notes that modifying the sorting program by replacing "JNC" with "JC" does not yield the expected results, suggesting there may be an underlying issue with the original implementation.
  • Another participant questions whether the original "JNC" version was functioning correctly, proposing that the array may have been partially sorted prior to the modification.
  • There is a suggestion to post the full program for better analysis, as the current format (images) is not conducive to troubleshooting.
  • Concerns are raised about the orientation of the posted code images, with a recommendation to type the code directly for clarity and ease of review.
  • One participant points out that the code increments DPTR twice per loop, which may not be appropriate for the sorting logic being implemented.
  • Another participant highlights that R0 is being overwritten, which could lead to issues in the sorting process, and suggests using a different register instead.
  • There is a discussion about the nature of the sorting algorithm being used, with participants clarifying that the current implementation does not follow a bubble sort approach.
  • A participant expresses confusion about how the code worked for ascending order but not for descending order, indicating a need for further investigation into the logic used.
  • One participant seeks recommendations for 8051 simulators that can be used on Windows, noting limitations with the current simulator being used.

Areas of Agreement / Disagreement

Participants express differing views on the effectiveness of the current sorting implementation and the modifications made for descending order. There is no consensus on the correctness of the original or modified code, and multiple competing perspectives on the sorting logic and register usage are present.

Contextual Notes

Participants note potential issues with the code related to the handling of registers and the incrementing of DPTR, as well as the need for clarity in code presentation. The discussion remains focused on troubleshooting and refining the sorting algorithm without resolving the underlying technical challenges.

Who May Find This Useful

This discussion may be useful for individuals working with 8051 microcontrollers, particularly those interested in sorting algorithms and debugging assembly language code.

tyogav
Messages
14
Reaction score
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,890
Technology news on Phys.org
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.
 
Full Program
 

Attachments

  • 1418913299646.jpg
    1418913299646.jpg
    27.4 KB · Views: 2,188
  • 1418913337210.jpg
    1418913337210.jpg
    14.9 KB · Views: 1,534
Sorry, I couldn't post it with correct orientation. Somehow I get it posted wrongly.
 
@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.
 
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
 
Thank you. That's much better.
 
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   Reactions: tyogav
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.
 

Similar threads

Replies
6
Views
4K
Replies
7
Views
4K
  • · Replies 10 ·
Replies
10
Views
3K
  • · Replies 7 ·
Replies
7
Views
4K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 8 ·
Replies
8
Views
1K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 8 ·
Replies
8
Views
6K
  • · Replies 2 ·
Replies
2
Views
4K