贴代码吧,高位和低位都有计算不正确的。
附keil的除法库。
C?UIDIV:
CJNE R4,#0x00,UIDIV_U8_16
CJNE R6,#0x00,UIDIV_U16_16
MOV A,R7
MOV B,R5
DIV AB
MOV R7,A
MOV R5,B
RET
UIDIV_U8_16:
CLR A
XCH A,R4
MOV R0,A
MOV B,#0x08
UIDIV_U8_16_loop:
MOV A,R7 ;R6R7保存被除数和结果,逐位左移。
ADD A,R7
MOV R7,A
MOV A,R6
RLC A
MOV R6,A
MOV A,R4
RLC A
MOV R4,A
MOV A,R6
SUBB A,R5
MOV A,R4
SUBB A,R0
JC UIDIV_U8_16_NotAddOne
MOV R4,A
MOV A,R6
SUBB A,R5
MOV R6,A
INC R7
UIDIV_U8_16_NotAddOne:
DJNZ B,UIDIV_U8_16_loop
CLR A
XCH A,R6
MOV R5,A
RET
UIDIV_U16_16:
MOV A,R5
MOV R0,A
MOV B,A
MOV A,R6
DIV AB
JB OV,UIDIV_U16_16_End
MOV R6,A
MOV R5,B
MOV B,#0x08
UIDIV_U16_16_Loop:
MOV A,R7
ADD A,R7
MOV R7,A
MOV A,R5
RLC A
MOV R5,A
JC UIDIV_U16_16_Big1
SUBB A,R0
JNC UIDIV_U16_16_Big2
DJNZ B,UIDIV_U16_16_Loop
RET
UIDIV_U16_16_Big1:
CLR C
SUBB A,R0
UIDIV_U16_16_Big2:
MOV R5,A
INC R7
DJNZ B,UIDIV_U16_16_Loop
UIDIV_U16_16_End:
RET
|