Continue to Site

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.

  • 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.

Bubble Sort 8051

Status
Not open for further replies.

REBORN

New Member
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:
  $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:
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:
  $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
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top