1. Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.
    Dismiss Notice

Bubble Sort 8051

Discussion in '8051/8951' started by REBORN, Nov 23, 2015.

  1. REBORN

    REBORN New Member

    Joined:
    Nov 23, 2015
    Messages:
    1
    Likes:
    0
    Hi !

    I need a help to do a bubble sort in assembly 8051, and the number of the elements of vector is located in R0 , and so far I do this code.

    Code (asm):

      $MOD51
       
       ORG     0
       
    ORD:  
       MOV      A,@R0
       MOV      R7,A
       DEC      R7
       INC      R0
       MOV      B, R0

    ROT2:
       MOV      R0,B
       MOV      A, R7  
       MOV      R6,A
       
    ROT3:  
       ACALL    COMP
       DJNZ      R6,ROT3
       DJNZ      R7,ROT2
       RET

    COMP:  
       MOV      A,@R0
       INC      R0
       CLR      C
       SUBB    A,@R0
       JC        FIM
       XCH      A,@R0
       DEC     R0
       XCH      A,R0
       INC      R0
       XCH      A,@R0
       RET
    FIM:
       SJMP    FIM
       
       END
     
     
    Last edited by a moderator: Nov 23, 2015
  2. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,163
    Likes:
    909
    Location:
    Rochdale UK
    Sorry had to clean it up so I could read it!!!

    Code tags make it easier for others to read code.. What's not happeneing
     
  3. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,163
    Likes:
    909
    Location:
    Rochdale UK
    That wasn't bubble sort.. Bubble sort is the slowest sort routine... starting at the beginning it will swap bytes systematically and run through the array n(2) times... I have implemented a bubble sort with 8 items located at position 0x30..

    Code (asm):

      $MOD51

       ORG   0
    OUTLP:
       MOV   R0,#30H
       MOV   R1,#31H
       MOV   R2,#7
    INLP:   MOV    A,@R0
       MOV    B,@R1
       ACALL    COMP
       INC   R1
       INC   R0
       DJNZ    R2,INLP
       DJNZ    R3,OUTLP
    FIM:
       SJMP    FIM
    COMP:
       CLR    C
       SUBB    A,B
       JC    DONE
       MOV   A,@R0
       MOV   B,@R1
       MOV    @R0,B
       MOV   @R1,A
    DONE:   RET

       END
     
     

Share This Page