Vypocet odmocniny

MK info
Středa Březen 17 14:28:22 CET 2004


 > Cau
 > muzete mi poradit jak na uP realizovat vypocet druhe odmocniny z urciteho
> cisla ? Potrebuji to s presnosti na jednotky.
 >
 > Milos

Snad to pomuze, je to pro x51

S pozdravem
MK

http://bazar.mcu.cz
http://www.mcu.cz


;SQROOT1 is called to calculate the square root of a 32-bit number.

;

;Call:

;

;     r0 => LSB of 32-bit input

;     lcall SQROOT1

;

;Return:

;

; computed square root is in acc(LSB's) and b(MSB's)

; (root is also in est and est+1)

;

;SQROOT1 uses the formula

;

; estimate = (last_estimate + input/last_estimate)/2

;

;Method is described as Newton's, Newton-Raphson, and Babylonian method.

;DIV32U is called to do the 32/16-bit division.

;SQROOT1 perfoms a fixed number of iterations to converge to the root.

;

;SQROOT1 will destroy all registers except r0, dptr.

;

;Original Author: John Veazey, Ridgecrest, CA, 18 Apr 99

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;

dseg

est:  ds    2     ;The estimated value being developed

sqrcnt: ds  1     ;Counts iterations

cseg

SQROOT1:

;

;Save r0 on the stack

;

      mov   a,r0

      push  acc

;

;Initialize the iteration counter

;

      mov   sqrcnt,#7   ;There will be seven calculations

;

;Find the MSB set in input, call it n as in 2**n. Calculate

; r = ceiling(n+1)/2, then set up the first estimate to

; be (2**r)-1.

;

      inc   r0

      inc   r0

      inc   r0

      mov   a,@r0       ;Is MS byte of input not-zero?

      jz    sqr112

      mov   r1,#33      ; Yes, set r1 to the bit number+2

      sjmp  sqr140

sqr112:

      dec   r0          ; No,

      mov   a,@r0       ;Is 2nd MS byte of input not-zero?

      jz    sqr114

      mov   r1,#25      ;  Yes

      sjmp  sqr140

sqr114:

      dec   r0

      mov   a,@r0       ;Is 3rd MS byte of input not-zero?

      jz    sqr116

      mov   r1,#17      ;  Yes

      sjmp  sqr140

sqr116:

      dec   r0

      mov   est+1,#0

      mov   a,@r0       ;Is LS byte of input zero?

      jnz   sqr122

      mov   est,#0      ; Yes, return with a zero

      ljmp  sqr900      ;  because input is zero

sqr122:

      mov   est,#1

      dec   a           ;Is LS byte of input = 1?

      jz    sqr900      ; Yes, return with a one

      inc   a           ; No

      mov   r1,#9

sqr140:

      dec   r1

      rlc   a

      jnc   sqr140

      mov   a,r1        ;Form ceiling[(n+1)/2]

      clr   c

      rrc   a           ; (divide by 2, add remainder)

      jnc   sqr142

      inc   a

sqr142:

      mov   r1,a

      clr   a

      mov   est+1,a

sqr144:

      setb  c           ;Get the 1 to shift in

      rlc   a

      mov   b,a

      mov   a,est+1

      rlc   a

      mov   est+1,a

      mov   a,b

      dec   r1

      cjne  r1,#0,sqr144

      mov   est,a

;

;Load input into DIV32U dividend register

;

sqr200:

      pop   acc

      push  acc

      mov   r0,a

      mov   a,@r0

      mov   r4,a

      inc   r0

      mov   a,@r0

      mov   r5,a

      inc   r0

      mov   a,@r0

      mov   r6,a

      inc   r0

      mov   a,@r0

      mov   r7,a

;

;Load last estimate into DIV32U divisor registers

;

      mov   a,est

      mov   r0,a

      mov   a,est+1

      mov   r1,a

;

;Call DIV32U to do the 32/16-bit division (input/est)

;

      lcall DIV32U      ;(r7,r6,r5,r4)/(r1,r0) = (r5,r4)

      jnc   sqr310      ;If overflow, set to maximum number

      mov   r4,#-1

      mov   r5,#-1

;

;Add the last estimate to the quotient

;

sqr310:

      mov   a,r4

      add   a,est       ;sets c

      mov   est,a

      mov   a,est+1

      addc  a,r5        ;sets c

;

;Divide sum by 2 and save as new estimate

;

      rrc   a           ;sets c

      mov   est+1,a

      mov   a,est

      rrc   a           ;c discarded

      mov   est,a

;

;Decrement the iteration counter and repeat if not zero

      djnz  sqrcnt,sqr200

;

;Make the normal return

;

sqr900:

      pop   acc         ;Restore caller's pointer

      mov   r0,a

      mov   b,est+1     ;Get answer in (b,a)

      mov   a,est

      ret

;end of SQROOT1








Další informace o konferenci Hw-list