本帖最后由 DownCloud 于 2011-10-10 18:46 编辑
2字节数B15,B14..B0=(B15B14..B8)*256+(B9B8..B0)
除以250就可以较快速的通过移位算出来,此方法可以推广到除以一个 靠近256,512,1024。。。等常数的算法
;下面给一个自己写的特殊算式
;计算的范围为:1×1×1/1000~9999×127×51/1000
;入口:(R45 R1 R2)出口:R67
;--------------------------------------------------------------------------
;黄 ,20110120
; 优化,20110126
;T=175..211(3 Loops)
;方法:2字节数B15,B14..B0=(B15B14..B10)*2^10+(B9B8..B0)=(B15B14..B10)*1024+(B9B8..B0)
;=(B15B14..B10)*1000+(B15B14..B10)*24+(B9B8..B0)
;除以1000后=(B15B14..B10)+((B15B14..B10)*24+(B9B8..B0))/1000=(B15B14..B10)
;--------------------------------------------------------------------------
Huan1: Acall R45Div1KC0 ;R3:=R45 Div 1000, R45:=R45 Mod 1000
Mov B,R1 ;R45:=R45*R1
Mul AB
Xch A,R5
Mov B,R1
Mul AB
Xch A,R5
Add A,B
Mov R4,A
Mov A,R3 ;R67:=R3*R1*R2
Mov B,R1
Mul AB
Mov R3,B
Mov B,R2
Mul AB
Mov R6,B
Mov R7,A
Mov A,R3
Mov B,R2
Mul AB
Add A,R6
Mov R6,A
Acall R45Div1K
Mov A,R3 ;R67:+R3*R2
Mov B,R2
Mul AB
Add A,R7
Mov R7,A
Mov A,B
Addc A,R6
Mov R6,A
Mov A,R5 ;R45:+R45*R2
Mov B,R2
Mul AB
Mov R5,A
Mov A,R4
Mov R4,B
Mov B,R2
Mul AB
Add A,R4
Mov R4,A
Clr A
Addc A,B
Mov C,Acc.0 ;Highest Byte can only be 1 or 0, in C
ACall R45Div1K
Mov A,R3 ;R67:+R3
Add A,R7
Mov R7,A
Clr A
Addc A,R6
Mov R6,A
;R45=(b15...b9)*500+(b15...b9)*12+(b8...b0)
Mov A,#12 ;.R45:=R45+12
Add A,R5
Clr A
Addc A,R4
Rrc A ;get (b15...b9)
Jz NoInc ;(b15...b9) of R45 >0 then .R45>=512 then R45>=500
Inc R7 ;R45>=500 then Inc R67
Cjne R7,#0,NoInc
Inc R6
NoInc: Ret
;--------------------------------------------------------------------------
;R3:=R45 Div 1000, R45:=R45 Mod 1000
;=(B15B14..B10)*1000+(B15B14..B10)*24+(B9B8..B0)
;R45/1000=(B15B14..B10)+((B15B14..B10)*24+(B9B8..B0))/1000
;--------------------------------------------------------------------------
R45Div1KC0: Clr C ;R3:=B15B14..B10 of R45
R45Div1K: Mov A,R4
Rrc A
Clr C
Rrc A
Mov R3,A
Mov B,#(1024-1000) ;R45:=R45(B9B8..B0)+R3:*(1024-1000)
Mul AB
Add A,R5
Mov R5,A
Mov A,R4
Anl A,#00000011B
Addc A,B
R45Div1KLp: Mov R4,A
Cjne A,#4,$+3 ;If B15B14..B10 of R45>0 then R45>1000
Jc R45Div1KQ
Inc R3 ;R3:+1, R45:-1000
Xch A,R5 ;C=0, For MaxR3(3F)*24+MaxR45(3FF)=9E7<10000H
Subb A,#Low(1000)
Xch A,R5
Subb A,#High(1000)
Sjmp R45Div1KLp ;Tested Max 3 Loops, 20110126, at 4412*86*51
R45Div1KQ: Ret |