-
May 19th, 2012, 04:11 PM
#1
Thread Starter
Fanatic Member
Optimization.
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)
Assembly Code:
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;
-
May 19th, 2012, 04:38 PM
#2
Thread Starter
Fanatic Member
Re: Optimization.
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.
-
May 20th, 2012, 10:52 AM
#3
Re: Optimization.
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!
-
May 27th, 2012, 07:56 AM
#4
Thread Starter
Fanatic Member
Re: Optimization.
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.
asm Code:
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;
Comments might not be valid anymore.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|