Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

8051 Microcontroller Descending Order

  1. Dec 18, 2014 #1
    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?
     

    Attached Files:

  2. jcsd
  3. Dec 18, 2014 #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.
     
  4. Dec 18, 2014 #3
    Full Program
     

    Attached Files:

  5. Dec 18, 2014 #4
    Sorry, I couldn't post it with correct orientation. Somehow I get it posted wrongly.
     
  6. Dec 18, 2014 #5

    Mark44

    Staff: Mentor

    @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.
     
  7. Dec 18, 2014 #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. Dec 18, 2014 #7

    Mark44

    Staff: Mentor

    Thank you. That's much better.
     
  9. Dec 18, 2014 #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.
     
  10. Dec 18, 2014 #9

    rcgldr

    User Avatar
    Homework Helper

    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: Dec 18, 2014
  11. Dec 18, 2014 #10
    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.
     
  12. Dec 18, 2014 #11

    rcgldr

    User Avatar
    Homework Helper

    Why not use this to decrement DPTR, and also avoid overwriting R0?

    Code (Text):

            XCH     A,DPL
            JNZ     D1
            DEC     DPH
    D1:     DEC     A
            XCH     A,DPL
     
     
    Last edited: Dec 18, 2014
  13. Dec 19, 2014 #12
    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:
     
  14. Dec 19, 2014 #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.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: 8051 Microcontroller Descending Order
  1. A 8051 program in C (Replies: 7)

Loading...