Search This Blog

Saturday, August 27, 2011

WAP in 8051 to sort a block of data in ascending or descending order


; PROGRAM STARTS HERE


MOV R6, #07H ; EXTERNAL COUNTER
START : MOV R7, #07H ; INTERNAL COUNTER
MOV R0, #30H ; INTERNAL MEMORY ADDRESS
MOV A, #00H ; CLEAR ACCUMULATOR
; CY = 0 A>VAL
; CY = 1 A<VAL
BACK :   MOV A, @R0 ;
INC R0 ;
CJNE A, @R0, CARRY ;
SJMP DECREMENTC ;
CARRY : JC DECREMENTC ;
MOV B, @R0 ;
MOV @R0, A ;
DEC R0 ;
MOV A, B ;
MOV @R0, A ;
DECREMENTC : INC R0 ;
DJNZ R7, BACK ;
DJNZ R6, START ;
END

19 comments:

  1. its showing error in 12th line .. "illegal operand"

    ReplyDelete
    Replies
    1. the space between ,and b shud nt come in that 12th line

      Delete
    2. Please Write the line your facing trouble, instead of the line number.

      Delete
  2. CJNE A, @R0, CARRY
    in this line a instruction error exists...

    ReplyDelete
  3. This comment has been removed by the author.

    ReplyDelete
  4. this is incorrect the instruction
    cjne a, @r0, carry
    doesnot exist
    try another one

    ReplyDelete
    Replies
    1. Please have a look at the following link...
      http://www.8052.com/51cjne

      A traditional 8051 has been used to execute this program..
      If you are using a new one or some new compiler or simulator the equivalent command has to be replaced....

      Delete
    2. cjne should have A,B,carry in its syntax

      Delete
  5. this is incorrect the instruction
    cjne a, @r0, carry
    doesnot exist
    try another one

    ReplyDelete
    Replies
    1. Please have a look at the following link...
      http://www.8052.com/51cjne

      A traditional 8051 has been used to execute this program..
      If you are using a new one or some new compiler or simulator the equivalent command has to be replaced....

      Delete
    2. That equivalent command i will redo and update soon.

      Delete
    3. % PROGRAM FOR DESCENDING ORDER
      MOV R0,#0FH
      UP1:MOV R7,#0FH
      MOV R1,#30H
      UP:MOV A,@R1
      INC R1
      MOV B,@R1
      CJNE A,B,NEXT
      SJMP DOWN
      NEXT:JNC DOWN
      MOV @R1,A
      DEC R1
      MOV A,B
      MOV @R1,A
      INC R1
      DOWN: DJNZ R7,UP
      DJNZ R0,UP1
      END

      Delete
    4. % program for ascending order

      MOV R0,#04H ;external counter
      UP1:MOV R7,#04H ;internal counter
      MOV R1,#30H
      UP:MOV A,@R1
      INC R1
      MOV B,@R1
      CJNE A,B,NEXT
      SJMP DOWN
      NEXT:JC DOWN
      MOV @R1,A
      DEC R1
      MOV A,B
      MOV @R1,A
      INC R1
      DOWN: DJNZ R7,UP
      DJNZ R0,UP1
      END

      Delete
    5. good work charusheela patil ....

      Delete
  6. How to write the code for arranging a block of binary numbers in ascending order ?

    ReplyDelete
    Replies
    1. Need information like how the binary numbers are stored?? Being a byte based memory system is stored as fixed sized array in memory as 1 or zero or some other way??

      Delete
  7. THIS PROGRAM WILL CERTAINLY WORK. ITS MY EFFORT. TRY OUT THIS ONE, 100% WORKING....
    ORG 0000H
    MOV R0,#40H
    MOV R2,#10
    RK:MOV R1,#40H
    MOV R3,#10
    KR:MOV A,@R0
    MOV B,@R1
    CJNE A,B,XY
    XY:JC KABIR
    MOV @R0,B
    MOV @R1,A
    KABIR:INC R1
    DJNZ R3,KR
    INC R0
    DJNZ R2,RK
    END

    ReplyDelete
  8. cjne a,00,carry
    try this....
    instead of @r0 ,register address is used in asm instruction

    ReplyDelete
  9. DECENDING ORDER :

    0000| MOV R4,#0AH
    0002| AGAIN : MOV R3,#0AH
    0004| MOV R0,#30H
    0006| CLR C
    0007| UP : MOV A,@R0
    0008| MOV R1,A
    0009| INC R0
    000A| MOV A,@R0
    000B| SUBB A,R1
    000C| JC SKIP
    000E| MOV A,@R0
    000F| DEC R0
    0010| MOV @R0,A
    0011| MOV A,R1
    0012| INC R0
    0013| MOV @R0,A
    0014| SKIP : DJNZ R3,UP
    0016| DJNZ R4,AGAIN
    END


    ASENDING ORDER
    0000| MOV R4,#0AH
    0002| AGAIN : MOV R3,#0AH
    0004| MOV R0,#30H
    0006| CLR C
    0007| UP : MOV A,@R0
    0008| MOV R1,A
    0009| INC R0
    000A| MOV A,@R0
    000B| SUBB A,R1
    000C| JNC SKIP
    000E| MOV A,@R0
    000F| DEC R0
    0010| MOV @R0,A
    0011| MOV A,R1
    0012| INC R0
    0013| MOV @R0,A
    0014| SKIP : DJNZ R3,UP
    0016| DJNZ R4,AGAIN
    END

    ReplyDelete