Hi!

> I'm a newbie to Assembler programming..

> I would like to know where you can get simple and good tutorials for free?

> And how do you code a simple program which solves the squareroot from three

> positive numbers and prints it on the result on the screen?? (I'm using

> MASM)

>

> For example:

> c:\root.exe

> Give a integer: 121

> The squareroot is: 11

> c:\

> I'm a newbie to Assembler programming..

> I would like to know where you can get simple and good tutorials for free?

> And how do you code a simple program which solves the squareroot from three

> positive numbers and prints it on the result on the screen?? (I'm using

> MASM)

>

> For example:

> c:\root.exe

> Give a integer: 121

> The squareroot is: 11

> c:\

**Newbie**, there plenty of resources on the web for this very problem. If your not going to do your homework yourself, you should atleast learn how to do research enough to use someone else's code. Try: http://www.google.com For search words try: square root MASM assembly :)

Try guess and check:

- [*]Pick a number[*]Multiply number by itself - square it[*]Test if number is similar to target number

[*]Change guessed number based on comparison[*]Goto 2 if not done

bitRAKE, are you telling him to use those steps in order to calculate the square root or I misunderstoond what you said?

If i did not misunderstand those steps are pretty bad, the calculation would be slow and maybe almost impossible. Imagine square root of 2!!!

If i misunderstood, sorry. ;)

If i did not misunderstand those steps are pretty bad, the calculation would be slow and maybe almost impossible. Imagine square root of 2!!!

If i misunderstood, sorry. ;)

**CodeLover**, those steps work if you program Step 5 correctly. ;) There is going to be some rounding. Yes, it would be slow - I didn't say it would be fast and

**Newbie**didn't ask for a fast routine.

In square root of 25, that will work slowly but correctly. But i do not think square root of 2 would be so precise. Imagine:

Pick a number

Multiply number by itself - square it

Test if number is similar to target number

Change guessed number based on comparison

Goto 2 if not done

Steps:

1

eax=1*1

cmp eax,2

Change the number to what? 1.1?

No, not equal. You arrive later, after some seconds, to 1.4 Is 1.4 equal to 2. No, it is not. What does your program do? He realizes that 1.4^2 equals 1.96 and that is a short distance so he makes the 4 remains after the point and try to find another digit following the 4. Or does your program now searches to 1.5, and when realizes that he has passed comes back to 4 and looks for the new digit? Or does your program just makes calculations with only one digit after the point? Not only it would be slow, but also imprecise. In a few words, it would be totally unusufulness.

Pick a number

Multiply number by itself - square it

Test if number is similar to target number

Change guessed number based on comparison

Goto 2 if not done

Steps:

1

eax=1*1

cmp eax,2

Change the number to what? 1.1?

No, not equal. You arrive later, after some seconds, to 1.4 Is 1.4 equal to 2. No, it is not. What does your program do? He realizes that 1.4^2 equals 1.96 and that is a short distance so he makes the 4 remains after the point and try to find another digit following the 4. Or does your program now searches to 1.5, and when realizes that he has passed comes back to 4 and looks for the new digit? Or does your program just makes calculations with only one digit after the point? Not only it would be slow, but also imprecise. In a few words, it would be totally unusufulness.

BitRake's technique actually works a lot faster than you would think!!!

B

B

I love that site :)

Helped alot for that division question I had :) Thanks again for that link BitRACK

Sliver

Helped alot for that division question I had :) Thanks again for that link BitRACK

Sliver

I love that site :)

Helped alot for that division question I had :) Thanks again for that link BitRACK

Sliver

__BitRACK__?

I've always taken it as a deep form of affection. :grin:

Doesn't this strike you as home work?

Who in their right mind would honestly want to program somthing that gives the square root of an inputted number?

What shall I do today? I could make a 3D engine, or some app, or maybe challenge a friend to a game of hearts over the internet.... No I will do something that to others looks very much like homework!

As for calling you BitRACK, is that like a spice rack, except for bits?

Mirno

Who in their right mind would honestly want to program somthing that gives the square root of an inputted number?

What shall I do today? I could make a 3D engine, or some app, or maybe challenge a friend to a game of hearts over the internet.... No I will do something that to others looks very much like homework!

As for calling you BitRACK, is that like a spice rack, except for bits?

Mirno

Doesn't this strike you as home work?

Who in their right mind would honestly want to program somthing that gives the square root of an inputted number?

What shall I do today? I could make a 3D engine, or some app, or maybe challenge a friend to a game of hearts over the internet.... No I will do something that to others looks very much like homework!

As for calling you BitRACK, is that like a spice rack, except for bits?

Mirno

Honestly I've never noticed it... :grin:

I guess it's my quick typing skills :) Sorry...

I do like the Bit/Spice rack analogy...

Please except my appology...

I'm sure I'll get it right next time... Although I've noticed that I've written that alias down so many times that I've grown used to it...

I have to go check my source code(s) where I give you credit... I have to make sure they are correct too...

Silver (what people used to type for me)

ps. Just wanted to see how far this went :)

called you BitRack more than once :(

----EDIT----

Just wanted to say...

I'm SO sorry :) ...

I guess it's my quick typing skills :) Sorry...

I do like the Bit/Spice rack analogy...

Please except my appology...

I'm sure I'll get it right next time... Although I've noticed that I've written that alias down so many times that I've grown used to it...

I have to go check my source code(s) where I give you credit... I have to make sure they are correct too...

Silver (what people used to type for me)

ps. Just wanted to see how far this went :)

called you BitRack more than once :(

----EDIT----

Just wanted to say...

I'm SO sorry :) ...

OK, coming back to the topic... :tongue:

Yes, bitRAKE, there are no doubts that the method you are talking about is not bad :) . Actually it is not fast, but it is useful, sorry by saying it was not, i was just asking ;). Maybe my way of asking was arrogant, but i am not. It is just that i wanted to know more about that, and i made that pertinent questions. I think that is called the Socratic method. Anyways, Socrates, as the history records, was an example person. (maybe i am not) :grin:

But there are not doubts that your method is slow. So, in that case, i think this could be a bit faster:

Yes, bitRAKE, there are no doubts that the method you are talking about is not bad :) . Actually it is not fast, but it is useful, sorry by saying it was not, i was just asking ;). Maybe my way of asking was arrogant, but i am not. It is just that i wanted to know more about that, and i made that pertinent questions. I think that is called the Socratic method. Anyways, Socrates, as the history records, was an example person. (maybe i am not) :grin:

But there are not doubts that your method is slow. So, in that case, i think this could be a bit faster:

[*]Factorize in prime factors the number.

[*]Simplify roots.

[*]Operate numbers.

For example, you wanna do the square root of 75.

First step:

75 | 3

25 | 5

5 | 5

1 |

Second step:

As you see you have: square_root (5*5)*square_root(3)

So, simplifying roots now it is 5 * square_root(3).

Third step:

Operate the numbers. Here you have to do that 5 * square_root(3). The square root of 3 can be calculated using the method you posted, or have a table of some prime numbers roots. That would make the program a bit larger, but could be useful to have the square roots of the prime numbers from 2 to 29, for example. Or use your method to the calculation of these roots, as your method is appropiated for prime numbers.

I think with the combination of the two methods you can achieve a good speed. I think the method posted here is fast, and can help you solve the roots for big numbers. Then you will have roots of prime numbers, and there use your method to their calculation and/or using a table that contains the value of the little numbers such as 2, 3, etc, that also almost always appear in factorization.

Here is another example:

square_root(2576)

2576 | 2

1288 | 2

644 | 2

322 | 2

161 | 7

23 | 23

1 |

Now there is:

**square_root(2*2)*square_root(2*2)*square_root(7)*square_root(23)**

So, that is the same as:

**4*square_root(7)*square_root(23)**

Now your method can be applied to obtain the values for the remaining square roots, or they can be in the little table. Any method you use will give you this:

**4* 2,64575* 4,79583**

That is equal to

**50,75428**.

```
```

fild myInteger

fsqrt

fist myResult

If the rounding mode is set to crop.

BTW:

**bitRAKE**You never cease to amaze me with the ammount of links you have found. :alright:

search for the word **google**in this board and most of them are probably posted by

usually when I have a question in mind, i can imagine either of them saying: gooogle... and so I do, and end up not posting my question here ;)

.pix

**f00der**and**bitRick**:grin:usually when I have a question in mind, i can imagine either of them saying: gooogle... and so I do, and end up not posting my question here ;)

.pix

**CodeLover**, it certainly depends on the bit of percision you need. How do you suggest we find the prime factorization!? I think you've just moved from one problem to another. ;)

**Sliver**, don't worry about it.

My real name is Rickey,

but I've been called a lot worse. :grin:

**dxantos**, thanks - I have a good memory and have see much.

Actually, there is a quite simple way of getting as many digits as needed for the square root of any given number, without using any guessing.

It goes like this:

Pick your favorite number.

Group the digits in groups of two starting at the decimal point and going both left and right. Notice that the 5 being a single digit is taken by itself.

Find the greatest number that squared is less than the first group of two digits. In this case: 2^2 = 4 < 5.

The 2 will be your first digit in the result.

Subtract the square of that number from the first group of digits. 5 - 2^2 = 1.

To that 1, append the next group of two digits => 140. Take all the numbers that are in the result so far and double it. 2 * 2 = 4

Write that below and find a digit such that 4x * x is the greatest result that is less than the 140. that digit x is the second digit of the result. subtract the result of the multiplication from 140 and append the next group of two digits. Repeat until you cry.

The decimal point goes after the same number of digits as there are groups to the left of the decimal point of the given number. 540.900000 has two groups of two digits to the left of the decimal, so 23 are two digits and the decimal follows the 23. Simple yet very tedious when done on paper.

This can be implemented for numbers of any length and it can all be computed without any floating point arithmetic. Actually, considering that usually the numbers in the multiplications get bigger as you go along, you will be limited by various things, unless you implement your own addition, subtraction, and multiplicaiton algorithms.

It goes like this:

Pick your favorite number.

Group the digits in groups of two starting at the decimal point and going both left and right. Notice that the 5 being a single digit is taken by itself.

Find the greatest number that squared is less than the first group of two digits. In this case: 2^2 = 4 < 5.

The 2 will be your first digit in the result.

Subtract the square of that number from the first group of digits. 5 - 2^2 = 1.

To that 1, append the next group of two digits => 140. Take all the numbers that are in the result so far and double it. 2 * 2 = 4

Write that below and find a digit such that 4x * x is the greatest result that is less than the 140. that digit x is the second digit of the result. subtract the result of the multiplication from 140 and append the next group of two digits. Repeat until you cry.

The decimal point goes after the same number of digits as there are groups to the left of the decimal point of the given number. 540.900000 has two groups of two digits to the left of the decimal, so 23 are two digits and the decimal follows the 23. Simple yet very tedious when done on paper.

This can be implemented for numbers of any length and it can all be computed without any floating point arithmetic. Actually, considering that usually the numbers in the multiplications get bigger as you go along, you will be limited by various things, unless you implement your own addition, subtraction, and multiplicaiton algorithms.

This is interesting

**Hel**. Could you convert the algo to binary?It's too late for me to figure out how to make full use of this, but:

sqr(10000b)=100b

Count 0's and shr by 1/2

It esentially amounts to log base 2 ops:

sqr(x)= log (x) / log (2)

For odd bits, shr 1, do op, and mul by sqr(2)

sqr(10000b)=100b

Count 0's and shr by 1/2

It esentially amounts to log base 2 ops:

sqr(x)= log (x) / log (2)

For odd bits, shr 1, do op, and mul by sqr(2)

I took a look at it, and I was easily able to obtain the square root of 10d in binary.

It works like a charm in any base, as long as you do all the math in that base.

In fact, I found it easier to do it in base 2. Because the x in my previous example would be only a 0 or a 1, the multiplication is kept very simple. You either get 0 or you get the number itself!

The only tough thing is converting the stuff after the decimal point to base 10 because you have to use negative exponents.

Sqr(1010b) = 11.00101b

11b = 1x2^1 + 1x2^0 = 3d

.00101 = 0x2^-1 + 0x2^-2 + 1x2^-3 + 0x2^-4 + 1x2^-5

.00101 = 0 + 0 + 1/8 + 0 + 1/32

.00101 = 5/32

.00101 = 0.15625

Sqr(10d) = 3.15625d

Your Windows calculator will definitely not do the binary decimals. It's too stupid. :)

Interestingly, if you shift the decimal point in the initial number by 2x either way and you shift the decimal in the result by x, you still get a good approximation of the new initial number.

It works like a charm in any base, as long as you do all the math in that base.

In fact, I found it easier to do it in base 2. Because the x in my previous example would be only a 0 or a 1, the multiplication is kept very simple. You either get 0 or you get the number itself!

The only tough thing is converting the stuff after the decimal point to base 10 because you have to use negative exponents.

Sqr(1010b) = 11.00101b

11b = 1x2^1 + 1x2^0 = 3d

.00101 = 0x2^-1 + 0x2^-2 + 1x2^-3 + 0x2^-4 + 1x2^-5

.00101 = 0 + 0 + 1/8 + 0 + 1/32

.00101 = 5/32

.00101 = 0.15625

Sqr(10d) = 3.15625d

Your Windows calculator will definitely not do the binary decimals. It's too stupid. :)

Interestingly, if you shift the decimal point in the initial number by 2x either way and you shift the decimal in the result by x, you still get a good approximation of the new initial number.