Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!

Optimization.

BlindSniper

New member
Joined
Jan 4, 2011
Messages
865
Hi all,
Recently I've taken it upon myself to learn some assembly, to aid my software development skills and i've tried some basic code and taught myself some registers and Now i've succeeded in coding my first algorithm in assembly.(albeit an easy one). I've coded the subtraction based Euclidean algorithm
Code:
function gcd(a, b)
    if a = 0
       return b
    while b ≠ 0
        if a > b
           a := a − b
        else
           b := b − a
    return a
My assembly is a very literal translation, so Please give me some tips on how to optimize it.
This was written in Delphi 7 Inline Assembler (BASM)
[highlight=vb]
test ecx,ecx; // Check If Zero
jnz @@Continue; //IF it is not Zero Go to @@Continue, Else go on
mov eax,edx; //Put second paramater as result
ret; //Return
@@Continue:
test edx,edx; //Begin of while loop
jz @@Wend; // If second paramater is zero go to the end
cmp ecx,eax; // Compare First and second paramater
jnb @@Lower; //If First > second then go to @@Lower
jmp @@Higher; //Else go to higher
@@Lower:
sub ecx,edx; // first = first - second
jmp @@Continue; // next iteration of while loop
@@Higher:
sub edx,ecx; // second = second - first
jmp @@Continue;// next iteration of while loop
@@Wend: //While loop ended, need to return value.
mov eax,ecx;
[/highlight]
 

BlindSniper

New member
Joined
Jan 4, 2011
Messages
865
I've discovered that it works for 5 and 15 but not for 21 and 6, I'll look into it when it is morning.
 

szlamany

MS SQL Powerposter
Joined
Mar 5, 2004
Messages
17,872
I was looking at your code and I see that you have a CMP ECX,EAX that will JNB @@LOWER

Then next like of code is JMP @@HIGHER

That line of code can be removed if you simply put @@HIGHER: logic above the @@LOWER logic - it will simply fall through to that spot.

Meaning you don't need the @@HIGHER: label at all.

Since your logic is only about 12 operations that is around an 8% decrease in CPU use!
 

BlindSniper

New member
Joined
Jan 4, 2011
Messages
865
Ahh, Thank you.
My week was busy but I got around fixing it.
I also added the optimizations you suggested.

This is done for what ever calling convention Delphi(Pascal) uses.
[highlight=vb]
test ecx,ecx; // Check If Zero
jnz @@Continue; //IF it is not Zero Go to @@Continue, Else go on
mov eax,edx; //Put second paramater as result
ret; //Return
@@Continue:
test edx,edx; //Begin of while loop
jz @@Wend; // If second paramater is zero go to the end
cmp ecx,edx; // Compare First and second paramater
jnbe @@Lower; //If First > second then go to @@Lower
sub edx,ecx; // second = second - first
jmp @@Continue;// next iteration of while loop
@@Lower:
sub ecx,edx; // first = first - second
jmp @@Continue; // next iteration of while loop
@@Wend: //While loop ended, need to return value.
mov eax,ecx;
[/highlight]
Comments might not be valid anymore.
 
Top