MCS51單片機的排序程序(ASM源代碼)
;內部RAM數據排序程序(升序)
;入口??? :R0(起始地址),N(數據個數)
;占用資源:ACC,B,R5,R6,R7
;堆棧需求:2字節
;出口??? :R0
ISELSORT :MOV??? R7,N
????????? DEC??? R7
ISST1??? :MOV??? A,R7
????????? MOV??? R6,A
????????? MOV??? A,R0
????????? MOV??? R1,A
????????? MOV??? R2,A
????????? MOV??? B,@R1
ISST2??? :INC??? R1
????????? MOV??? A,@R1
????????? CLR??? C
????????? SUBB?? A,B
????????? JC???? ISST3
????????? MOV??? A,R1
????????? MOV??? R2,A
????????? MOV??? B,@R1
ISST3??? :DJNZ?? R6,ISST2
????????? MOV??? A,B
????????? XCH??? A,@R1
????????? MOV??? B,R2
????????? MOV??? R1,B
????????? MOV??? @R1,A
????????? DJNZ?? R7,ISST1
????????? RET
;外部RAM數據排序程序(升序)
;入口??? :ADDPH,ADDPL(起始地址),N(數據個數)
;占用資源:ACC,B,R0,R1,R5,R7
;堆棧需求:2字節
;出口??? :ADDPH-ADDPL
ESELSORT :MOV??? R7,N
????????? DEC??? R7
ESST1??? :MOV??? A,R7
????????? MOV??? R6,A
????????? MOV??? DPL,ADDPL
????????? MOV??? R1,DPL
????????? MOV??? DPH,ADDPH
????????? MOV??? R0,DPH
????????? MOVX?? A,@DPTR
????????? MOV??? B,A
ESST2??? :INC??? DPTR
????????? MOVX?? A,@DPTR
????????? CLR??? C
????????? SUBB?? A,B
????????? JC???? ESST3
????????? MOV??? R0,DPL
????????? MOV??? R1,DPH
????????? MOVX?? A,@DPTR
????????? MOV??? B,A
ESST3??? :DJNZ?? R6,ESST2
????????? MOVX?? A,@DPTR
????????? XCH??? A,B
????????? MOVX?? @DPTR,A
????????? MOV??? DPL,R0
????????? MOV??? DPH,R1
????????? MOV??? A,B
????????? MOVX?? @DPTR,A
????????? DJNZ?? R7,ESST1
????????? RET
;外部RAM浮點數排序程序(升序)
;入口??? :ADDPH,ADDPL(起始地址),N(數據個數)
;占用資源:ACC,B,R0,R1,R2,R3,R4,R5,R6,R7,NCNT
;堆棧需求:5字節
;出口??? :ADDPH,ADDPL
FSORT??? :MOV??? A,N
????????? MOV??? NCNT,A
????????? DEC??? NCNT
FST1???? :MOV??? B,NCNT
????????? MOV??? DPL,ADDPL
????????? MOV??? R1,DPL
????????? MOV??? DPH,ADDPH
????????? MOV??? R0,DPH
????????? MOVX?? A,@DPTR
????????? MOV??? R2,A
????????? INC??? DPTR
????????? MOVX?? A,@DPTR
????????? MOV??? R3,A
????????? INC??? DPTR
????????? MOVX?? A,@DPTR
????????? MOV??? R4,A
FST2???? :INC??? DPTR
????????? MOVX?? A,@DPTR
????????? MOV??? R5,A
????????? INC??? DPTR
????????? MOVX?? A,@DPTR
????????? MOV??? R6,A
????????? INC??? DPTR
????????? MOVX?? A,@DPTR
????????? MOV??? R7,A
????????? PUSH?? B
????????? LCALL? FCMP
????????? POP??? B
????????? JNC??? FST4
????????? MOV??? A,DPL
????????? CLR??? C
????????? SUBB?? A,#02H
????????? MOV??? R1,A
????????? MOV??? R0,DPH
????????? JNC??? FST3
????????? DEC??? R0
FST3???? :MOV??? A,R5
????????? MOV??? R2,A
????????? MOV??? A,R6
????????? MOV??? R3,A
????????? MOV??? A,R7
????????? MOV??? R4,A
FST4???? :DJNZ?? B,FST2
????????? MOV??? A,DPL
????????? CLR??? C
????????? SUBB?? A,#02H
????????? MOV??? DPL,A
????????? JNC??? FST5
????????? DEC??? DPH
FST5???? :MOV??? A,R2
????????? MOVX?? @DPTR,A
????????? INC??? DPTR
????????? MOV??? A,R3
????????? MOVX?? @DPTR,A
????????? INC??? DPTR
????????? MOV??? A,R4
????????? MOVX?? @DPTR,A
????????? MOV??? A,R0
????????? MOV??? P2,A
????????? MOV??? A,R5
????????? MOVX?? @R1,A
????????? INC??? R1
????????? MOV??? A,R6
????????? MOVX?? @R1,A
????????? INC??? R1
????????? MOV??? A,R7
????????? MOVX?? @R1,A
????????? DJNZ?? NCNT,FST1
????????? RET
評論
查看更多