This may be used in masking and some algos related to number systems (for ex. in crypting)

Any odd number may be represeted by difference of two squares like:

N = X^2 - Y^2

we can use X = (N+1)/2 and Y=X-1 ?Svin :)

so we can know those X and Y by three clocks in assembly

eax = N

lea ecx,

shr ecx,1 ;ecx = X

lea ebx, ;ebx = Y eax = ecx^2 - ebx^2

Now if we to know values of ecx^2 and edx^2 we can perform just one mul not two

;if edx < 10000h

mov eax,ebx

mul eax ;eax = ebx^2

lea edx,[1] ;edx = ecx^2

Any odd number may be represeted by difference of two squares like:

N = X^2 - Y^2

we can use X = (N+1)/2 and Y=X-1 ?Svin :)

so we can know those X and Y by three clocks in assembly

eax = N

lea ecx,

shr ecx,1 ;ecx = X

lea ebx, ;ebx = Y eax = ecx^2 - ebx^2

Now if we to know values of ecx^2 and edx^2 we can perform just one mul not two

;if edx < 10000h

mov eax,ebx

mul eax ;eax = ebx^2

lea edx,[1] ;edx = ecx^2

Maybe this can be used also:

N=X^3 - Y^3 what equals: (X-Y)(X^2 + XY + Y^2)

You need a buffer to store N, and of course X and Y.

The code would be:

mov eax, X

mov ebx, Y

push eax

sub eax, ebx ;X - Y

mov N1, eax ;store in N1

pop eax ;eax has lost its value

shl eax,1 ;X^2

mov N2, eax ;save in N2

shr eax,1 ;store X into eax

mul ebx ; XY

mov N3, eax ;save in N3

shl ebx,1 ; Y^2

mov N4, ebx ;save in N4

add N2, N3 ;X^2 + XY + Y^2)

add N2, N4 ;

mov ebx, N2 ;save in ebx

mov eax, N1 ;eax is (X - Y)

mul ebx ;(X-Y)(X^2 + XY + Y^2)

mov N, eax ;save into N

N=X^3 - Y^3 what equals: (X-Y)(X^2 + XY + Y^2)

You need a buffer to store N, and of course X and Y.

The code would be:

mov eax, X

mov ebx, Y

push eax

sub eax, ebx ;X - Y

mov N1, eax ;store in N1

pop eax ;eax has lost its value

shl eax,1 ;X^2

mov N2, eax ;save in N2

shr eax,1 ;store X into eax

mul ebx ; XY

mov N3, eax ;save in N3

shl ebx,1 ; Y^2

mov N4, ebx ;save in N4

add N2, N3 ;X^2 + XY + Y^2)

add N2, N4 ;

mov ebx, N2 ;save in ebx

mov eax, N1 ;eax is (X - Y)

mul ebx ;(X-Y)(X^2 + XY + Y^2)

mov N, eax ;save into N

I am having now a problem with vectors. Today a friend told me that he needed to calculate magnitude of vectors given angles and magnitude of the resultant. This is the example he showed me:

"There are two vectors. They represent strenghts. They each make a 18 grades angle with the axis of the resultant vector, that has a magnitude of 6000. I need to find the magnitude of the 18 grades vectors". It would be very good if you post some help, he needs the algorithm (i wont tell him i discovered it, i will give you the credits ;).

Thanks

"There are two vectors. They represent strenghts. They each make a 18 grades angle with the axis of the resultant vector, that has a magnitude of 6000. I need to find the magnitude of the 18 grades vectors". It would be very good if you post some help, he needs the algorithm (i wont tell him i discovered it, i will give you the credits ;).

Thanks

Maybe this illustrates the problem.

**The Svin**, that is a nice piece of code. :)

I would love a class where you were the teacher!

Let me pull up a chair...

**CodeLover**,

__shl eax,2__does not equal (eax)^2.

Unless eax = 2.

CodeLover,

Each of the vectors can be broken down into an x

component and a y component. Using your diagram,

the x-axis is horizontal the y-axis is vertical,

and the two vectors appear to be 18 degrees above

and 18 degrees below the x-axis. And if the two

vectors are of equal magnitude then the problem

is just a matter of finding the magnitude so that

the x component of each vector is 3000, so that

when the x component of both vectors are added

together the resulting vector lies on the x-axis

with a magnitude of 6000; the sum of the two

vectors. Don't confuse my x with your

X(magnitude).

The x component of each vector would be:

vector.x = X( cos( 18 ))

and

6000 = 2( vector.x )= 2( X( cos( 18 ))) = 6000

solve for X

hope this helps and that I haven't made a stupid

error!

farrier

Each of the vectors can be broken down into an x

component and a y component. Using your diagram,

the x-axis is horizontal the y-axis is vertical,

and the two vectors appear to be 18 degrees above

and 18 degrees below the x-axis. And if the two

vectors are of equal magnitude then the problem

is just a matter of finding the magnitude so that

the x component of each vector is 3000, so that

when the x component of both vectors are added

together the resulting vector lies on the x-axis

with a magnitude of 6000; the sum of the two

vectors. Don't confuse my x with your

X(magnitude).

The x component of each vector would be:

vector.x = X( cos( 18 ))

and

6000 = 2( vector.x )= 2( X( cos( 18 ))) = 6000

solve for X

hope this helps and that I haven't made a stupid

error!

farrier

bitRAKE, i don't see where i put "shl eax,2", i wrote always shl eax,1.

Thank you farrier, what i've done was seeing it as a triangle, with one side 18 and the other 6000. If you watch at the graph, the x axis works well as 6000, the magnitude of one of the vectors is the hypotenuse, and the angle is 18. So, as the cosine of an angle is Adj/hyp:

Cos (18)=6000/magnitude, so:

6000/cos (18)=magnitude, what is, approximately:

magnitude= 6308,77334543, what makes sense.

Thanks you farrier, your help enlightened my problem.

Thank you farrier, what i've done was seeing it as a triangle, with one side 18 and the other 6000. If you watch at the graph, the x axis works well as 6000, the magnitude of one of the vectors is the hypotenuse, and the angle is 18. So, as the cosine of an angle is Adj/hyp:

Cos (18)=6000/magnitude, so:

6000/cos (18)=magnitude, what is, approximately:

magnitude= 6308,77334543, what makes sense.

Thanks you farrier, your help enlightened my problem.

Sorry, typo. Can you explain this line for me?

shl eax,1 ;X^2

shl eax,1 ;X^2

Sorry bitRAKE, i made a stupid mistake :). Anyway, i have never written "shl eax,2" ;).

You were right friend, i should have written the piece of code like this:

mov eax, X

mov ebx, Y

push eax

sub eax, ebx ;X - Y

mov N1, eax ;store in N1

pop eax ;eax has lost its value

push eax

mul eax ;X^2

mov N2, eax ;save in N2

pop eax ;store X into eax

mul ebx ; XY

mov N3, eax ;save in N3

mov eax,ebx ;

mul eax ; Y^2

mov N4, eax ;save in N4

add N2, N3 ;X^2 + XY + Y^2)

add N2, N4 ;

mov ebx, N2 ;save in ebx

mov eax, N1 ;eax is (X - Y)

mul ebx ;(X-Y)(X^2 + XY + Y^2)

mov N, eax ;save into N

You were right friend, i should have written the piece of code like this:

mov eax, X

mov ebx, Y

push eax

sub eax, ebx ;X - Y

mov N1, eax ;store in N1

pop eax ;eax has lost its value

push eax

mul eax ;X^2

mov N2, eax ;save in N2

pop eax ;store X into eax

mul ebx ; XY

mov N3, eax ;save in N3

mov eax,ebx ;

mul eax ; Y^2

mov N4, eax ;save in N4

add N2, N3 ;X^2 + XY + Y^2)

add N2, N4 ;

mov ebx, N2 ;save in ebx

mov eax, N1 ;eax is (X - Y)

mul ebx ;(X-Y)(X^2 + XY + Y^2)

mov N, eax ;save into N

N=X^3 - Y^3 what equals: (X-Y)(X^2 + XY + Y^2)

(X-Y)(X^2 + XY + Y^2) = (x-y)[(x+y)^2-xy]

mov ebx,x

mov ecx,y

mov eax,ebx

sub ebx,ecx ;x-y

push eax

mul ecx ;xy

mov edx,ecx ;save x

mov ecx,eax ; ecx=xy

pop eax ; get y

add eax,edx ;x+y

mul eax

sub eax,ecx ;(x+y)^2-xy

mul ebx

mov N,eax ;

bye

eko

(X-Y)(X^2 + XY + Y^2) = (x-y)[(x+y)^2-xy]

mov ebx,x

mov ecx,y

mov eax,ebx

sub ebx,ecx ;x-y

push eax

mul ecx ;xy

mov edx,ecx ;save x

mov ecx,eax ; ecx=xy

pop eax ; get y

add eax,edx ;x+y

mul eax

sub eax,ecx ;(x+y)^2-xy

mul ebx

mov N,eax ;

bye

eko

This can be used also

mov eax,X

push eax

mul eax

pop ebx

mul ebx

mov N1, eax

mov eax, Y

push eax

mul eax

pop ebx

mul ebx

mov ebx, N1

sub ebx,eax

mov N, ebx

mov eax,X

push eax

mul eax

pop ebx

mul ebx

mov N1, eax

mov eax, Y

push eax

mul eax

pop ebx

mul ebx

mov ebx, N1

sub ebx,eax

mov N, ebx

How about this?:

```
mov ebx,x
```

mov ecx,y

mov eax,ebx ; x

sub ebx,ecx ; ebx=x-y

mul ecx ; xy

add ecx,x ; x+y

xchg ecx,eax ; eax=(x+y) ; ecx=xy

mul eax

sub eax,ecx ; (x+y)^2-xy

mul ebx

mov N,eax

Still only three **mul**.bitRake: xchg ecx,eax nice one . i havnt thought about it

codelover: your code is still with 4 mul

bye

eko:alright:

codelover: your code is still with 4 mul

bye

eko:alright:

I would replace 2 commented instructions by these 3.

They take 2 clocks instead of 5 by xchg r,r and add r,m

Apart from this your code is very well organized and eko gave IMHO the best

possible formula transformation, though IMHO FPU fits better for long equations:

mov ebx,x

mov ecx,y

mov eax,ebx ; x

mul ecx ; xy

push eax ;!

sub ebx,ecx ; ebx=x-y

lea eax, ;!

; add ecx,x ; x+y

; xchg ecx,eax ; eax=(x+y) ; ecx=xy

pop ecx ;!

mul eax

sub eax,ecx ; (x+y)^2-xy

mul ebx

mov N,eax

They take 2 clocks instead of 5 by xchg r,r and add r,m

Apart from this your code is very well organized and eko gave IMHO the best

possible formula transformation, though IMHO FPU fits better for long equations:

mov ebx,x

mov ecx,y

mov eax,ebx ; x

mul ecx ; xy

push eax ;!

sub ebx,ecx ; ebx=x-y

lea eax, ;!

; add ecx,x ; x+y

; xchg ecx,eax ; eax=(x+y) ; ecx=xy

pop ecx ;!

mul eax

sub eax,ecx ; (x+y)^2-xy

mul ebx

mov N,eax

; ebx-x

; ecx-y

1 lea eax, ; a=x+y

1 neg ecx ; c=-y

10 imul ebx ; a=(x+y)*x

1 lea edi, ; i=x-y

11 imul ecx,ecx ; s=y*y

1 add eax,ecx ; a=(x+y)*x+y*y

10 imul edi ; a=((x+y)*x+y*y)*(x-y)

=35

; ecx-y

1 lea eax, ; a=x+y

1 neg ecx ; c=-y

10 imul ebx ; a=(x+y)*x

1 lea edi, ; i=x-y

11 imul ecx,ecx ; s=y*y

1 add eax,ecx ; a=(x+y)*x+y*y

10 imul edi ; a=((x+y)*x+y*y)*(x-y)

=35

ebx=x

eax=y

^ result in edx

or instead the last two lines

sub eax,edx (xy-(x+y)^2)

imul ebx,-1 ; result in eax (x-y)[-1(xy-(x+y)^2)]

3 registers

dont know how much clocks

eax=y

```
lea edx,[ebx+eax];d=x+y
```

neg edx = d=-x-y

imul ebx;a=xy

lea ebx,[edx+2*ebx];b=-y-x+2x=x-y

imul edx,edx ; d =(x+y)^2

sub edx, eax;d=(x+y)^2-xy

imul edx,ebx ; (x-y)((x+y)^2-xy)

^ result in edx

or instead the last two lines

sub eax,edx (xy-(x+y)^2)

imul ebx,-1 ; result in eax (x-y)[-1(xy-(x+y)^2)]

3 registers

dont know how much clocks

How could you optimize not knowing clock table :)

But in the case it doesn't matter - use what I wrote as clock referrence.

The thing is that if you use imul reg,reg (additional parameter)

it result in one extra byte for prefix and one extra clock(11 instead of 10) so it will be 36 clocks instead of 35 in my version.

Nevertheless I enjoyed the formula transformation. It's good and nessesery training.

Need to be done on every day basics.

Most of uneffective code is written 'cause people don't care to calculate or don't use even basic (5th-6th form) arithmetics.

Unfortunatly I just don't have talent to show it :(

But with exchaging basic math solutions as we do now may be it would be a little bit better :)

But in the case it doesn't matter - use what I wrote as clock referrence.

The thing is that if you use imul reg,reg (additional parameter)

it result in one extra byte for prefix and one extra clock(11 instead of 10) so it will be 36 clocks instead of 35 in my version.

Nevertheless I enjoyed the formula transformation. It's good and nessesery training.

Need to be done on every day basics.

Most of uneffective code is written 'cause people don't care to calculate or don't use even basic (5th-6th form) arithmetics.

Unfortunatly I just don't have talent to show it :(

But with exchaging basic math solutions as we do now may be it would be a little bit better :)

hii ..

about the clock table .. i know the basic things . that lea is 1 clock the same for add,sub,and and etc..

but there are things i dont understand like :

you wrote imul with only one parameter 10 clock when i look at help files with clock table . imul reg 12-42 clocks on 486 ........

how do i know how much clocks mul takes ?

i realy like math . and I like to get into the little things ( thats way i code in asm ) but i have alot to learn (i'm in the 11 grade )

bye

eko

about the clock table .. i know the basic things . that lea is 1 clock the same for add,sub,and and etc..

but there are things i dont understand like :

you wrote imul with only one parameter 10 clock when i look at help files with clock table . imul reg 12-42 clocks on 486 ........

how do i know how much clocks mul takes ?

i realy like math . and I like to get into the little things ( thats way i code in asm ) but i have alot to learn (i'm in the 11 grade )

bye

eko

One of places where you can find clock tables is Anger.hlp which,

I belive is included in MASM32.

Despite of difference in clocking particular instructions the main

idea of my previous post was that

imul reg,reg

is always slower than

imul reg

and longer.

I'm glad that we are both agree on importance of math knoulidge and training in asm programming.

Couple thoughts:

1. Most of the beginners here know FPU badly.

And to know it better there are always three general methods:

- To see examples of particular equitions or other formulas realized in asm

- To read explonations and comments in those examples

- Try to make realization of math equtions yourselfe.

The most productive way is when complexity of tasks and examples

grows slow but at the number of exersizes to be done and examples to be learnt is quite big. (At least 15-20 a day)

It makes it easier even for very beginner to get used to the tasks and feel in making math as a fish in the water.

What do you think if we start giving ourself right here exersizes (starting with very basic arithmetics) and try to do exersizes of each other in both fpu and interger. And include comments and

explanations?

I belive is included in MASM32.

Despite of difference in clocking particular instructions the main

idea of my previous post was that

imul reg,reg

is always slower than

imul reg

and longer.

I'm glad that we are both agree on importance of math knoulidge and training in asm programming.

Couple thoughts:

1. Most of the beginners here know FPU badly.

And to know it better there are always three general methods:

- To see examples of particular equitions or other formulas realized in asm

- To read explonations and comments in those examples

- Try to make realization of math equtions yourselfe.

The most productive way is when complexity of tasks and examples

grows slow but at the number of exersizes to be done and examples to be learnt is quite big. (At least 15-20 a day)

It makes it easier even for very beginner to get used to the tasks and feel in making math as a fish in the water.

What do you think if we start giving ourself right here exersizes (starting with very basic arithmetics) and try to do exersizes of each other in both fpu and interger. And include comments and

explanations?

What do you think if we start giving ourself right here exersizes (starting with very basic arithmetics) and try to do exersizes of each other in both fpu and interger. And include comments and

yeah i realy like the idea ! and my fpu knowlege is bad :( so exercise will be good for me

here is a realy simple start(maybe continue) of integer math

(i dont have any idea for other equation maybe you can choose one)

n=x^2-y^2 finding n in the shortest way

(x-y)(X+Y)

eax -x

ebx -y

lea ecx, ;c=x+y

sub eax,ebx;a =x-y ;

mul ecx ; a =(x-y)(X+Y)

i guess this one too easy ,nothing comes into my mind

bye

eko