- 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
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]
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
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]