< >;浮点运算子程序库
;整理:聂小猛。该资料来自“51单片机世界”http://go.163.com/~dz2000,欢迎访问。
;1,FSDT浮点数格式化 F[R0]->[RO]GeShiHua *
;2,FADD浮点数加法 F[R0]+[R1]=[R0],OV
;3,FSUB浮点数减法 F[R0]-[R1]=[R0],OV
;4,FMUL浮点数乘法 F[R0]x[R1]=[R0],OV
;5,FDIV浮点数除法 F[R0]/[R1]=R0,OV
;6,FSQR浮点数开平方 /--F[R0]=[R0],OV
;7,FPLN浮点数多项式 F[R0]DuoXiangShi=[R0],OV
;8,FABS浮点数绝对值 |F[R0]|=[R0] *COS
;9,FSGN浮点数符号 [R0] A=1>+,A=0FFH>-,A=0 *
;10,FINT浮点取整函数 F[R0]QuZheng *SIN
;11,FRCP浮点倒数函数 1/F[R0]=R0,OV *arcctg
;12,FLOG以10为底对数函数LogF[R0]=[R0],OV
;13,FLN以e为底对数函数 LnF[R0]=[R0],OV
;14,FEXP10以10为底指数函数10`[R0]=[R0],OV
;15,FEXP以e为底指数函数 e`[R0]=[R0],OV
;16,FEXP2以2为底指数函数2`[R0]=[R0],OV
;17,FCOS浮点余弦函数 COSF[R0]=[R0]
;18,FSIN浮点正弦函数 SINF[R0]=[R0]
;19,FATN浮点正切函数 arcctgF[R0]=[R0]
;24,BCDTOF浮点bcd转格式化BCDf[R0]=F[R0]
;25,FTOBCD格式化转浮点 bcd F[R0]=BCDF[R0]
;26,RTOD浮点弧度数转度数HuF[R0]=DuF[R0]
;27,DTOR浮点度数转弧度数DuF[R0]=HuF[R0] </P>
< >
;1
;F[R0]->[RO]GeShiHua
SW,A,R2,R3,R4,2FH.7;6
FSDT: LCALL MOVR0 ;;
LCALL RLN
LJMP MOV0
;2
;F[R0]+[R1]=[R0],OV
;PSW,A,R2-R7,2FH.0,2FH.7;6
FADD: CLR F0
SJMP AS ;;
;3
;F[R0]-[R1]=[R0],OV
;PSW,A,R2-R7,2FH.0,2FH.7;6
FSUB: SETB F0
AS: LCALL MOVR1
MOV C,F0
RRC A
XRL A,@R1
MOV C,ACC.7
ASN: MOV 2FH.0,C
XRL A,@R0
RLC A
MOV F0,C
AS0: LCALL MOVR0
LCALL AS1</P>
<P>MOV0: INC R0
INC R0
MOV A,R4
MOV @R0,A
DEC R0
MOV A,R3
MOV @R0,A
DEC R0
MOV A,R2
MOV C,2FH.7
MOV ACC.7,C
MOV @R0,A
CLR ACC.7
XRL A,#3FH
JNZ MOV01
SETB OV
RET
MOV01: MOV A,@R0
CLR OV
RET</P>
<P>MOVR0: MOV A,@R0
MOV C,ACC.7
MOV 2FH.7,C
MOV C,ACC.6
MOV ACC.7,C
MOV R2,A
INC R0
MOV A,@R0
MOV R3,A
INC R0
MOV A,@R0
MOV R4,A
DEC R0
DEC R0
RET</P>
<P>MOVR1: MOV A,@R1
MOV C,ACC.7
MOV 2FH.0,C
MOV C,ACC.6
MOV ACC.7,C
MOV R5,A
INC R1
MOV A,@R1
MOV R6,A
INC R1
MOV A,@R1
MOV R7,A
DEC R1
DEC R1
RET</P>
<P>AS1: MOV A,R6
JZ AS2
MOV A,R3
JNZ EQ
MOV A,R6
MOV R3,A
MOV A,R7
MOV R4,A
MOV A,R5
MOV R2,A
MOV C,2FH.0
MOV 2FH.7,C
AS2: RET
EQ: MOV A,R2
XRL A,R5
JZ AS4
JB ACC.7,EQ3
MOV A,R2
CLR C
SUBB A,R5
JC EQ4
EQ2: CLR C
MOV A,R6
RRC A
MOV R6,A
MOV A,R7
RRC A
MOV R7,A
INC R5
ORL A,R6
JNZ EQ
MOV A,R2
MOV R5,A
SJMP AS4
EQ3: MOV A,R2
JNB ACC.7,EQ2
EQ4: CLR C
LCALL RR1
ORL A,R3
JNZ EQ
MOV A,R5
MOV R2,A
AS4: JB F0,AS5
MOV A,R4
ADD A,R7
MOV R4,A
MOV A,R3
ADDC A,R6
MOV R3,A
JNC AS2
LJMP RR1
AS5: CLR C
MOV A,R4
SUBB A,R7
MOV B,A
MOV A,R3
SUBB A,R6
JC AS6
MOV R4,B
MOV R3,A
LJMP RLN
AS6: CPL 2FH.7
CLR C
MOV A,R7
SUBB A,R4
MOV R4,A
MOV A,R6
SUBB A,R3
MOV R3,A
RLN: MOV A,R3
ORL A,R4
JNZ RLN1
MOV R2,A
RET
RLN1: MOV A,R3
JB ACC.7,RLN2
CLR C
LCALL RL1
SJMP RLN1
RLN2: CLR OV
RET
RL1: MOV A,R4
RLC A
MOV R4,A
MOV A,R3
RLC A
MOV R3,A
DEC R2
MOV A,R2
XRL A,#0C0H
JNZ RL1E
MOV R2,A
MOV R3,A
MOV R4,A
RL1E: CLR OV
RET
RR1: MOV A,R3
RRC A
MOV R3,A
MOV A,R4
RRC A
MOV R4,A
INC R2
CLR OV
MOV A,R2
XRL A,#40H
JNZ RR1E
MOV R2,#3FH
SETB OV
RR1E: RET
;4
;F[R0]x[R1]=[R0],OV
;PSW,A,B,R2-R7,2FH.0,2FH.7;6
FMUL: LCALL MOVR0
MOV A,@R0
XRL A,@R1
RLC A
MOV 2FH.7,C
LCALL MUL0
LJMP MOV0
MUL0: LCALL MOVR1
MUL1: MOV A,R3
JZ MUL5
MOV A,R6
JZ MUL4
MULD: MOV A,R7
MOV B,R4
MUL AB
MOV A,B
XCH A,R7
MOV B,R3
MUL AB
ADD A,R7
MOV R7,A
CLR A
ADDC A,B
XCH A,R4
MOV B,R6
MUL AB
ADD A,R7
MOV R7,A
MOV A,B
ADDC A,R4
MOV R4,A
CLR A
RLC A
XCH A,R3
MOV B,R6
MUL AB
ADD A,R4
MOV R4,A
MOV A,B
ADDC A,R3
MOV R3,A
MOV A,R7
RLC A
MOV A,R3
JB ACC.7,MUL2
LCALL RL1
MUL2: MOV A,R2
ADD A,R5
MD: MOV R2,A
JB ACC.7,MUL3
JNB ACC.6,MUL5
MOV R2,#3FH
SETB OV
RET
MUL3: JB ACC.6,MUL5
MUL4: CLR A
MOV R2,A
MOV R3,A
MOV R4,A
MUL5: CLR OV
RET</P>
<P>;5
;F[R0]/[R1]=R0,OV
;PSW,A,B,R2-R7,2FH.0,2FH.7;5
FDIV: INC R0
MOV A,@R0
DEC R0
JNZ DIV1
CLR OV
RET
DIV1: INC R1
MOV A,@R1
DEC R1
JNZ DIV2
SETB OV
RET
DIV2: LCALL MOVR0
MOV A,@R0
XRL A,@R1
RLC A
MOV 2FH.7,C
LCALL MOVR1
DIV3: CLR C
MOV A,R4
SUBB A,R7
MOV A,R3
SUBB A,R6
JC DIV4
LCALL RR1
DIV4: CLR A
XCH A,R0
PUSH ACC
CLR A
XCH A,R1
PUSH ACC
MOV A,R2
PUSH ACC
MOV B,#10H
DIVD1: CLR C
MOV A,R1
RLC A
MOV R1,A
MOV A,R0
RLC A
MOV R0,A
MOV A,R4
RLC A
MOV R4,A
XCH A,R3
RLC A
XCH A,R3 ;;;;;;;
MOV F0,C
CLR C
SUBB A,R7
MOV R2,A
MOV A,R3
SUBB A,R6
ANL C,/F0
JC DIVD2
MOV R3,A
MOV A,R2
MOV R4,A
INC R1
DIVD2: DJNZ B,DIVD1
MOV A,R6
CLR C
RRC A
SUBB A,R3
CLR A
ADDC A,R1
MOV R4,A
CLR A
ADDC A,R0
MOV R3,A
POP ACC
MOV R2,A
POP ACC
MOV R1,A
POP ACC
MOV R0,A
MOV A,R2
CLR C
SUBB A,R5
LCALL MD
LJMP MOV0
;6
;/--F[R0]=[R0],OV
;PSW,A,B,R2-R7;2
FSQR: MOV A,@R0
JNB ACC.7,SQR
SETB OV
RET
SQR: MOV C,ACC.6
MOV ACC.7,C
INC A
CLR C
RRC A
MOV @R0,A
INC R0
JC SQR0
MOV A,@R0
RRC A
MOV @R0,A
INC R0
MOV A,@R0
RRC A
MOV @R0,A
DEC R0
SQR0: MOV A,@R0
JZ SQR9
MOV R2,A
INC R0
MOV A,@R0
MOV R3,A
MOV A,R2
ADD A,#57H
JC SQR2
ADD A,#45H
JC SQR1
ADD A,#24H
MOV B,#0E3H
MOV R4,#80H
SJMP SQR3
SQR1: MOV B,#0B2H
MOV R4,#0A0H
SJMP SQR3
SQR2: MOV B,#8DH
MOV R4,#0D0H
SQR3: MUL AB
MOV A,B
ADD A,R4
MOV R4,A
MOV B,A
MUL AB
XCH A,R3
CLR C
SUBB A,R3
MOV R3,A
MOV A,B
XCH A,R2
SUBB A,R2
MOV R2,A
SQR4: SETB C
MOV A,R4
RLC A
MOV R6,A
CLR A
RLC A
MOV R5,A
MOV A,R3
SUBB A,R6
MOV B,A
MOV A,R2
SUBB A,R5
JC SQR5
INC R4
MOV R2,A
MOV R3,A
SJMP SQR4
SQR5: MOV A,R4
XCH A,R2
RRC A
MOV F0,C
MOV A,R3
MOV R5,A
MOV R4,#8
SQR6: CLR C
MOV A,R3
RLC A
MOV R3,A
CLR C
MOV A,R5
SUBB A,R2
JB F0,SQR7
JC SQR8
SQR7: MOV R5,A
INC R3
SQR8: CLR C
MOV A,R5
RLC A
MOV R5,A
MOV F0,C
DJNZ R4,SQR6
MOV A,R3
MOV @R0,A
DEC R0
MOV A,R2
MOV @R0,A
SQR9: DEC R0
CLR OV
RET
;7
;F[R0]DuoXiangShi=[R0],OV
;PSW,A,B,R2-R7,2FH.0.7;4
FPLN: POP DPH
POP DPL
XCH A,R0
XCH A,R1
XCH A,R0
CLR A
MOV R2,A
MOV R3,A
MOV R4,A
CLR 2FH.7
PLN1: CLR A
MOVC A,@A+DPTR
MOV C,ACC.7
MOV 2FH.0,C
MOV C,ACC.6
MOV ACC.7,C
MOV R5,A
INC DPTR
CLR A
MOVC A,@A+DPTR
MOV R6,A
INC DPTR
CLR A
MOVC A,@A+DPTR
MOV R7,A
INC DPTR
MOV C,2FH.0
RRC A
XRL A,2FH
RLC A
MOV F0,C
LCALL AS1
CLR A
MOVC A,@A+DPTR
CJNE A,#40H,PLN2
XCH A,R0
XCH A,R1
XCH A,R0
LCALL MOV0
CLR A
INC DPTR
JMP @A+DPTR
PLN2: MOV A,@R1
XRL A,2FH
RLC A
MOV 2FH.7,C
LCALL MUL0
SJMP PLN1
;8
;|F[R0]|=[R0]
;A;2
FABS: MOV A,@R0
CLR ACC.7
MOV @R0,A
RET
;9
;[R0] A=1>+,A=0FFH>-,A=0>0
;PSW,A;2
FSGN: MOV A,@R0
MOV C,ACC.7
JNZ SGN1
INC R0
MOV A,@R0
DEC R0
JZ SGN2
SGN1: MOV A,#1
JNC SGN2
MOV A,#0FFH
SGN2: RET
;10
;F[R0]QuZheng
;PSW,A,R2,R3,R4,2FH.7;6
FINT: LCALL MOVR0
LCALL INT
LJMP MOV0
INT: MOV A,R3
ORL A,R4
JNZ INTA
MOV R2,A
RET
INTA: MOV A,R2
JZ INTB
JB ACC.7,INTB
CLR C
SUBB A,#10H
JC INTC
RET
INTB: CLR A
MOV R4,A
MOV C,2FH.7
RRC A
MOV R3,A
RL A
MOV R2,A
RET
INTC: CLR F0
INTD: CLR C
LCALL RR1
ORL C,F0
MOV F0,C
CJNE R2,#16,INTD
JNB F0,INTE
JNB 2FH.7,INTE
INC R4
MOV A,R4
JNZ INTE
INC R3
INTE: LJMP RLN
;11
;1/F[R0]=R0,OV
;PSW,A,B,R2-R7,2FH.0.7;5
FRCP: MOV A,@R0
MOV C,ACC.7
MOV 2FH.7,C
MOV C,ACC.6
MOV ACC.7,C
MOV R5,A
INC R0
MOV A,@R0
JNZ RCP
DEC R0
SETB OV
RET
RCP: MOV R6,A
INC R0
MOV A,@R0
MOV R7,A
DEC R0
DEC R0
MOV R2,#1
MOV R3,#80H
MOV R4,#0
LJMP DIV3
;12
;LogF[R0]=[R0],OV
;PSW,A,B,R2-R7,2FH.0.7;9
FLOG: LCALL FLN
JNB OV,LOG
RET
LOG: MOV R5,#0FFH
MOV R6,#0DEH
MOV R7,#5CH
LCALL MUL1
LJMP MOV0
;13
;LnF[R0]=[R0],OV
;PSW,A,B,R2-R7,2FH.0.7;7
FLN: LCALL MOVR0
JB 2FH.7,LNOV
MOV A,R3
JNZ LN0
LNOV: SETB OV
RET
</P> |