MCS51單片機(jī)數(shù)制轉(zhuǎn)換程序
;BCD小數(shù)轉(zhuǎn)換為二進(jìn)制小數(shù)(2位)
;入口??? :R0(低位首址),R7
;占用資源:ACC,B,R5
;堆棧需求:2字節(jié)
;出口??? :R3,R4
PDTB???? :CLR??? A
????????? MOV??? R3,A
????????? MOV??? R4,A
PDB1???? :MOV??? A,R3
????????? MOV??? B,#9AH
????????? MUL??? AB
????????? MOV??? R5,B
????????? XCH??? A,R4
????????? MOV??? B,#19H
????????? MUL??? AB
????????? ADD??? A,R4
????????? MOV??? A,R5
????????? ADDC?? A,B
????????? MOV??? R5,A
????????? MOV??? A,@R0
????????? MOV??? B,#9AH
????????? MUL??? AB
????????? ADD??? A,R5
????????? MOV??? R4,A
????????? CLR??? A
????????? ADDC?? A,B
????????? XCH??? A,R3
????????? MOV??? B,#19H
????????? MUL??? AB
????????? ADD??? A,R4
????????? MOV??? R4,A
????????? MOV??? A,B
????????? ADDC?? A,R3
????????? MOV??? R3,A
????????? MOV??? A,@R0
????????? MOV??? B,#19H
????????? MUL??? AB
????????? ADD??? A,R3
????????? MOV??? R3,A
????????? DEC??? R0
????????? DJNZ?? R7,PDB1
????????? RET
;BCD小數(shù)轉(zhuǎn)換為二進(jìn)制小數(shù)(N位)
;入口??? :R1(低位首址),M,N
;占用資源:ACC,B,R2,R3,R7
;堆棧需求:2字節(jié)
;出口??? :R0
PDTBMN?? :MOV??? A,R0
????????? MOV??? R2,A
????????? MOV??? A,R1
????????? MOV??? R3,A
????????? MOV??? B,N
????????? CLR??? A
PDBMN1?? :MOV??? @R0,A
????????? INC??? R0
????????? DJNZ?? B,PDBMN1
????????? MOV??? A,N
????????? SWAP?? A
????????? RR???? A
????????? MOV??? R7,A
PDBMN2?? :MOV??? A,R2
????????? MOV??? R0,A
????????? MOV??? A,R3
????????? MOV??? R1,A
????????? MOV??? B,M
????????? CLR??? C
PDBMN3?? :MOV??? A,@R1
????????? ADDC?? A,@R1
????????? DA???? A
????????? JNB??? ACC.4,PDBMN4
????????? SETB?? C
????????? CLR??? ACC.4
PDBMN4?? :MOV??? @R1,A
????????? INC??? R1
????????? DJNZ?? B,PDBMN3
????????? MOV??? B,N
PDBMN5?? :MOV??? A,@R0
????????? RLC??? A
????????? MOV??? @R0,A
????????? INC??? R0
????????? DJNZ?? B,PDBMN5
????????? DJNZ?? R7,PDBMN2
????????? MOV??? A,R2
????????? MOV??? R0,A
????????? RET
;BCD整數(shù)轉(zhuǎn)換為二進(jìn)制整數(shù)(1位)
;入口??? :R0(高位地址),R7
;占用資源:ACC,B
;堆棧需求:2字節(jié)
;出口??? :R4
IDTB1??? :CLR??? A
????????? MOV??? R4,A
IDB11??? :MOV??? A,R4
????????? MOV??? B,#0AH
????????? MUL??? AB
????????? ADD??? A,@R0
????????? INC??? R0
????????? MOV??? R4,A
????????? DJNZ?? R7,IDB11
????????? RET
;BCD整數(shù)轉(zhuǎn)換為二進(jìn)制整數(shù)(2位)
;入口??? :R0(高位地址),R7
;占用資源:ACC,B
;堆棧需求:2字節(jié)
;出口??? :R3,R4
IDTB2??? :CLR??? A
????????? MOV??? R3,A
????????? MOV??? R4,A
IDB21??? :MOV??? A,R4
????????? MOV??? B,#0AH
????????? MUL??? AB
????????? MOV??? R4,A
????????? MOV??? A,B
????????? XCH??? A,R3
????????? MOV??? B,#0AH
????????? MUL??? AB
????????? ADD??? A,R3
????????? MOV??? R3,A
????????? MOV??? A,R4
????????? ADD??? A,@R0
????????? INC??? R0
????????? MOV??? R4,A
????????? CLR??? A
????????? ADDC?? A,R3
????????? MOV??? R3,A
????????? DJNZ?? R7,IDB21
????????? RET
;BCD整數(shù)轉(zhuǎn)換為二進(jìn)制整數(shù)(3位)
;入口??? :R0(高位地址),R7
;占用資源:ACC,B
;堆棧需求:2字節(jié)
;出口??? :R2,R3,R4
IDTB3??? :CLR??? A
????????? MOV??? R2,A
????????? MOV??? R3,A
????????? MOV??? R4,A
IDB31??? :MOV??? A,R4
????????? MOV??? B,#0AH
????????? MUL??? AB
????????? MOV??? R4,A
????????? MOV??? A,B
????????? XCH??? A,R3
????????? MOV??? B,#0AH
????????? MUL??? AB
????????? ADD??? A,R3
????????? MOV??? R3,A
????????? CLR??? A
????????? ADDC?? A,B
????????? XCH??? A,R2
????????? MOV??? B,#0AH
????????? MUL??? AB
????????? ADD??? A,R2
????????? MOV??? R2,A
????????? MOV??? A,R4
????????? ADD??? A,@R0
????????? INC??? R0
????????? MOV??? R4,A
????????? CLR??? A
????????? ADDC?? A,R3
????????? MOV??? R3,A
????????? CLR??? A
????????? ADDC?? A,R2
????????? MOV??? R2,A
????????? DJNZ?? R7,IDB31
????????? RET
;BCD整數(shù)轉(zhuǎn)換為二進(jìn)制整數(shù)(N位)
;入口??? :R1(高位地址),M,N
;占用資源:ACC,B,R2,R7,NCNT,F0
;堆棧需求:2字節(jié)
;出口??? :R0
IDTBMN?? :MOV??? A,R0
????????? MOV??? R2,A
????????? MOV??? B,N
????????? CLR??? A
IDBMN1?? :MOV??? @R0,A
????????? INC??? R0
????????? DJNZ?? B,IDBMN1
????????? MOV??? A,R2
????????? MOV??? R0,A
????????? MOV??? A,M
????????? MOV??? NCNT,A
IDBMN2?? :MOV??? R7,N
????????? CLR??? A
????????? CLR??? F0
IDBMN3?? :XCH??? A,@R0
????????? MOV??? B,#0AH
????????? MUL??? AB
????????? MOV??? C,F0
????????? ADDC?? A,@R0
????????? MOV??? F0,C
????????? MOV??? @R0,A
????????? INC??? R0
????????? MOV??? A,B
????????? DJNZ?? R7,IDBMN3
????????? MOV??? A,R2
????????? MOV??? R0,A
????????? MOV??? A,@R1
????????? INC??? R1
????????? ADD??? A,@R0
????????? MOV??? @R0,A
????????? DJNZ?? NCNT,IDBMN2
????????? RET
;二進(jìn)制小數(shù)(2位)轉(zhuǎn)換為十進(jìn)制小數(shù)(分離BCD碼)
;入口??? :R3,R4,R7
;占用資源:ACC,B
;堆棧需求:3字節(jié)
;出口??? :R0
PBTD???? :MOV??? A,R7
????????? PUSH?? A
PBD1???? :MOV??? A,R4
????????? MOV??? B,#0AH
????????? MUL??? AB
????????? MOV??? R4,A
????????? MOV??? A,B
????????? XCH??? A,R3
????????? MOV??? B,#0AH
????????? MUL??? AB
????????? ADD??? A,R3
????????? MOV??? R3,A
????????? CLR??? A
????????? ADDC?? A,B
????????? MOV??? @R0,A
????????? INC??? R0
????????? DJNZ?? R7,PBD1
????????? POP??? A
????????? MOV??? R7,A
????????? MOV??? A,R0
????????? CLR??? C
????????? SUBB?? A,R7
????????? MOV??? R0,A
????????? RET
;二進(jìn)制小數(shù)(M位)轉(zhuǎn)換為十進(jìn)制小數(shù)(分離BCD碼)
;入口??? :R1,M,N
;占用資源:ACC,B,R2,R3,R7,NCNT
;堆棧需求:2字節(jié)
;出口??? :R0
PBTDMN?? :MOV??? A,R0
????????? MOV??? R2,A
????????? MOV??? A,R1
????????? MOV??? R3,A
????????? MOV??? A,N
????????? MOV??? NCNT,A
PBDMN1?? :MOV??? R7,M
????????? CLR??? A
????????? CLR??? F0
PBDMN2?? :XCH??? A,@R1
????????? MOV??? B,#0AH
????????? MUL??? AB
????????? MOV??? C,F0
????????? ADDC?? A,@R1
????????? MOV??? F0,C
????????? MOV??? @R1,A
????????? INC??? R1
????????? MOV??? A,B
????????? DJNZ?? R7,PBDMN2
????????? ADDC?? A,#00H
????????? MOV??? @R0,A
????????? INC??? R0
????????? MOV??? A,R3
????????? MOV??? R1,A
????????? DJNZ?? NCNT,PBDMN1
????????? MOV??? A,R2
????????? MOV??? R0,A
????????? RET
;二進(jìn)制整數(shù)(2位)轉(zhuǎn)換為十進(jìn)制整數(shù)(分離BCD碼)
;入口??? :R3,R4
;占用資源:ACC,R2,NDIV31
;堆棧需求:5字節(jié)
;出口??? :R0,NCNT
IBTD21?? :MOV??? NCNT,#00H
????????? MOV??? R2,#00H
IBD211?? :MOV??? R7,#0AH
????????? LCALL? NDIV31
????????? MOV??? A,R7
????????? MOV??? @R0,A
????????? INC??? R0
????????? INC??? NCNT
????????? MOV??? A,R3
????????? ORL??? A,R4
????????? JNZ??? IBD211
????????? MOV??? A,R0
????????? CLR??? C
????????? SUBB?? A,NCNT
????????? MOV??? R0,A
????????? RET
;二進(jìn)制整數(shù)(2位)轉(zhuǎn)換為十進(jìn)制整數(shù)(組合BCD碼)
;入口??? :R3,R4
;占用資源:ACC,B,R7
;堆棧需求:3字節(jié)
;出口??? :R0
IBTD22?? :MOV??? A,R0
????????? PUSH?? A
????????? MOV??? R7,#03H
????????? CLR??? A
IBD221?? :MOV??? @R0,A
????????? INC??? R0
????????? DJNZ?? R7,IBD221
????????? POP??? A
????????? MOV??? R0,A
????????? MOV??? R7,#10H
IBD222?? :PUSH?? A
????????? CLR??? C
????????? MOV??? A,R4
????????? RLC??? A
????????? MOV??? R4,A
????????? MOV??? A,R3
????????? RLC??? A
????????? MOV??? R3,A
????????? MOV??? B,#03H
IBD223?? :MOV??? A,@R0
????????? ADDC?? A,@R0
????????? DA???? A
????????? MOV??? @R0,A
????????? INC??? R0
????????? DJNZ?? B,IBD223
????????? POP??? A
????????? MOV??? R0,A
????????? DJNZ?? R7,IBD222
????????? RET
;二進(jìn)制整數(shù)(3位)轉(zhuǎn)換為十進(jìn)制整數(shù)(分離BCD碼)
;入口??? :R2,R3,R4
;占用資源:ACC,R2,NDIV31
;堆棧需求:5字節(jié)
;出口??? :R0,NCNT
IBTD31?? :CLR??? A
????????? MOV??? NCNT,A
IBD311?? :MOV??? R7,#0AH
????????? LCALL? NDIV31
????????? MOV??? A,R7
????????? MOV??? @R0,A
????????? INC??? R0
????????? INC??? NCNT
????????? MOV??? A,R2
????????? ORL??? A,R3
????????? ORL??? A,R4
????????? JNZ??? IBD311
????????? MOV??? A,R0
????????? CLR??? C
????????? SUBB?? A,NCNT
????????? MOV??? R0,A
????????? RET
;二進(jìn)制整數(shù)(3位)轉(zhuǎn)換為十進(jìn)制整數(shù)(組合BCD碼)
;入口??? :R2,R3,R4
;占用資源:ACC,B,R7
;堆棧需求:3字節(jié)
;出口??? :R0
IBTD32?? :MOV??? A,R0
????????? PUSH?? A
????????? MOV??? R7,#04H
????????? CLR??? A
IBD321?? :MOV??? @R0,A
????????? INC??? R0
????????? DJNZ?? R7,IBD321
????????? POP??? A
????????? MOV??? R0,A
????????? MOV??? R7,#18H
IBD322?? :PUSH?? A
????????? CLR??? C
????????? MOV??? A,R4
????????? RLC??? A
????????? MOV??? R4,A
????????? MOV??? A,R3
????????? RLC??? A
????????? MOV??? R3,A
????????? MOV??? A,R2
????????? RLC??? A
????????? MOV??? R2,A
????????? MOV??? B,#04H
IBD323?? :MOV??? A,@R0
????????? ADDC?? A,@R0
????????? DA???? A
????????? MOV??? @R0,A
????????? INC??? R0
????????? DJNZ?? B,IBD323
????????? POP??? A
????????? MOV??? R0,A
????????? DJNZ?? R7,IBD322
????????? RET
;二進(jìn)制整數(shù)(M位)轉(zhuǎn)換為十進(jìn)制整數(shù)(組合BCD碼)
;入口??? :R1,M,N
;占用資源:ACC,B,R2,R3,R7
;堆棧需求:2字節(jié)
;出口??? :R0
IBTDMN?? :MOV??? A,R0
????????? MOV??? R2,A
????????? MOV??? A,R1
????????? MOV??? R3,A
????????? MOV??? B,N
????????? CLR??? A
IBDMN1?? :MOV??? @R0,A
????????? INC??? R0
????????? DJNZ?? B,IBDMN1
????????? MOV??? A,M
????????? SWAP?? A
????????? RR???? A
????????? CLR??? C
????????? MOV??? R7,A
IBDMN2?? :MOV??? A,R2
????????? MOV??? R0,A
????????? MOV??? A,R3
????????? MOV??? R1,A
????????? MOV??? B,M
IBDMN3?? :MOV??? A,@R1
????????? RLC??? A
????????? MOV??? @R1,A
????????? INC??? R1
????????? DJNZ?? B,IBDMN3
????????? MOV??? B,N
IBDMN4?? :MOV??? A,@R0
????????? ADDC?? A,@R0
????????? DA???? A
????????? JNB??? ACC.4,IBDMN5
????????? SETB?? C
????????? CLR??? ACC.4
IBDMN5?? :MOV??? @R0,A
????????? INC??? R0
????????? DJNZ?? B,IBDMN4
????????? DJNZ?? R7,IBDMN2
????????? MOV??? A,R2
????????? MOV??? R0,A
????????? RET
評(píng)論
查看更多