It is currently Mon Feb 17, 2020 11:40 pm

 All times are UTC

 Page 1 of 1 [ 3 posts ]
 Print view Previous topic | Next topic
Author Message
 Post subject: Finding the square root.Posted: Sun Mar 06, 2016 8:51 pm

Joined: Thu Feb 18, 2016 7:54 pm
Posts: 3
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.

Top

 Post subject: Re: Finding the square root.Posted: Sat Mar 12, 2016 1:36 am

Joined: Sat Jan 09, 2016 9:58 pm
Posts: 22
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.

Top

 Post subject: Re: Finding the square root.Posted: Fri Dec 08, 2017 10:57 am

Joined: Tue Nov 15, 2005 7:58 am
Posts: 129
Location: Oregon, USA
There's a square-root routine in this example I wrote for RosettaCode:
http://rosettacode.org/wiki/Sieve_of_Eratosthenes#68000_Assembly

Top

 Display posts from previous: All posts1 day7 days2 weeks1 month3 months6 months1 year Sort by AuthorPost timeSubject AscendingDescending
 Page 1 of 1 [ 3 posts ]

 All times are UTC

#### Who is online

Users browsing this forum: No registered users and 8 guests

 You cannot post new topics in this forumYou cannot reply to topics in this forumYou cannot edit your posts in this forumYou cannot delete your posts in this forumYou cannot post attachments in this forum

Search for:
 Jump to:  Select a forum ------------------ EASy68K Forum    FAQ    Latest Features    EASy68K Questions    68000 Programming Questions    Wish List    Wishes Granted    Undocumented Features    Documented Features    General Discussion    EASy68K Projects