//无符号数归并排序
ORG 0000H
LJMP MAIN
ORG 0F00H
MAIN:
//测试数据准备2100H~210AH填入0BH~01H
MOV SP, #4FH
MOV DPTR, #2100H
MOV R7, #0BH
TEST_LOOP1:
MOV A, R7
MOVX @DPTR, A
INC DPTR
DJNZ R7, TEST_LOOP1
MOV DPTR, #2105H
MOV A, #0BH
MOVX @DPTR, A
MOV BEGIN_POINTER, #00H
MOV END_POINTER , #0AH
LCALL MERGE_SORT
SJMP $
BEGIN_POINTER DATA 30H
MID_POINTER DATA 31H
END_POINTER DATA 32H
MERGE_SORT:
PUSH BEGIN_POINTER
PUSH END_POINTER
MOV A, BEGIN_POINTER
CJNE A, END_POINTER, N_EQU
POP END_POINTER
POP BEGIN_POINTER
RET
N_EQU:
JC CONTINUE //BEGIN_POINTER<END_POINTER
POP END_POINTER
POP BEGIN_POINTER
RET
CONTINUE:
CLR C
MOV B, #2
MOV A, BEGIN_POINTER
ADD A, END_POINTER
DIV AB
MOV MID_POINTER, A
//递归
MOV BEGIN_POINTER, BEGIN_POINTER
PUSH MID_POINTER
MOV END_POINTER, MID_POINTER
LCALL MERGE_SORT
POP MID_POINTER
INC MID_POINTER
POP END_POINTER
POP BEGIN_POINTER
PUSH BEGIN_POINTER
PUSH END_POINTER
PUSH MID_POINTER
MOV BEGIN_POINTER, MID_POINTER
MOV END_POINTER, END_POINTER
LCALL MERGE_SORT
POP MID_POINTER
DEC MID_POINTER
POP END_POINTER
MOV MERGE_END_POINTER, END_POINTER
POP BEGIN_POINTER
MOV MERGE_BEGIN_POINTER,BEGIN_POINTER
MOV MERGE_MID_POINTER, MID_POINTER
LCALL MERGE
RET
MERGE_BEGIN_POINTER DATA 33H
MERGE_MID_POINTER DATA 34H
MERGE_END_POINTER DATA 35H
MERGE_TEMP_K DATA 36H
MERGE_TEMP_I DATA 37H
MERGE_TEMP_J DATA 38H
MERGE_TEMP_1 DATA 39H
//1100H开始为数组
RESULT_DPTR EQU 1100H
MERGE:
MOV MERGE_TEMP_K, #0
MOV MERGE_TEMP_I, MERGE_BEGIN_POINTER
MOV MERGE_TEMP_J, MERGE_MID_POINTER
INC MERGE_TEMP_J
LOOP1:
MOV A, MERGE_TEMP_I
CJNE A, MERGE_MID_POINTER, N_EQU1
LJMP NEXT_JUDGE1
N_EQU1:
JNC BRANCH1
NEXT_JUDGE1:
MOV A, MERGE_TEMP_J
CJNE A, MERGE_END_POINTER, N_EQU2
LJMP LP1_JUDGE
N_EQU2:
JNC BRANCH1
LP1_JUDGE:
MOV DPL, MERGE_TEMP_I
MOVX A, @DPTR
MOV MERGE_TEMP_1, A
MOV DPL, MERGE_TEMP_J
MOVX A, @DPTR
CJNE A, MERGE_TEMP_1, N_EQU3
LJMP LP1_BRANCH2
N_EQU3:
JC LP1_BRANCH2
LP1_BRANCH1:
MOV DPL, MERGE_TEMP_I
MOVX A, @DPTR
INC MERGE_TEMP_I
MOV DPTR, #RESULT_DPTR
MOV DPL, MERGE_TEMP_K
MOVX @DPTR, A
INC MERGE_TEMP_K
MOV DPTR, #2100H
LJMP END_BRANCH
LP1_BRANCH2:
MOV DPL, MERGE_TEMP_J
MOVX A, @DPTR
INC MERGE_TEMP_J
MOV DPTR, #RESULT_DPTR
MOV DPL, MERGE_TEMP_K
MOVX @DPTR, A
INC MERGE_TEMP_K
MOV DPTR, #2100H
END_BRANCH:
LJMP LOOP1
BRANCH1:
MOV MERGE_TEMP_1, MERGE_MID_POINTER
INC MERGE_TEMP_1
MOV A, MERGE_TEMP_I
CJNE A, MERGE_TEMP_1, BRANCH2
BRANCH1_LOOP:
MOV A, MERGE_TEMP_J
CJNE A, MERGE_END_POINTER, N_EQU4
LJMP BRANCH1_LOOP_OPER
N_EQU4:
JNC BRANCH2
BRANCH1_LOOP_OPER:
MOV DPL, MERGE_TEMP_J
MOVX A, @DPTR
INC MERGE_TEMP_J
MOV DPTR, #RESULT_DPTR
MOV DPL, MERGE_TEMP_K
MOVX @DPTR, A
INC MERGE_TEMP_K
MOV DPTR, #2100H
LJMP BRANCH1_LOOP
BRANCH2:
MOV MERGE_TEMP_1, MERGE_END_POINTER
INC MERGE_TEMP_1
MOV A, MERGE_TEMP_J
CJNE A, MERGE_TEMP_J, LOOP2_START
BRANCH2_LOOP:
MOV A, MERGE_TEMP_I
CJNE A, MERGE_MID_POINTER, N_EQU5
LJMP BRANCH2_LOOP_OPER
N_EQU5:
JNC LOOP2_START
BRANCH2_LOOP_OPER:
MOV DPL, MERGE_TEMP_I
MOVX A, @DPTR
INC MERGE_TEMP_I
MOV DPTR, #RESULT_DPTR
MOV DPL, MERGE_TEMP_K
MOVX @DPTR, A
INC MERGE_TEMP_K
MOV DPTR, #2100H
LJMP BRANCH2_LOOP
LOOP2_START:
MOV MERGE_TEMP_J, #0
MOV MERGE_TEMP_I, MERGE_BEGIN_POINTER
LOOP2:
MOV A, MERGE_TEMP_J
CJNE A, MERGE_TEMP_K, N_EQU6
LJMP RETURN
N_EQU6:
JNC RETURN
MOV DPTR, #RESULT_DPTR
MOV DPL, MERGE_TEMP_J
MOVX A, @DPTR
MOV DPTR, #2100H
MOV DPL, MERGE_TEMP_I
MOVX @DPTR, A
INC MERGE_TEMP_I
INC MERGE_TEMP_J
LJMP LOOP2
RETURN:
RET
END
04-03