1单字节无符号数并归排序实例
DATS EQU 20H
N EQU 5DH
DATS1 EQU 21H
TEST:
MOV DPTR,#LIST
MOV P2,#DATS
MOV R0,#0
MOV R2,#N
CPY:
CLR A
MOVC A,@A+DPTR
MOVX @R0,A
INC DPTR
INC R0
DJNZ R2,CPY
MGSRT1:
MOV R3,#1
LOOP:
MOV P2,#DATS
MOV DPH,#DATS1
LCALL MGPSS
MOV P2,#DATS1
MOV DPH,#DATS
LCALL MGPSS
JC LOOPE
MOV A,R3
CLR C
SUBB A,#N
JC LOOP
LOOPE:
RET
MGPSS:
MOV R7,#N
MOV R0,#0
MOV DPL,#0
MERGS:
MOV A,R7
JZ MERGSE
CLR C
SUBB A,R3
JNC MERGS2
MERGS1:
MOV A,R0
MOV R1,A
MOV A,R7
MOV R2,A
LCALL MOVS
MERGSE:
MOV A,R3
CLR C
RLC A
MOV R3,A
RET
MERGS2:
JZ MERGS1
MOV R7,A
MOV A,R3
MOV R6,A
MOV A,R0
ADD A,R3
MOV R1,A
MOV A,R7
CLR C
SUBB A,R3
JNC MERGS3
MOV A,R7
MOV R5,A
MOV R7,#0
SJMP MERGS4
MERGS3:
MOV R7,A
MOV A,R3
MOV R5,A
MERGS4:
MOV A,R0
ADD A,R6
ADD A,R5
MOV R4,A
LCALL MERGE
MOV A,R4
MOV R0,A
LJMP MERGS
MERGE:
MOVX A,@R1
MOV B,A
MOVX A,@R0
CLR C
SUBB A,B
JC MERG1
MOVX A,@R1
MOVX @DPTR,A
INC R1
INC DPTR
DJNZ R5,MERGE
MOV A,R0
MOV R1,A
MOV A,R6
MOV R2,A
SJMP MOVS
MERG1:
MOVX A,@R0
MOVX @DPTR,A
INC R0
INC DPTR
DJNZ R6,MERGE
MOV A,R5
MOV R2,A
MOVS:
MOVX A,@R1
MOVX @DPTR,A
INC DPTR
INC R1
DJNZ R2,MOVS
RET
2多字节浮点数并归排序实例
ADDR EQU 20H
TEMP EQU 28H
NUMB EQU 0120H
D1 EQU 30H
D2 EQU 33H
NH DATA 36H
NL DATA 37H
LENH DATA 38H
LENL DATA 39H
CONH DATA 3AH
CONL DATA 3BH
TST:
MOV P2,#ADDR
MOV R0,#0
MOV R2,#HIGH(NUMB)
MOV R3,#LOW(NUMB)
MOV DPTR,#LIST
MOV A,R3
JZ TST1
INC R2
TST1:
MOV R4,#3
TEST2:
CLR A
MOVC A,@A+DPTR
MOVX @R0,A
INC DPTR
INC R0
CJNE R0,#0,TEST3
INC P2
TST3:
DJNZ R4,TST2
DJNZ R3,TST1
DJNZ R2,TST1
LCALL MSORT
MSORT:
MOV LENH,#0
MOV LENL,#1
LOOP:
MOV R2,#ADDR
MOV DPH,#TEMP
LCALL MPASS
MOV R2,#TEMP
MOV DPH,#ADDR
LCALL MPASS
MOV A,LENL
CLR C
SUBB A,#LOW(NUMB)
MOV A,LENH
SUBB A,#HIGH(NUMB)
JC LOOP
LOOPE:
RET
MPASS:
MOV NH,#HIGH(NUMB)
MOV NL,#LOW(NUMB)
MOV R0,#0
MOV DPL,#0
MERGS:
MOV A,NH
ORL A,NL
JZ MERGSE
MOV A,NL
CLR C
SUBB A,LENL
MOV B,A
MOV A,NH
SUBB A,LENH
JNC MERGS2
MERGS1:
MOV A,R0
MOV R1,A
MOV P2,R2
MOV CONH,NH
MOV CONL,NL
LCALL MOVS
MERGSE:
MOV A,LENL
CLR C
RLC A
MOV LENL,A
MOV A,LENH
RLC A
MOV LENH,A
RET
MERGS2:
MOV NH,A
ORL A,B
JZ MERGS1
MOV NL,B
MOV R4,LENH
MOV R5,LENL
MOV A,R5
MOV B,#3
MUL AB
MOV R6,B
MOV R7,A
MOV A,R4
MOV B,#3
MUL AB
ADD A,R6
MOV R6,A
MOV A,R0
ADD A,R7
MOV R1,A
MOV A,R2
ADDC A,R6
MOV R3,A
MOV A,NL
CLR C
SUBB A,LENL
MOV B,A
MOV A,NH
SUBB A,LENH
JNC MERGS3
MOV R6,NH
MOV R7,NL
MOV NH,#0
MOV NL,#0
SJMP MERGS4
MERGS3:
MOV NH,A
MOV NL,B
MOV R6,LENH
MOV R7,LENL
MERGS4:
MOV A,R7
MOV B,#3
MUL AB
MOV CONH,B
MOV CONL,A
MOV A,R6
MOV B,#3
MUL AB
ADD A,CONH
MOV CONH,A
MOV A,R1
ADD A,CONL
PUSH ACC
MOV A,R3
ADDC A,CONH
PUSH ACC
LCALL MERGE
POP ACC
MOV R2,A
POP ACC
MOV R0,A
LJMP MERGS
MERGE:
MOV A,R0
PUSH ACC
XCH A,R1
PUSH ACC
MOV R0,#D1
MOV P2,R2
LCALL LOAD
POP ACC
PUSH ACC
MOV R1,A
MOV P2,R3
MOV R0,#D2
LCALL LOAD
MOV R0,#D1
MOV R1,#D2
LCALL FLTCMP
POP ACC
MOV R1,A
POP ACC
MOV R0,A
JC MERG5
MOV P2,R3
MOV B,#3
MERG2:
MOVX A,@R1
MOVX @DPTR,A
INC DPTR
INC R1
CJNE R1,#0,MERG3
INC P2
INC R3
MERG3:
DJNZ B,MERG2
CJNE R7,#0,MERG4
DEC R6
MERG4: DEC R7
MOV A,R6
ORL A,R7
JNZ MERGE
MOV A,R0
MOV R1,A
MOV P2,R2
MOV CONH,R4
MOV CONL,R5
SJMP MOVS
MERG5:
MOV P2,R2
MOV B,#3
MERG6:
MOVX A,@R0
MOVX @DPTR,A
INC DPTR
INC R0
CJNE R0,#0,MERG7
INC P2
INC R2
MERG7:
DJNZ B,MERG6
CJNE R5,#0,MERG8
DEC R4
MERG8:
DEC R5
MOV A,R4
ORL A,R5
JNZ MERGE
MOV P2,R3
MOV CONH,R6
MOV CONL,R7
MOVS:
MOV A,CONL
JZ MOVS1
INC CONH
MOVS1:
MOV B,#3
MOVS2:
MOVX A,@R1
MOVX @DPTR,A
INC DPTR
INC R1
CJNE R1,#0,MOVS3
INC P2
MOVS3:
DJNZ B,MOVS2
DJNZ CONL,MOVS1
DJNZ CONH,MOVS1
RET
LOAD:
MOV B,#3
LOAD1:
MOVX A,@R1
MOV @R0,A
INC R0
INC R1
CJNE R1,#0,LOAD2
INC P2
LOAD2:
DJNZ B,LOAD1
RET
FLTCMP:
MOV A,@R0
XRL A,@R1
JNB ACC.7,CMP2
MOV A,@R0
RLC A
MOV A,#0FFH
RET
CMP2:
MOV A,@R1
MOV C,ACC.6
MOV ACC.7,C
MOV B,A
MOV A,@R0
MOV C,ACC.7
MOV F0,C
MOV C,ACC.6
MOV ACC.7,C
CLR C
SUBB A,B
JZ CMP6
RLC A
JNB F0,CMP5
CPL C
CMP5:
MOV A,#0FFH
RET
CMP6:
INC R0
INC R0
INC R1
INC R1
CLR C
MOV A,@R0
SUBB A,@R1
MOV B,A
DEC R0
DEC R1
MOV A,@R0
SUBB A,@R1
DEC R0
DEC R1
ORL A,B
JZ CMP7
JNB F0,CMP7
CPL C
CMP7:
RET