MCS51單片機的檢索查找程序
;單字節順序查找程序
;入口??? :R0,R1,A,R7
;占用資源:B
;堆棧需求:2字節
;出口??? :R0,R1,A
FINDB1?? :MOV??? B,A
????????? MOV??? DPL,R1
????????? MOV??? DPH,R0
FINDB11? :MOVX?? A,@DPTR
????????? CJNE?? A,B,FINDB12
????????? MOV??? R1,DPL
????????? MOV??? R0,DPH
????????? CLR??? A
????????? RET
FINDB12? :INC??? DPTR
????????? DJNZ?? R7,FINDB11
????????? MOV??? A,#0FFH
????????? RET
;單字節順序查找程序
;入口??? :R0,R1,A,R6,R7
;占用資源:B
;堆棧需求:2字節
;出口??? :R0,R1,A
FINDB2?? :MOV??? B,A
????????? MOV??? DPL,R1
????????? MOV??? DPH,R0
????????? MOV??? A,R7
????????? JZ???? FINDB21
????????? INC??? R6
FINDB21? :MOVX?? A,@DPTR
????????? CJNE?? A,B,FINDB22
????????? MOV??? R1,DPL
????????? MOV??? R0,DPH
????????? CLR??? A
????????? RET
FINDB22? :INC??? DPTR
????????? DJNZ?? R7,FINDB21
????????? DJNZ?? R6,FINDB21
????????? MOV??? A,#0FFH
????????? RET
;雙字節字符串順序查找程序
;入口??? :R0,R1,R3,R4,R7
;占用資源:ACC,B
;堆棧需求:2字節
;出口??? :R0,R1,A
FINDS1?? :MOV??? DPL,R1
????????? MOV??? DPH,R0
FINDS11? :MOVX?? A,@DPTR
????????? INC??? DPTR
????????? CLR??? C
????????? SUBB?? A,R4
????????? JNZ??? FINDS12
????????? MOVX?? A,@DPTR
????????? SUBB?? A,R3
????????? JNZ??? FINDS12
????????? MOV??? A,DPL
????????? CLR??? C
????????? SUBB?? A,#01H
????????? MOV??? R1,A
????????? MOV??? A,DPH
????????? SUBB?? A,#00H
????????? MOV??? R0,A
????????? CLR??? A
????????? RET
FINDS12? :DJNZ?? R7,FINDS11
????????? MOV??? A,#0FFH
????????? RET
;雙字節字符串順序查找程序
;入口??? :R0,R1,R3,R4,R6,R7
;占用資源:ACC,B
;堆棧需求:2字節
;出口??? :R0,R1,A
FINDS2?? :MOV??? DPL,R1
????????? MOV??? DPH,R0
????????? MOV??? A,R7
????????? JZ???? FINDS21
????????? INC??? R6
FINDS21? :MOVX?? A,@DPTR
????????? INC??? DPTR
????????? CLR??? C
????????? SUBB?? A,R4
????????? JNZ??? FINDS22
????????? MOVX?? A,@DPTR
????????? SUBB?? A,R3
????????? JNZ??? FINDS22
????????? MOV??? A,DPL
????????? CLR??? C
????????? SUBB?? A,#01H
????????? MOV??? R1,A
????????? MOV??? A,DPH
????????? SUBB?? A,#00H
????????? MOV??? R0,A
????????? CLR??? A
????????? RET
FINDS22? :DJNZ?? R7,FINDS21
????????? DJNZ?? R6,FINDS21
????????? MOV??? A,#0FFH
????????? RET
;N字節字符串順序查找程序
;入口??? :ADDPH,ADDPL,R0,R6,R7,N
;占用資源:ACC,B,R2,NCNT
;堆棧需求:2字節
;出口??? :ADDPH,ADDPL,A
FINDN??? :MOV??? A,R0
????????? MOV??? R2,A
????????? MOV??? A,ADDPL
????????? MOV??? DPL,A
????????? MOV??? A,ADDPH
????????? MOV??? DPH,A
????????? MOV??? A,R7
????????? JZ???? FINDN1
????????? INC??? R6
FINDN1?? :MOV??? A,R2
????????? MOV??? R0,A
????????? MOV??? A,N
????????? MOV??? NCNT,A
FINDN2?? :MOVX?? A,@DPTR
????????? CLR??? C
????????? SUBB?? A,@R0
????????? JNZ??? FINDN3
????????? INC??? DPTR
????????? INC??? R0
????????? DJNZ?? NCNT,FINDN2
????????? MOV??? A,DPL
????????? CLR??? C
????????? SUBB?? A,N
????????? MOV??? ADDPL,A
????????? MOV??? A,DPH
????????? SUBB?? A,#00H
????????? MOV??? ADDPH,A
????????? CLR??? A
????????? RET
FINDN3?? :CLR??? C
????????? MOV??? A,R0
????????? SUBB?? A,R2
????????? JNZ??? FINDN4
????????? INC??? DPTR
FINDN4?? :DJNZ?? R7,FINDN1
????????? DJNZ?? R6,FINDN1
????????? MOV??? A,#0FFH
????????? RET
;單字節最值查找程序
;入口??? :R0,R1,R6,R7
;占用資源:ACC,B
;堆棧需求:2字節
;出口??? :R0(最大值),R1(最小值),R2,R3,R4,R5
FMAMIB?? :MOV??? DPL,R1
????????? MOV??? DPH,R0
????????? MOVX?? A,@DPTR
????????? MOV??? R0,A
????????? MOV??? R1,A
????????? MOV??? A,DPL
????????? MOV??? R3,A
????????? MOV??? R5,A
????????? MOV??? A,DPH
????????? MOV??? R2,A
????????? MOV??? R4,A
????????? MOV??? A,R7
????????? JZ???? FMMB1
????????? INC??? R6
FMMB1??? :MOVX?? A,@DPTR
????????? MOV??? B,A
????????? SETB?? C
????????? SUBB?? A,R0
????????? JC???? FMMB2
????????? MOV??? R0,B
????????? MOV??? R3,DPL
????????? MOV??? R2,DPH
????????? SJMP?? FMMB3
FMMB2??? :MOV??? A,B
????????? CLR??? C
????????? SUBB?? A,R1
????????? JNC??? FMMB3
????????? MOV??? R1,B
????????? MOV??? R5,DPL
????????? MOV??? R4,DPH
FMMB3??? :INC??? DPTR
????????? DJNZ?? R7,FMMB1
????????? DJNZ?? R6,FMMB1
????????? RET
;浮點數順序查找程序
;入口??? :R0,R1,R2,R3,R4,R6,R7
;占用資源:B,NCNT,FCMP
;堆棧需求:2字節
;出口??? :R0,R1,A
FINDF??? :MOV??? DPL,R1
????????? MOV??? DPH,R0
????????? MOV??? A,R7
????????? MOV??? B,A
????????? MOV??? NCNT,R6
????????? JZ???? FINDF1
????????? INC??? NCNT
FINDF1?? :MOVX?? A,@DPTR
????????? INC??? DPTR
????????? MOV??? R5,A
????????? MOVX?? A,@DPTR
????????? INC??? DPTR
????????? MOV??? R6,A
????????? MOVX?? A,@DPTR
????????? INC??? DPTR
????????? MOV??? R7,A
????????? LCALL? FCMP
????????? JNZ??? FINDF2
????????? MOV??? A,DPL
????????? CLR??? C
????????? SUBB?? A,#03H
????????? MOV??? R1,A
????????? MOV??? A,DPH
????????? SUBB?? A,#00H
????????? MOV??? R0,A
????????? CLR??? A
????????? RET
FINDF2?? :DJNZ?? B,FINDF1
????????? DJNZ?? NCNT,FINDF1
????????? MOV??? A,#0FFH
????????? RET
;浮點數最值查找程序
;入口??? :ADDPH,ADDPL,R6,R7
;占用資源:ACC,B,NCNT,ITEMOV,EBMOV,MOVB,MOVR1,FCMP
;堆棧需求:5字節
;出口??? :[R0](最大值),[R1](最小值),R2,R3,R4,R5
FMAMIF?? :MOV??? A,ADDPL
????????? MOV??? R3,A
????????? MOV??? R5,A
????????? MOV??? DPL,A
????????? MOV??? A,ADDPH
????????? MOV??? R2,A
????????? MOV??? R4,A
????????? MOV??? DPH,A
????????? MOV??? B,R7
????????? MOV??? R7,#03H
????????? LCALL? ITEMOV
????????? MOV??? R7,#03H
????????? LCALL? IBMOV
????????? MOV??? A,B
????????? JZ???? FMMF1
????????? INC??? NCNT
FMMF1??? :PUSH?? B
????????? MOVX?? A,@DPTR
????????? INC??? DPTR
????????? MOV??? R2,A
????????? MOVX?? A,@DPTR
????????? INC??? DPTR
????????? MOV??? R3,A
????????? MOVX?? A,@DPTR
????????? INC??? DPTR
????????? MOV??? R4,A
????????? LCALL? MOVR1
????????? LCALL? FCMP
????????? JNC??? FMMF2
????????? MOV??? A,R0
????????? XCH??? A,R1
????????? MOV??? R0,A
????????? LCALL? MOVB
????????? MOV??? R5,DPL
????????? MOV??? R4,DPH
????????? MOV??? A,R0
????????? XCH??? A,R1
????????? MOV??? R0,A
????????? SJMP?? FMMF3
FMMF2??? :MOV??? A,R0
????????? XCH??? A,R1
????????? MOV??? R0,A
????????? LCALL? MOVR1
????????? LCALL? FCMP
????????? MOV??? A,R0
????????? XCH??? A,R1
????????? MOV??? R0,A
????????? JZ???? FMMF3
????????? JC???? FMMF3
????????? LCALL? MOVB
????????? MOV??? R3,DPL
????????? MOV??? R2,DPH
FMMF3??? :POP??? B
????????? DJNZ?? B,FMMF1
????????? DJNZ?? NCNT,FMMF1
????????? RET
;單字節折半查找程序
;入口??? :A,R0,R1,R6,R7
;占用資源:B,R2
;堆棧需求:2字節
;出口??? :R0,R1
SEARCHB? :MOV??? B,A
????????? MOV??? A,R1
????????? ADD??? A,R7
????????? MOV??? R7,A
????????? MOV??? A,R0
????????? ADDC?? A,R6
????????? MOV??? R6,A
????????? MOV??? A,R7
????????? SUBB?? A,#01H
????????? MOV??? R7,A
????????? JNC??? SECH1
????????? DEC??? R6
SECH1??? :MOV??? A,R7
????????? CLR??? C
????????? SUBB?? A,R1
????????? MOV??? A,R6
????????? SUBB?? A,R0
????????? JNC??? SECH2
????????? MOV??? A,#0FFH
????????? RET
SECH2??? :MOV??? A,R7
????????? ADD??? A,R1
????????? MOV??? R2,A
????????? MOV??? A,R6
????????? ADDC?? A,R0
????????? RRC??? A
????????? MOV??? DPH,A
????????? MOV??? A,R2
????????? RRC??? A
????????? MOV??? DPL,A
????????? MOVX?? A,@DPTR
????????? CLR??? C
????????? SUBB?? A,B
????????? JNC??? SECH3
????????? INC??? DPTR
????????? MOV??? R0,DPH
????????? MOV??? R1,DPL
????????? SJMP?? SECH1
SECH3??? :JZ???? SECH5
????????? MOV??? A,DPL
????????? SUBB?? A,#01H
????????? MOV??? R7,A
????????? JNC??? SECH4
????????? MOV??? R6,DPH
????????? DEC??? R6
SECH4??? :SJMP?? SECH1
??????????? SECH5??? :MOV??? R0,DPH
??????????? MOV??? R1,DPL
??????????? CLR??? A
??????????? RET
評論
查看更多