EASy68K
http://www.easy68k.com/EASy68Kforum/

Finding the square root.
http://www.easy68k.com/EASy68Kforum/viewtopic.php?f=6&t=1539
Page 1 of 1

Author:  Shawn565 [ Sun Mar 06, 2016 8:51 pm ]
Post subject:  Finding the square root.

I want to know how can you find the square root of a number.

For example: If I want the square root of 72.

How would I go on about doing this.

Any help is appreciated

Thanks.

Author:  Obeisance [ Sat Mar 12, 2016 1:36 am ]
Post subject:  Re: Finding the square root.

Let me preface by saying I'm very much a newbie. So much so that I have not written M68k code yet (I have only read it). I have also only thought in integers (so division is a new game for me).

An iterative strategy may be necessary if you don't have access to higher math functions:

https://en.wikipedia.org/wiki/Nth_root_algorithm

With this algorithm, you could perform a square root by combining multiplication, division and addition. Somewhere there is learning material about the packed decimal storage strategies which allow for non-integer numbers too.

example:
Code:
;put the value we are interested in finding the square root of in D0
CLR.L D1
CLR.L D2
CLR.L D3
;use 1 as the iteration difference tolerance -> when the difference between loop results is smaller than this, we're done
MOVE.L #1,D1  ;we need a first guess for the square root value (this is x_k)

loopStart
MOVE.L D0,D3  ;store a temporary version of the number which we are finding the square root of
DIVU.L D1,D3  ;D3 contains -> refer to wiki article: A/x      (the remainder is discarded)
ADD.L D1,D3  ;D3 now contains -> refer to wiki article: (x+A/x)
DIVU.L #2,D3 ;D3 now contains -> refer to wiki article: 1/2*(x+A/x) -> this is our next guess for the square root, x_(k+1)
MOVE.L D3,D2 ;temporarily store our guess in D2
SUB.L D1,D2 ;D2 contains the difference between the last sqrt guess, in D1, and the new sqrt guess, in D3
MOVE.L D3,D1 ;get ready for the next iteration
CMP.L #1,D2 ; D2 minus 1 -> set condition codes
BHI loopStart ;if D2 > #1, we are outside our tolerance so we loop again
;when finished, D3 and D1 both contain the square root


This code is quite daft since it uses too many registers for such a simple operation. It could even be outright wrong. Let me know what you think. I have not practiced M68k code yet. I'm only answering so that you don't get complete silence.

Author:  gtippery [ Fri Dec 08, 2017 10:57 am ]
Post subject:  Re: Finding the square root.

There's a square-root routine in this example I wrote for RosettaCode:
http://rosettacode.org/wiki/Sieve_of_Eratosthenes#68000_Assembly

Page 1 of 1 All times are UTC
Powered by phpBB® Forum Software © phpBB Group
http://www.phpbb.com/